author | jlahoda |
Wed, 02 Nov 2016 07:38:37 +0100 | |
changeset 41865 | 3ef02797070d |
parent 41631 | a348d1cc8d9d |
child 41934 | a4da50688dc7 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
38521 | 2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.internal.jshell.tool; |
|
27 |
||
41865 | 28 |
import jdk.jshell.SourceCodeAnalysis.Documentation; |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
29 |
import jdk.jshell.SourceCodeAnalysis.QualifiedNames; |
33362 | 30 |
import jdk.jshell.SourceCodeAnalysis.Suggestion; |
31 |
||
32 |
import java.awt.event.ActionListener; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.InputStream; |
|
41628 | 35 |
import java.io.InterruptedIOException; |
33362 | 36 |
import java.io.PrintStream; |
37 |
import java.io.UncheckedIOException; |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
38 |
import java.util.ArrayList; |
41865 | 39 |
import java.util.Arrays; |
38521 | 40 |
import java.util.Collection; |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
41 |
import java.util.Collections; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
42 |
import java.util.HashMap; |
41865 | 43 |
import java.util.Iterator; |
33362 | 44 |
import java.util.List; |
45 |
import java.util.Locale; |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
46 |
import java.util.Map; |
33362 | 47 |
import java.util.Objects; |
48 |
import java.util.Optional; |
|
41865 | 49 |
import java.util.function.Function; |
38521 | 50 |
import java.util.prefs.BackingStoreException; |
51 |
import java.util.stream.Collectors; |
|
52 |
import java.util.stream.Stream; |
|
33362 | 53 |
|
41865 | 54 |
import jdk.internal.shellsupport.doc.JavadocFormatter; |
33362 | 55 |
import jdk.internal.jline.NoInterruptUnixTerminal; |
56 |
import jdk.internal.jline.Terminal; |
|
57 |
import jdk.internal.jline.TerminalFactory; |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
58 |
import jdk.internal.jline.TerminalSupport; |
33362 | 59 |
import jdk.internal.jline.WindowsTerminal; |
60 |
import jdk.internal.jline.console.ConsoleReader; |
|
41865 | 61 |
import jdk.internal.jline.console.CursorBuffer; |
33362 | 62 |
import jdk.internal.jline.console.KeyMap; |
63 |
import jdk.internal.jline.console.UserInterruptException; |
|
64 |
import jdk.internal.jline.console.completer.Completer; |
|
40767 | 65 |
import jdk.internal.jline.console.history.History; |
66 |
import jdk.internal.jline.console.history.MemoryHistory; |
|
38521 | 67 |
import jdk.internal.jline.extra.EditingHistory; |
33362 | 68 |
import jdk.internal.jshell.tool.StopDetectingInputStream.State; |
41631
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
69 |
import jdk.internal.misc.Signal; |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
70 |
import jdk.internal.misc.Signal.Handler; |
33362 | 71 |
|
72 |
class ConsoleIOContext extends IOContext { |
|
73 |
||
38521 | 74 |
private static final String HISTORY_LINE_PREFIX = "HISTORY_LINE_"; |
75 |
||
33362 | 76 |
final JShellTool repl; |
77 |
final StopDetectingInputStream input; |
|
78 |
final ConsoleReader in; |
|
79 |
final EditingHistory history; |
|
40767 | 80 |
final MemoryHistory userInputHistory = new MemoryHistory(); |
33362 | 81 |
|
82 |
String prefix = ""; |
|
83 |
||
84 |
ConsoleIOContext(JShellTool repl, InputStream cmdin, PrintStream cmdout) throws Exception { |
|
85 |
this.repl = repl; |
|
86 |
this.input = new StopDetectingInputStream(() -> repl.state.stop(), ex -> repl.hard("Error on input: %s", ex)); |
|
87 |
Terminal term; |
|
36501 | 88 |
if (System.getProperty("test.jdk") != null) { |
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
89 |
term = new TestTerminal(input); |
36501 | 90 |
} else if (System.getProperty("os.name").toLowerCase(Locale.US).contains(TerminalFactory.WINDOWS)) { |
33362 | 91 |
term = new JShellWindowsTerminal(input); |
92 |
} else { |
|
93 |
term = new JShellUnixTerminal(input); |
|
94 |
} |
|
95 |
term.init(); |
|
96 |
in = new ConsoleReader(cmdin, cmdout, term); |
|
97 |
in.setExpandEvents(false); |
|
98 |
in.setHandleUserInterrupt(true); |
|
38521 | 99 |
List<String> persistenHistory = Stream.of(repl.prefs.keys()) |
100 |
.filter(key -> key.startsWith(HISTORY_LINE_PREFIX)) |
|
101 |
.sorted() |
|
102 |
.map(key -> repl.prefs.get(key, null)) |
|
103 |
.collect(Collectors.toList()); |
|
104 |
in.setHistory(history = new EditingHistory(in, persistenHistory) { |
|
105 |
@Override protected boolean isComplete(CharSequence input) { |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38541
diff
changeset
|
106 |
return repl.analysis.analyzeCompletion(input.toString()).completeness().isComplete(); |
33362 | 107 |
} |
108 |
}); |
|
109 |
in.setBellEnabled(true); |
|
38541
44e95493fd13
8131017: jshell tool: pasting code with tabs invokes tab completion
jlahoda
parents:
38521
diff
changeset
|
110 |
in.setCopyPasteDetection(true); |
33362 | 111 |
in.addCompleter(new Completer() { |
112 |
private String lastTest; |
|
113 |
private int lastCursor; |
|
114 |
private boolean allowSmart = false; |
|
115 |
@Override public int complete(String test, int cursor, List<CharSequence> result) { |
|
116 |
int[] anchor = new int[] {-1}; |
|
117 |
List<Suggestion> suggestions; |
|
118 |
if (prefix.isEmpty() && test.trim().startsWith("/")) { |
|
119 |
suggestions = repl.commandCompletionSuggestions(test, cursor, anchor); |
|
120 |
} else { |
|
121 |
int prefixLength = prefix.length(); |
|
122 |
suggestions = repl.analysis.completionSuggestions(prefix + test, cursor + prefixLength, anchor); |
|
123 |
anchor[0] -= prefixLength; |
|
124 |
} |
|
125 |
if (!Objects.equals(lastTest, test) || lastCursor != cursor) |
|
126 |
allowSmart = true; |
|
127 |
||
128 |
boolean smart = allowSmart && |
|
129 |
suggestions.stream() |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38541
diff
changeset
|
130 |
.anyMatch(s -> s.matchesType()); |
33362 | 131 |
|
132 |
lastTest = test; |
|
133 |
lastCursor = cursor; |
|
134 |
allowSmart = !allowSmart; |
|
135 |
||
136 |
suggestions.stream() |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38541
diff
changeset
|
137 |
.filter(s -> !smart || s.matchesType()) |
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38541
diff
changeset
|
138 |
.map(s -> s.continuation()) |
33362 | 139 |
.forEach(result::add); |
140 |
||
141 |
boolean onlySmart = suggestions.stream() |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38541
diff
changeset
|
142 |
.allMatch(s -> s.matchesType()); |
33362 | 143 |
|
144 |
if (smart && !onlySmart) { |
|
145 |
Optional<String> prefix = |
|
146 |
suggestions.stream() |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38541
diff
changeset
|
147 |
.map(s -> s.continuation()) |
33362 | 148 |
.reduce(ConsoleIOContext::commonPrefix); |
149 |
||
150 |
String prefixStr = prefix.orElse("").substring(cursor - anchor[0]); |
|
151 |
try { |
|
152 |
in.putString(prefixStr); |
|
153 |
cursor += prefixStr.length(); |
|
154 |
} catch (IOException ex) { |
|
155 |
throw new IllegalStateException(ex); |
|
156 |
} |
|
36990 | 157 |
result.add(repl.messageFormat("jshell.console.see.more")); |
33362 | 158 |
return cursor; //anchor should not be used. |
159 |
} |
|
160 |
||
161 |
if (result.isEmpty()) { |
|
162 |
try { |
|
163 |
//provide "empty completion" feedback |
|
164 |
//XXX: this only works correctly when there is only one Completer: |
|
165 |
in.beep(); |
|
166 |
} catch (IOException ex) { |
|
167 |
throw new UncheckedIOException(ex); |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
return anchor[0]; |
|
172 |
} |
|
173 |
}); |
|
174 |
bind(DOCUMENTATION_SHORTCUT, (ActionListener) evt -> documentation(repl)); |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
175 |
for (FixComputer computer : FIX_COMPUTERS) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
176 |
for (String shortcuts : SHORTCUT_FIXES) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
177 |
bind(shortcuts + computer.shortcut, (ActionListener) evt -> fixes(computer)); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
178 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
179 |
} |
41631
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
180 |
try { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
181 |
Signal.handle(new Signal("CONT"), new Handler() { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
182 |
@Override public void handle(Signal sig) { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
183 |
try { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
184 |
in.getTerminal().reset(); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
185 |
in.redrawLine(); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
186 |
in.flush(); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
187 |
} catch (Exception ex) { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
188 |
ex.printStackTrace(); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
189 |
} |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
190 |
} |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
191 |
}); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
192 |
} catch (IllegalArgumentException ignored) { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
193 |
//the CONT signal does not exist on this platform |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
194 |
} |
33362 | 195 |
} |
196 |
||
197 |
@Override |
|
198 |
public String readLine(String prompt, String prefix) throws IOException, InputInterruptedException { |
|
199 |
this.prefix = prefix; |
|
200 |
try { |
|
201 |
return in.readLine(prompt); |
|
202 |
} catch (UserInterruptException ex) { |
|
203 |
throw (InputInterruptedException) new InputInterruptedException().initCause(ex); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
@Override |
|
208 |
public boolean interactiveOutput() { |
|
209 |
return true; |
|
210 |
} |
|
211 |
||
212 |
@Override |
|
213 |
public Iterable<String> currentSessionHistory() { |
|
214 |
return history.currentSessionEntries(); |
|
215 |
} |
|
216 |
||
217 |
@Override |
|
218 |
public void close() throws IOException { |
|
38521 | 219 |
//save history: |
220 |
try { |
|
221 |
for (String key : repl.prefs.keys()) { |
|
222 |
if (key.startsWith(HISTORY_LINE_PREFIX)) |
|
223 |
repl.prefs.remove(key); |
|
224 |
} |
|
225 |
Collection<? extends String> savedHistory = history.save(); |
|
226 |
if (!savedHistory.isEmpty()) { |
|
227 |
int len = (int) Math.ceil(Math.log10(savedHistory.size()+1)); |
|
228 |
String format = HISTORY_LINE_PREFIX + "%0" + len + "d"; |
|
229 |
int index = 0; |
|
230 |
for (String historyLine : savedHistory) { |
|
231 |
repl.prefs.put(String.format(format, index++), historyLine); |
|
232 |
} |
|
233 |
} |
|
234 |
} catch (BackingStoreException ex) { |
|
235 |
throw new IllegalStateException(ex); |
|
236 |
} |
|
33362 | 237 |
in.shutdown(); |
238 |
try { |
|
239 |
in.getTerminal().restore(); |
|
240 |
} catch (Exception ex) { |
|
241 |
throw new IOException(ex); |
|
242 |
} |
|
38909
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
38908
diff
changeset
|
243 |
input.shutdown(); |
33362 | 244 |
} |
245 |
||
246 |
private void bind(String shortcut, Object action) { |
|
247 |
KeyMap km = in.getKeys(); |
|
248 |
for (int i = 0; i < shortcut.length(); i++) { |
|
249 |
Object value = km.getBound(Character.toString(shortcut.charAt(i))); |
|
250 |
if (value instanceof KeyMap) { |
|
251 |
km = (KeyMap) value; |
|
252 |
} else { |
|
253 |
km.bind(shortcut.substring(i), action); |
|
254 |
} |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
private static final String DOCUMENTATION_SHORTCUT = "\033\133\132"; //Shift-TAB |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
259 |
private static final String[] SHORTCUT_FIXES = { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
260 |
"\033\015", //Alt-Enter (Linux) |
39811
b8d3d2487379
8162255: 'Alt-Enter v'/'Alt-Enter i' not working on some terminals
jlahoda
parents:
38909
diff
changeset
|
261 |
"\033\012", //Alt-Enter (Linux) |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
262 |
"\033\133\061\067\176", //F6/Alt-F1 (Mac) |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
263 |
"\u001BO3P" //Alt-F1 (Linux) |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
264 |
}; |
33362 | 265 |
|
41865 | 266 |
private String lastDocumentationBuffer; |
267 |
private int lastDocumentationCursor = (-1); |
|
268 |
||
33362 | 269 |
private void documentation(JShellTool repl) { |
270 |
String buffer = in.getCursorBuffer().buffer.toString(); |
|
271 |
int cursor = in.getCursorBuffer().cursor; |
|
41865 | 272 |
boolean firstInvocation = !buffer.equals(lastDocumentationBuffer) || cursor != lastDocumentationCursor; |
273 |
lastDocumentationBuffer = buffer; |
|
274 |
lastDocumentationCursor = cursor; |
|
275 |
List<String> doc; |
|
276 |
String seeMore; |
|
277 |
Terminal term = in.getTerminal(); |
|
33362 | 278 |
if (prefix.isEmpty() && buffer.trim().startsWith("/")) { |
41865 | 279 |
doc = Arrays.asList(repl.commandDocumentation(buffer, cursor, firstInvocation)); |
280 |
seeMore = "jshell.console.see.help"; |
|
33362 | 281 |
} else { |
41865 | 282 |
JavadocFormatter formatter = new JavadocFormatter(term.getWidth(), |
283 |
term.isAnsiSupported()); |
|
284 |
Function<Documentation, String> convertor; |
|
285 |
if (firstInvocation) { |
|
286 |
convertor = d -> d.signature(); |
|
287 |
} else { |
|
288 |
convertor = d -> formatter.formatJavadoc(d.signature(), |
|
289 |
d.javadoc() != null ? d.javadoc() |
|
290 |
: repl.messageFormat("jshell.console.no.javadoc")); |
|
291 |
} |
|
292 |
doc = repl.analysis.documentation(prefix + buffer, cursor + prefix.length(), !firstInvocation) |
|
293 |
.stream() |
|
294 |
.map(convertor) |
|
295 |
.collect(Collectors.toList()); |
|
296 |
seeMore = "jshell.console.see.javadoc"; |
|
33362 | 297 |
} |
298 |
||
299 |
try { |
|
41865 | 300 |
if (doc != null && !doc.isEmpty()) { |
301 |
if (firstInvocation) { |
|
302 |
in.println(); |
|
303 |
in.println(doc.stream().collect(Collectors.joining("\n"))); |
|
304 |
in.println(repl.messageFormat(seeMore)); |
|
305 |
in.redrawLine(); |
|
306 |
in.flush(); |
|
307 |
} else { |
|
308 |
in.println(); |
|
309 |
||
310 |
int height = term.getHeight(); |
|
311 |
String lastNote = ""; |
|
312 |
||
313 |
PRINT_DOC: for (Iterator<String> docIt = doc.iterator(); docIt.hasNext(); ) { |
|
314 |
String currentDoc = docIt.next(); |
|
315 |
String[] lines = currentDoc.split("\n"); |
|
316 |
int firstLine = 0; |
|
317 |
||
318 |
PRINT_PAGE: while (true) { |
|
319 |
int toPrint = height - 1; |
|
320 |
||
321 |
while (toPrint > 0 && firstLine < lines.length) { |
|
322 |
in.println(lines[firstLine++]); |
|
323 |
toPrint--; |
|
324 |
} |
|
325 |
||
326 |
if (firstLine >= lines.length) { |
|
327 |
break; |
|
328 |
} |
|
329 |
||
330 |
lastNote = repl.getResourceString("jshell.console.see.next.page"); |
|
331 |
in.print(lastNote + ConsoleReader.RESET_LINE); |
|
332 |
in.flush(); |
|
333 |
||
334 |
while (true) { |
|
335 |
int r = in.readCharacter(); |
|
336 |
||
337 |
switch (r) { |
|
338 |
case ' ': continue PRINT_PAGE; |
|
339 |
case 'q': |
|
340 |
case 3: |
|
341 |
break PRINT_DOC; |
|
342 |
default: |
|
343 |
in.beep(); |
|
344 |
break; |
|
345 |
} |
|
346 |
} |
|
347 |
} |
|
348 |
||
349 |
if (docIt.hasNext()) { |
|
350 |
lastNote = repl.getResourceString("jshell.console.see.next.javadoc"); |
|
351 |
in.print(lastNote + ConsoleReader.RESET_LINE); |
|
352 |
in.flush(); |
|
353 |
||
354 |
while (true) { |
|
355 |
int r = in.readCharacter(); |
|
356 |
||
357 |
switch (r) { |
|
358 |
case ' ': continue PRINT_DOC; |
|
359 |
case 'q': |
|
360 |
case 3: |
|
361 |
break PRINT_DOC; |
|
362 |
default: |
|
363 |
in.beep(); |
|
364 |
break; |
|
365 |
} |
|
366 |
} |
|
367 |
} |
|
368 |
} |
|
369 |
//clear the "press space" line: |
|
370 |
in.getCursorBuffer().buffer.replace(0, buffer.length(), lastNote); |
|
371 |
in.getCursorBuffer().cursor = 0; |
|
372 |
in.killLine(); |
|
373 |
in.getCursorBuffer().buffer.append(buffer); |
|
374 |
in.getCursorBuffer().cursor = cursor; |
|
375 |
in.redrawLine(); |
|
376 |
in.flush(); |
|
377 |
} |
|
33362 | 378 |
} else { |
379 |
in.beep(); |
|
380 |
} |
|
381 |
} catch (IOException ex) { |
|
382 |
throw new IllegalStateException(ex); |
|
383 |
} |
|
384 |
} |
|
385 |
||
386 |
private static String commonPrefix(String str1, String str2) { |
|
387 |
for (int i = 0; i < str2.length(); i++) { |
|
388 |
if (!str1.startsWith(str2.substring(0, i + 1))) { |
|
389 |
return str2.substring(0, i); |
|
390 |
} |
|
391 |
} |
|
392 |
||
393 |
return str2; |
|
394 |
} |
|
395 |
||
396 |
@Override |
|
397 |
public boolean terminalEditorRunning() { |
|
398 |
Terminal terminal = in.getTerminal(); |
|
399 |
if (terminal instanceof JShellUnixTerminal) |
|
400 |
return ((JShellUnixTerminal) terminal).isRaw(); |
|
401 |
return false; |
|
402 |
} |
|
403 |
||
404 |
@Override |
|
405 |
public void suspend() { |
|
406 |
try { |
|
407 |
in.getTerminal().restore(); |
|
408 |
} catch (Exception ex) { |
|
409 |
throw new IllegalStateException(ex); |
|
410 |
} |
|
411 |
} |
|
412 |
||
413 |
@Override |
|
414 |
public void resume() { |
|
415 |
try { |
|
416 |
in.getTerminal().init(); |
|
417 |
} catch (Exception ex) { |
|
418 |
throw new IllegalStateException(ex); |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
public void beforeUserCode() { |
|
40767 | 423 |
synchronized (this) { |
424 |
inputBytes = null; |
|
425 |
} |
|
33362 | 426 |
input.setState(State.BUFFER); |
427 |
} |
|
428 |
||
429 |
public void afterUserCode() { |
|
430 |
input.setState(State.WAIT); |
|
431 |
} |
|
432 |
||
433 |
@Override |
|
434 |
public void replaceLastHistoryEntry(String source) { |
|
435 |
history.fullHistoryReplace(source); |
|
436 |
} |
|
437 |
||
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
438 |
//compute possible options/Fixes based on the selected FixComputer, present them to the user, |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
439 |
//and perform the selected one: |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
440 |
private void fixes(FixComputer computer) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
441 |
String input = prefix + in.getCursorBuffer().toString(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
442 |
int cursor = prefix.length() + in.getCursorBuffer().cursor; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
443 |
FixResult candidates = computer.compute(repl, input, cursor); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
444 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
445 |
try { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
446 |
final boolean printError = candidates.error != null && !candidates.error.isEmpty(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
447 |
if (printError) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
448 |
in.println(candidates.error); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
449 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
450 |
if (candidates.fixes.isEmpty()) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
451 |
in.beep(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
452 |
if (printError) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
453 |
in.redrawLine(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
454 |
in.flush(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
455 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
456 |
} else if (candidates.fixes.size() == 1 && !computer.showMenu) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
457 |
if (printError) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
458 |
in.redrawLine(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
459 |
in.flush(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
460 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
461 |
candidates.fixes.get(0).perform(in); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
462 |
} else { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
463 |
List<Fix> fixes = new ArrayList<>(candidates.fixes); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
464 |
fixes.add(0, new Fix() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
465 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
466 |
public String displayName() { |
36990 | 467 |
return repl.messageFormat("jshell.console.do.nothing"); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
468 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
469 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
470 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
471 |
public void perform(ConsoleReader in) throws IOException { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
472 |
in.redrawLine(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
473 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
474 |
}); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
475 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
476 |
Map<Character, Fix> char2Fix = new HashMap<>(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
477 |
in.println(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
478 |
for (int i = 0; i < fixes.size(); i++) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
479 |
Fix fix = fixes.get(i); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
480 |
char2Fix.put((char) ('0' + i), fix); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
481 |
in.println("" + i + ": " + fixes.get(i).displayName()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
482 |
} |
36990 | 483 |
in.print(repl.messageFormat("jshell.console.choice")); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
484 |
in.flush(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
485 |
int read; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
486 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
487 |
read = in.readCharacter(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
488 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
489 |
Fix fix = char2Fix.get((char) read); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
490 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
491 |
if (fix == null) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
492 |
in.beep(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
493 |
fix = fixes.get(0); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
494 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
495 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
496 |
in.println(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
497 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
498 |
fix.perform(in); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
499 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
500 |
in.flush(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
501 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
502 |
} catch (IOException ex) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
503 |
ex.printStackTrace(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
504 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
505 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
506 |
|
40767 | 507 |
private byte[] inputBytes; |
508 |
private int inputBytesPointer; |
|
509 |
||
510 |
@Override |
|
41628 | 511 |
public synchronized int readUserInput() throws IOException { |
40767 | 512 |
while (inputBytes == null || inputBytes.length <= inputBytesPointer) { |
513 |
boolean prevHandleUserInterrupt = in.getHandleUserInterrupt(); |
|
514 |
History prevHistory = in.getHistory(); |
|
515 |
||
516 |
try { |
|
517 |
input.setState(State.WAIT); |
|
518 |
in.setHandleUserInterrupt(true); |
|
519 |
in.setHistory(userInputHistory); |
|
520 |
inputBytes = (in.readLine("") + System.getProperty("line.separator")).getBytes(); |
|
521 |
inputBytesPointer = 0; |
|
522 |
} catch (UserInterruptException ex) { |
|
41628 | 523 |
throw new InterruptedIOException(); |
40767 | 524 |
} finally { |
525 |
in.setHistory(prevHistory); |
|
526 |
in.setHandleUserInterrupt(prevHandleUserInterrupt); |
|
527 |
input.setState(State.BUFFER); |
|
528 |
} |
|
529 |
} |
|
530 |
return inputBytes[inputBytesPointer++]; |
|
531 |
} |
|
532 |
||
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
533 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
534 |
* A possible action which the user can choose to perform. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
535 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
536 |
public interface Fix { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
537 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
538 |
* A name that should be shown to the user. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
539 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
540 |
public String displayName(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
541 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
542 |
* Perform the given action. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
543 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
544 |
public void perform(ConsoleReader in) throws IOException; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
545 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
546 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
547 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
548 |
* A factory for {@link Fix}es. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
549 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
550 |
public abstract static class FixComputer { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
551 |
private final char shortcut; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
552 |
private final boolean showMenu; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
553 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
554 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
555 |
* Construct a new FixComputer. {@code shortcut} defines the key which should trigger this FixComputer. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
556 |
* If {@code showMenu} is {@code false}, and this computer returns exactly one {@code Fix}, |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
557 |
* no options will be show to the user, and the given {@code Fix} will be performed. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
558 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
559 |
public FixComputer(char shortcut, boolean showMenu) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
560 |
this.shortcut = shortcut; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
561 |
this.showMenu = showMenu; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
562 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
563 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
564 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
565 |
* Compute possible actions for the given code. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
566 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
567 |
public abstract FixResult compute(JShellTool repl, String code, int cursor); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
568 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
569 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
570 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
571 |
* A list of {@code Fix}es with a possible error that should be shown to the user. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
572 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
573 |
public static class FixResult { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
574 |
public final List<Fix> fixes; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
575 |
public final String error; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
576 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
577 |
public FixResult(List<Fix> fixes, String error) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
578 |
this.fixes = fixes; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
579 |
this.error = error; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
580 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
581 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
582 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
583 |
private static final FixComputer[] FIX_COMPUTERS = new FixComputer[] { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
584 |
new FixComputer('v', false) { //compute "Introduce variable" Fix: |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
585 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
586 |
public FixResult compute(JShellTool repl, String code, int cursor) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
587 |
String type = repl.analysis.analyzeType(code, cursor); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
588 |
if (type == null) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
589 |
return new FixResult(Collections.emptyList(), null); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
590 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
591 |
return new FixResult(Collections.singletonList(new Fix() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
592 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
593 |
public String displayName() { |
36990 | 594 |
return repl.messageFormat("jshell.console.create.variable"); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
595 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
596 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
597 |
public void perform(ConsoleReader in) throws IOException { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
598 |
in.redrawLine(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
599 |
in.setCursorPosition(0); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
600 |
in.putString(type + " = "); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
601 |
in.setCursorPosition(in.getCursorBuffer().cursor - 3); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
602 |
in.flush(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
603 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
604 |
}), null); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
605 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
606 |
}, |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
607 |
new FixComputer('i', true) { //compute "Add import" Fixes: |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
608 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
609 |
public FixResult compute(JShellTool repl, String code, int cursor) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
610 |
QualifiedNames res = repl.analysis.listQualifiedNames(code, cursor); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
611 |
List<Fix> fixes = new ArrayList<>(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
612 |
for (String fqn : res.getNames()) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
613 |
fixes.add(new Fix() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
614 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
615 |
public String displayName() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
616 |
return "import: " + fqn; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
617 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
618 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
619 |
public void perform(ConsoleReader in) throws IOException { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
620 |
repl.state.eval("import " + fqn + ";"); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
621 |
in.println("Imported: " + fqn); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
622 |
in.redrawLine(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
623 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
624 |
}); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
625 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
626 |
if (res.isResolvable()) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
627 |
return new FixResult(Collections.emptyList(), |
36990 | 628 |
repl.messageFormat("jshell.console.resolvable")); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
629 |
} else { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
630 |
String error = ""; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
631 |
if (fixes.isEmpty()) { |
36990 | 632 |
error = repl.messageFormat("jshell.console.no.candidate"); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
633 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
634 |
if (!res.isUpToDate()) { |
36990 | 635 |
error += repl.messageFormat("jshell.console.incomplete"); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
636 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
637 |
return new FixResult(fixes, error); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
638 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
639 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
640 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
641 |
}; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
642 |
|
33362 | 643 |
private static final class JShellUnixTerminal extends NoInterruptUnixTerminal { |
644 |
||
645 |
private final StopDetectingInputStream input; |
|
646 |
||
647 |
public JShellUnixTerminal(StopDetectingInputStream input) throws Exception { |
|
648 |
this.input = input; |
|
649 |
} |
|
650 |
||
651 |
public boolean isRaw() { |
|
652 |
try { |
|
653 |
return getSettings().get("-a").contains("-icanon"); |
|
654 |
} catch (IOException | InterruptedException ex) { |
|
655 |
return false; |
|
656 |
} |
|
657 |
} |
|
658 |
||
659 |
@Override |
|
660 |
public InputStream wrapInIfNeeded(InputStream in) throws IOException { |
|
661 |
return input.setInputStream(super.wrapInIfNeeded(in)); |
|
662 |
} |
|
663 |
||
664 |
@Override |
|
665 |
public void disableInterruptCharacter() { |
|
666 |
} |
|
667 |
||
668 |
@Override |
|
669 |
public void enableInterruptCharacter() { |
|
670 |
} |
|
671 |
||
672 |
} |
|
673 |
||
674 |
private static final class JShellWindowsTerminal extends WindowsTerminal { |
|
675 |
||
676 |
private final StopDetectingInputStream input; |
|
677 |
||
678 |
public JShellWindowsTerminal(StopDetectingInputStream input) throws Exception { |
|
679 |
this.input = input; |
|
680 |
} |
|
681 |
||
682 |
@Override |
|
683 |
public void init() throws Exception { |
|
684 |
super.init(); |
|
685 |
setAnsiSupported(false); |
|
686 |
} |
|
687 |
||
688 |
@Override |
|
689 |
public InputStream wrapInIfNeeded(InputStream in) throws IOException { |
|
690 |
return input.setInputStream(super.wrapInIfNeeded(in)); |
|
691 |
} |
|
692 |
||
693 |
} |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
694 |
|
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
695 |
private static final class TestTerminal extends TerminalSupport { |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
696 |
|
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
697 |
private final StopDetectingInputStream input; |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
698 |
|
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
699 |
public TestTerminal(StopDetectingInputStream input) throws Exception { |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
700 |
super(true); |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
701 |
setAnsiSupported(false); |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
702 |
setEchoEnabled(true); |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
703 |
this.input = input; |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
704 |
} |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
705 |
|
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
706 |
@Override |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
707 |
public InputStream wrapInIfNeeded(InputStream in) throws IOException { |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
708 |
return input.setInputStream(super.wrapInIfNeeded(in)); |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
709 |
} |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
710 |
|
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
711 |
} |
33362 | 712 |
} |