8166333: jshell tool: shortcut var does not import its type
authorrfield
Fri, 11 Nov 2016 11:50:11 -0800
changeset 41996 389212e0746c
parent 41995 1ac75bf2dc3a
child 41997 79da2a8f4274
8166333: jshell tool: shortcut var does not import its type Reviewed-by: jlahoda
langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
--- a/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java	Fri Nov 11 17:45:29 2016 +0100
+++ b/langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java	Fri Nov 11 11:50:11 2016 -0800
@@ -57,7 +57,6 @@
 import jdk.internal.jline.TerminalSupport;
 import jdk.internal.jline.WindowsTerminal;
 import jdk.internal.jline.console.ConsoleReader;
-import jdk.internal.jline.console.CursorBuffer;
 import jdk.internal.jline.console.KeyMap;
 import jdk.internal.jline.console.UserInterruptException;
 import jdk.internal.jline.console.completer.Completer;
@@ -581,26 +580,55 @@
 
     private static final FixComputer[] FIX_COMPUTERS = new FixComputer[] {
         new FixComputer('v', false) { //compute "Introduce variable" Fix:
+            private void performToVar(ConsoleReader in, String type) throws IOException {
+                in.redrawLine();
+                in.setCursorPosition(0);
+                in.putString(type + "  = ");
+                in.setCursorPosition(in.getCursorBuffer().cursor - 3);
+                in.flush();
+            }
+
             @Override
             public FixResult compute(JShellTool repl, String code, int cursor) {
                 String type = repl.analysis.analyzeType(code, cursor);
                 if (type == null) {
                     return new FixResult(Collections.emptyList(), null);
                 }
-                return new FixResult(Collections.singletonList(new Fix() {
+                List<Fix> fixes = new ArrayList<>();
+                fixes.add(new Fix() {
                     @Override
                     public String displayName() {
                         return repl.messageFormat("jshell.console.create.variable");
                     }
+
                     @Override
                     public void perform(ConsoleReader in) throws IOException {
-                        in.redrawLine();
-                        in.setCursorPosition(0);
-                        in.putString(type + "  = ");
-                        in.setCursorPosition(in.getCursorBuffer().cursor - 3);
-                        in.flush();
+                        performToVar(in, type);
                     }
-                }), null);
+                });
+                int idx = type.lastIndexOf(".");
+                if (idx > 0) {
+                    String stype = type.substring(idx + 1);
+                    QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
+                    if (res.isUpToDate() && res.getNames().contains(type)
+                            && !res.isResolvable()) {
+                        fixes.add(new Fix() {
+                            @Override
+                            public String displayName() {
+                                return "import: " + type + ". " +
+                                        repl.messageFormat("jshell.console.create.variable");
+                            }
+
+                            @Override
+                            public void perform(ConsoleReader in) throws IOException {
+                                repl.state.eval("import " + type + ";");
+                                in.println("Imported: " + type);
+                                performToVar(in, stype);
+                            }
+                        });
+                    }
+                }
+                return new FixResult(fixes, null);
             }
         },
         new FixComputer('i', true) { //compute "Add import" Fixes:
@@ -614,6 +642,7 @@
                         public String displayName() {
                             return "import: " + fqn;
                         }
+
                         @Override
                         public void perform(ConsoleReader in) throws IOException {
                             repl.state.eval("import " + fqn + ";");