# HG changeset patch # User rfield # Date 1478893811 28800 # Node ID 389212e0746c2db28e9dc8aeeecd79470c503c8c # Parent 1ac75bf2dc3af7e56a3e6f64242fa4080e341e11 8166333: jshell tool: shortcut var does not import its type Reviewed-by: jlahoda diff -r 1ac75bf2dc3a -r 389212e0746c 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 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 + ";");