changeset 4460:c5d46cf53534

8166333: jshell tool: shortcut var does not import its type Reviewed-by: jlahoda
author rfield
date Fri, 11 Nov 2016 11:50:11 -0800
parents 4c650abde107
children 768f685bceb7
files src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
diffstat 1 files changed, 37 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java	Fri Nov 11 17:45:29 2016 +0100
+++ b/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 + ";");