author | redestad |
Wed, 13 Dec 2017 21:25:49 +0100 | |
changeset 48294 | 2608240fc957 |
parent 48275 | b2190c70a1ac |
child 48939 | ba545e52b932 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
44459 | 2 |
* Copyright (c) 2015, 2017, 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.io.IOException; |
|
33 |
import java.io.InputStream; |
|
41628 | 34 |
import java.io.InterruptedIOException; |
33362 | 35 |
import java.io.PrintStream; |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
36 |
import java.util.ArrayList; |
41865 | 37 |
import java.util.Arrays; |
38521 | 38 |
import java.util.Collection; |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
39 |
import java.util.Collections; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
40 |
import java.util.HashMap; |
41865 | 41 |
import java.util.Iterator; |
33362 | 42 |
import java.util.List; |
43 |
import java.util.Locale; |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
44 |
import java.util.Map; |
33362 | 45 |
import java.util.Optional; |
41865 | 46 |
import java.util.function.Function; |
38521 | 47 |
import java.util.stream.Collectors; |
48 |
import java.util.stream.Stream; |
|
33362 | 49 |
|
41865 | 50 |
import jdk.internal.shellsupport.doc.JavadocFormatter; |
33362 | 51 |
import jdk.internal.jline.NoInterruptUnixTerminal; |
52 |
import jdk.internal.jline.Terminal; |
|
53 |
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
|
54 |
import jdk.internal.jline.TerminalSupport; |
33362 | 55 |
import jdk.internal.jline.WindowsTerminal; |
56 |
import jdk.internal.jline.console.ConsoleReader; |
|
57 |
import jdk.internal.jline.console.KeyMap; |
|
58 |
import jdk.internal.jline.console.UserInterruptException; |
|
40767 | 59 |
import jdk.internal.jline.console.history.History; |
60 |
import jdk.internal.jline.console.history.MemoryHistory; |
|
38521 | 61 |
import jdk.internal.jline.extra.EditingHistory; |
44459 | 62 |
import jdk.internal.jline.internal.NonBlockingInputStream; |
33362 | 63 |
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
|
64 |
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
|
65 |
import jdk.internal.misc.Signal.Handler; |
46185
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
66 |
import jdk.jshell.ExpressionSnippet; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
67 |
import jdk.jshell.Snippet; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
68 |
import jdk.jshell.Snippet.SubKind; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
69 |
import jdk.jshell.SourceCodeAnalysis.CompletionInfo; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
70 |
import jdk.jshell.VarSnippet; |
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; |
|
44188
3f2047e62102
8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents:
43773
diff
changeset
|
86 |
this.input = new StopDetectingInputStream(() -> repl.stop(), ex -> repl.hard("Error on input: %s", ex)); |
33362 | 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(); |
|
47050 | 96 |
CompletionState completionState = new CompletionState(); |
43363
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
97 |
in = new ConsoleReader(cmdin, cmdout, term) { |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
98 |
@Override public KeyMap getKeys() { |
47050 | 99 |
return new CheckCompletionKeyMap(super.getKeys(), completionState); |
44459 | 100 |
} |
101 |
@Override |
|
102 |
protected boolean complete() throws IOException { |
|
47050 | 103 |
return ConsoleIOContext.this.complete(completionState); |
43363
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
104 |
} |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
105 |
}; |
33362 | 106 |
in.setExpandEvents(false); |
107 |
in.setHandleUserInterrupt(true); |
|
38521 | 108 |
List<String> persistenHistory = Stream.of(repl.prefs.keys()) |
109 |
.filter(key -> key.startsWith(HISTORY_LINE_PREFIX)) |
|
110 |
.sorted() |
|
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
111 |
.map(key -> repl.prefs.get(key)) |
38521 | 112 |
.collect(Collectors.toList()); |
113 |
in.setHistory(history = new EditingHistory(in, persistenHistory) { |
|
114 |
@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
|
115 |
return repl.analysis.analyzeCompletion(input.toString()).completeness().isComplete(); |
33362 | 116 |
} |
117 |
}); |
|
118 |
in.setBellEnabled(true); |
|
38541
44e95493fd13
8131017: jshell tool: pasting code with tabs invokes tab completion
jlahoda
parents:
38521
diff
changeset
|
119 |
in.setCopyPasteDetection(true); |
44459 | 120 |
bind(FIXES_SHORTCUT, (Runnable) () -> fixes()); |
41631
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
121 |
try { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
122 |
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
|
123 |
@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
|
124 |
try { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
125 |
in.getTerminal().reset(); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
126 |
in.redrawLine(); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
127 |
in.flush(); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
128 |
} catch (Exception ex) { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
129 |
ex.printStackTrace(); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
130 |
} |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
131 |
} |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
132 |
}); |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
133 |
} catch (IllegalArgumentException ignored) { |
a348d1cc8d9d
8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents:
41628
diff
changeset
|
134 |
//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
|
135 |
} |
33362 | 136 |
} |
137 |
||
138 |
@Override |
|
139 |
public String readLine(String prompt, String prefix) throws IOException, InputInterruptedException { |
|
140 |
this.prefix = prefix; |
|
141 |
try { |
|
142 |
return in.readLine(prompt); |
|
143 |
} catch (UserInterruptException ex) { |
|
144 |
throw (InputInterruptedException) new InputInterruptedException().initCause(ex); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
@Override |
|
149 |
public boolean interactiveOutput() { |
|
150 |
return true; |
|
151 |
} |
|
152 |
||
153 |
@Override |
|
154 |
public Iterable<String> currentSessionHistory() { |
|
155 |
return history.currentSessionEntries(); |
|
156 |
} |
|
157 |
||
158 |
@Override |
|
159 |
public void close() throws IOException { |
|
38521 | 160 |
//save history: |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
161 |
for (String key : repl.prefs.keys()) { |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
162 |
if (key.startsWith(HISTORY_LINE_PREFIX)) { |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
163 |
repl.prefs.remove(key); |
38521 | 164 |
} |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
165 |
} |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
166 |
Collection<? extends String> savedHistory = history.save(); |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
167 |
if (!savedHistory.isEmpty()) { |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
168 |
int len = (int) Math.ceil(Math.log10(savedHistory.size()+1)); |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
169 |
String format = HISTORY_LINE_PREFIX + "%0" + len + "d"; |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
170 |
int index = 0; |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
171 |
for (String historyLine : savedHistory) { |
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
172 |
repl.prefs.put(String.format(format, index++), historyLine); |
38521 | 173 |
} |
174 |
} |
|
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
175 |
repl.prefs.flush(); |
33362 | 176 |
in.shutdown(); |
177 |
try { |
|
178 |
in.getTerminal().restore(); |
|
179 |
} catch (Exception ex) { |
|
180 |
throw new IOException(ex); |
|
181 |
} |
|
38909
80e42e2d475b
8158174: jshell tool: leaks threads waiting on StopDetectingInputStream
jlahoda
parents:
38908
diff
changeset
|
182 |
input.shutdown(); |
33362 | 183 |
} |
184 |
||
185 |
private void bind(String shortcut, Object action) { |
|
186 |
KeyMap km = in.getKeys(); |
|
187 |
for (int i = 0; i < shortcut.length(); i++) { |
|
188 |
Object value = km.getBound(Character.toString(shortcut.charAt(i))); |
|
189 |
if (value instanceof KeyMap) { |
|
190 |
km = (KeyMap) value; |
|
191 |
} else { |
|
192 |
km.bind(shortcut.substring(i), action); |
|
193 |
} |
|
194 |
} |
|
195 |
} |
|
196 |
||
44459 | 197 |
private static final String FIXES_SHORTCUT = "\033\133\132"; //Shift-TAB |
33362 | 198 |
|
44459 | 199 |
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
200 |
private static final String LINE_SEPARATORS2 = LINE_SEPARATOR + LINE_SEPARATOR; |
|
41865 | 201 |
|
44459 | 202 |
@SuppressWarnings("fallthrough") |
47050 | 203 |
private boolean complete(CompletionState completionState) { |
44459 | 204 |
//The completion has multiple states (invoked by subsequent presses of <tab>). |
205 |
//On the first invocation in a given sequence, all steps are precomputed |
|
47050 | 206 |
//and placed into the todo list (completionState.todo). The todo list is |
207 |
//then followed on both the first and subsequent completion invocations: |
|
33362 | 208 |
try { |
44459 | 209 |
String text = in.getCursorBuffer().toString(); |
210 |
int cursor = in.getCursorBuffer().cursor; |
|
47050 | 211 |
|
212 |
List<CompletionTask> todo = completionState.todo; |
|
213 |
||
214 |
if (todo.isEmpty() || completionState.actionCount != 1) { |
|
215 |
ConsoleIOContextTestSupport.willComputeCompletion(); |
|
44459 | 216 |
int[] anchor = new int[] {-1}; |
217 |
List<Suggestion> suggestions; |
|
218 |
List<String> doc; |
|
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
47840
diff
changeset
|
219 |
boolean command = prefix.isEmpty() && text.startsWith("/"); |
44459 | 220 |
if (command) { |
221 |
suggestions = repl.commandCompletionSuggestions(text, cursor, anchor); |
|
222 |
doc = repl.commandDocumentation(text, cursor, true); |
|
41865 | 223 |
} else { |
44459 | 224 |
int prefixLength = prefix.length(); |
225 |
suggestions = repl.analysis.completionSuggestions(prefix + text, cursor + prefixLength, anchor); |
|
226 |
anchor[0] -= prefixLength; |
|
227 |
doc = repl.analysis.documentation(prefix + text, cursor + prefix.length(), false) |
|
228 |
.stream() |
|
229 |
.map(Documentation::signature) |
|
230 |
.collect(Collectors.toList()); |
|
231 |
} |
|
232 |
long smartCount = suggestions.stream().filter(Suggestion::matchesType).count(); |
|
233 |
boolean hasSmart = smartCount > 0 && smartCount <= in.getAutoprintThreshold(); |
|
234 |
boolean hasBoth = hasSmart && |
|
235 |
suggestions.stream() |
|
236 |
.map(s -> s.matchesType()) |
|
237 |
.distinct() |
|
238 |
.count() == 2; |
|
239 |
boolean tooManyItems = suggestions.size() > in.getAutoprintThreshold(); |
|
47484
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
240 |
CompletionTask ordinaryCompletion = |
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
241 |
new OrdinaryCompletionTask(suggestions, |
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
242 |
anchor[0], |
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
243 |
!command && !doc.isEmpty(), |
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
244 |
hasBoth); |
44459 | 245 |
CompletionTask allCompletion = new AllSuggestionsCompletionTask(suggestions, anchor[0]); |
41865 | 246 |
|
47050 | 247 |
todo = new ArrayList<>(); |
248 |
||
44459 | 249 |
//the main decission tree: |
250 |
if (command) { |
|
251 |
CompletionTask shortDocumentation = new CommandSynopsisTask(doc); |
|
252 |
CompletionTask fullDocumentation = new CommandFullDocumentationTask(todo); |
|
41865 | 253 |
|
44459 | 254 |
if (!doc.isEmpty()) { |
255 |
if (tooManyItems) { |
|
256 |
todo.add(new NoopCompletionTask()); |
|
257 |
todo.add(allCompletion); |
|
258 |
} else { |
|
259 |
todo.add(ordinaryCompletion); |
|
41865 | 260 |
} |
44459 | 261 |
todo.add(shortDocumentation); |
262 |
todo.add(fullDocumentation); |
|
263 |
} else { |
|
264 |
todo.add(new NoSuchCommandCompletionTask()); |
|
265 |
} |
|
266 |
} else { |
|
267 |
if (doc.isEmpty()) { |
|
268 |
if (hasSmart) { |
|
269 |
todo.add(ordinaryCompletion); |
|
270 |
} else if (tooManyItems) { |
|
271 |
todo.add(new NoopCompletionTask()); |
|
272 |
} |
|
273 |
if (!hasSmart || hasBoth) { |
|
274 |
todo.add(allCompletion); |
|
275 |
} |
|
276 |
} else { |
|
277 |
CompletionTask shortDocumentation = new ExpressionSignaturesTask(doc); |
|
278 |
CompletionTask fullDocumentation = new ExpressionJavadocTask(todo); |
|
41865 | 279 |
|
44459 | 280 |
if (hasSmart) { |
281 |
todo.add(ordinaryCompletion); |
|
282 |
} |
|
283 |
todo.add(shortDocumentation); |
|
284 |
if (!hasSmart || hasBoth) { |
|
285 |
todo.add(allCompletion); |
|
286 |
} |
|
287 |
if (tooManyItems) { |
|
288 |
todo.add(todo.size() - 1, fullDocumentation); |
|
289 |
} else { |
|
290 |
todo.add(fullDocumentation); |
|
41865 | 291 |
} |
292 |
} |
|
293 |
} |
|
33362 | 294 |
} |
44459 | 295 |
|
296 |
boolean success = false; |
|
297 |
boolean repaint = true; |
|
298 |
||
299 |
OUTER: while (!todo.isEmpty()) { |
|
300 |
CompletionTask.Result result = todo.remove(0).perform(text, cursor); |
|
301 |
||
302 |
switch (result) { |
|
303 |
case CONTINUE: |
|
304 |
break; |
|
305 |
case SKIP_NOREPAINT: |
|
306 |
repaint = false; |
|
307 |
case SKIP: |
|
308 |
todo.clear(); |
|
309 |
//intentional fall-through |
|
310 |
case FINISH: |
|
311 |
success = true; |
|
312 |
//intentional fall-through |
|
313 |
case NO_DATA: |
|
314 |
if (!todo.isEmpty()) { |
|
315 |
in.println(); |
|
316 |
in.println(todo.get(0).description()); |
|
317 |
} |
|
318 |
break OUTER; |
|
319 |
} |
|
320 |
} |
|
321 |
||
47050 | 322 |
completionState.actionCount = 0; |
323 |
completionState.todo = todo; |
|
324 |
||
44459 | 325 |
if (repaint) { |
326 |
in.redrawLine(); |
|
327 |
in.flush(); |
|
328 |
} |
|
329 |
||
330 |
return success; |
|
33362 | 331 |
} catch (IOException ex) { |
332 |
throw new IllegalStateException(ex); |
|
333 |
} |
|
334 |
} |
|
335 |
||
44459 | 336 |
private CompletionTask.Result doPrintFullDocumentation(List<CompletionTask> todo, List<String> doc, boolean command) { |
337 |
if (doc != null && !doc.isEmpty()) { |
|
338 |
Terminal term = in.getTerminal(); |
|
339 |
int pageHeight = term.getHeight() - NEEDED_LINES; |
|
340 |
List<CompletionTask> thisTODO = new ArrayList<>(); |
|
341 |
||
342 |
for (Iterator<String> docIt = doc.iterator(); docIt.hasNext(); ) { |
|
343 |
String currentDoc = docIt.next(); |
|
344 |
String[] lines = currentDoc.split("\n"); |
|
345 |
int firstLine = 0; |
|
346 |
||
347 |
while (firstLine < lines.length) { |
|
348 |
boolean first = firstLine == 0; |
|
349 |
String[] thisPageLines = |
|
350 |
Arrays.copyOfRange(lines, |
|
351 |
firstLine, |
|
352 |
Math.min(firstLine + pageHeight, lines.length)); |
|
353 |
||
354 |
thisTODO.add(new CompletionTask() { |
|
355 |
@Override |
|
356 |
public String description() { |
|
357 |
String key = !first ? "jshell.console.see.next.page" |
|
358 |
: command ? "jshell.console.see.next.command.doc" |
|
359 |
: "jshell.console.see.next.javadoc"; |
|
360 |
||
361 |
return repl.getResourceString(key); |
|
362 |
} |
|
363 |
||
364 |
@Override |
|
365 |
public Result perform(String text, int cursor) throws IOException { |
|
366 |
in.println(); |
|
367 |
for (String line : thisPageLines) { |
|
368 |
in.println(line); |
|
369 |
} |
|
370 |
return Result.FINISH; |
|
371 |
} |
|
372 |
}); |
|
373 |
||
374 |
firstLine += pageHeight; |
|
375 |
} |
|
376 |
} |
|
377 |
||
378 |
todo.addAll(0, thisTODO); |
|
379 |
||
380 |
return CompletionTask.Result.CONTINUE; |
|
381 |
} |
|
382 |
||
383 |
return CompletionTask.Result.FINISH; |
|
384 |
} |
|
385 |
//where: |
|
386 |
private static final int NEEDED_LINES = 4; |
|
387 |
||
33362 | 388 |
private static String commonPrefix(String str1, String str2) { |
389 |
for (int i = 0; i < str2.length(); i++) { |
|
390 |
if (!str1.startsWith(str2.substring(0, i + 1))) { |
|
391 |
return str2.substring(0, i); |
|
392 |
} |
|
393 |
} |
|
394 |
||
395 |
return str2; |
|
396 |
} |
|
397 |
||
44459 | 398 |
private interface CompletionTask { |
399 |
public String description(); |
|
400 |
public Result perform(String text, int cursor) throws IOException; |
|
401 |
||
402 |
enum Result { |
|
403 |
NO_DATA, |
|
404 |
CONTINUE, |
|
405 |
FINISH, |
|
406 |
SKIP, |
|
407 |
SKIP_NOREPAINT; |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
private final class NoopCompletionTask implements CompletionTask { |
|
412 |
||
413 |
@Override |
|
414 |
public String description() { |
|
415 |
throw new UnsupportedOperationException("Should not get here."); |
|
416 |
} |
|
417 |
||
418 |
@Override |
|
419 |
public Result perform(String text, int cursor) throws IOException { |
|
420 |
return Result.FINISH; |
|
421 |
} |
|
422 |
||
423 |
} |
|
424 |
||
425 |
private final class NoSuchCommandCompletionTask implements CompletionTask { |
|
426 |
||
427 |
@Override |
|
428 |
public String description() { |
|
429 |
throw new UnsupportedOperationException("Should not get here."); |
|
430 |
} |
|
431 |
||
432 |
@Override |
|
433 |
public Result perform(String text, int cursor) throws IOException { |
|
434 |
in.println(); |
|
435 |
in.println(repl.getResourceString("jshell.console.no.such.command")); |
|
436 |
in.println(); |
|
437 |
return Result.SKIP; |
|
438 |
} |
|
439 |
||
440 |
} |
|
441 |
||
442 |
private final class OrdinaryCompletionTask implements CompletionTask { |
|
443 |
private final List<Suggestion> suggestions; |
|
444 |
private final int anchor; |
|
445 |
private final boolean cont; |
|
47484
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
446 |
private final boolean showSmart; |
44459 | 447 |
|
448 |
public OrdinaryCompletionTask(List<Suggestion> suggestions, |
|
449 |
int anchor, |
|
450 |
boolean cont, |
|
47484
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
451 |
boolean showSmart) { |
44459 | 452 |
this.suggestions = suggestions; |
453 |
this.anchor = anchor; |
|
454 |
this.cont = cont; |
|
47484
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
455 |
this.showSmart = showSmart; |
44459 | 456 |
} |
457 |
||
458 |
@Override |
|
459 |
public String description() { |
|
460 |
throw new UnsupportedOperationException("Should not get here."); |
|
461 |
} |
|
462 |
||
463 |
@Override |
|
464 |
public Result perform(String text, int cursor) throws IOException { |
|
465 |
List<CharSequence> toShow; |
|
466 |
||
47484
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
467 |
if (showSmart) { |
44459 | 468 |
toShow = |
469 |
suggestions.stream() |
|
470 |
.filter(Suggestion::matchesType) |
|
471 |
.map(Suggestion::continuation) |
|
472 |
.distinct() |
|
473 |
.collect(Collectors.toList()); |
|
474 |
} else { |
|
475 |
toShow = |
|
476 |
suggestions.stream() |
|
477 |
.map(Suggestion::continuation) |
|
478 |
.distinct() |
|
479 |
.collect(Collectors.toList()); |
|
480 |
} |
|
481 |
||
482 |
if (toShow.isEmpty()) { |
|
483 |
return Result.CONTINUE; |
|
484 |
} |
|
485 |
||
486 |
Optional<String> prefix = |
|
487 |
suggestions.stream() |
|
488 |
.map(Suggestion::continuation) |
|
489 |
.reduce(ConsoleIOContext::commonPrefix); |
|
490 |
||
491 |
String prefixStr = prefix.orElse("").substring(cursor - anchor); |
|
492 |
in.putString(prefixStr); |
|
493 |
||
47484
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
494 |
boolean showItems = toShow.size() > 1 || showSmart; |
44459 | 495 |
|
496 |
if (showItems) { |
|
497 |
in.println(); |
|
498 |
in.printColumns(toShow); |
|
499 |
} |
|
500 |
||
501 |
if (!prefixStr.isEmpty()) |
|
47484
d0cb66d8cbf1
8189595: jshell tool: line redrawn on each completion
jlahoda
parents:
47216
diff
changeset
|
502 |
return showItems ? Result.FINISH : Result.SKIP_NOREPAINT; |
44459 | 503 |
|
504 |
return cont ? Result.CONTINUE : Result.FINISH; |
|
505 |
} |
|
506 |
||
507 |
} |
|
508 |
||
509 |
private final class AllSuggestionsCompletionTask implements CompletionTask { |
|
510 |
private final List<Suggestion> suggestions; |
|
511 |
private final int anchor; |
|
512 |
||
513 |
public AllSuggestionsCompletionTask(List<Suggestion> suggestions, |
|
514 |
int anchor) { |
|
515 |
this.suggestions = suggestions; |
|
516 |
this.anchor = anchor; |
|
517 |
} |
|
518 |
||
519 |
@Override |
|
520 |
public String description() { |
|
521 |
if (suggestions.size() <= in.getAutoprintThreshold()) { |
|
522 |
return repl.getResourceString("jshell.console.completion.all.completions"); |
|
523 |
} else { |
|
524 |
return repl.messageFormat("jshell.console.completion.all.completions.number", suggestions.size()); |
|
525 |
} |
|
526 |
} |
|
527 |
||
528 |
@Override |
|
529 |
public Result perform(String text, int cursor) throws IOException { |
|
530 |
List<String> candidates = |
|
531 |
suggestions.stream() |
|
532 |
.map(Suggestion::continuation) |
|
533 |
.distinct() |
|
534 |
.collect(Collectors.toList()); |
|
535 |
||
536 |
Optional<String> prefix = |
|
537 |
candidates.stream() |
|
538 |
.reduce(ConsoleIOContext::commonPrefix); |
|
539 |
||
540 |
String prefixStr = prefix.map(str -> str.substring(cursor - anchor)).orElse(""); |
|
541 |
in.putString(prefixStr); |
|
542 |
if (candidates.size() > 1) { |
|
543 |
in.println(); |
|
544 |
in.printColumns(candidates); |
|
545 |
} |
|
546 |
return suggestions.isEmpty() ? Result.NO_DATA : Result.FINISH; |
|
547 |
} |
|
548 |
||
549 |
} |
|
550 |
||
551 |
private final class CommandSynopsisTask implements CompletionTask { |
|
552 |
||
553 |
private final List<String> synopsis; |
|
554 |
||
555 |
public CommandSynopsisTask(List<String> synposis) { |
|
556 |
this.synopsis = synposis; |
|
557 |
} |
|
558 |
||
559 |
@Override |
|
560 |
public String description() { |
|
561 |
return repl.getResourceString("jshell.console.see.synopsis"); |
|
562 |
} |
|
563 |
||
564 |
@Override |
|
565 |
public Result perform(String text, int cursor) throws IOException { |
|
566 |
try { |
|
567 |
in.println(); |
|
568 |
in.println(synopsis.stream() |
|
569 |
.map(l -> l.replaceAll("\n", LINE_SEPARATOR)) |
|
570 |
.collect(Collectors.joining(LINE_SEPARATORS2))); |
|
571 |
} catch (IOException ex) { |
|
572 |
throw new IllegalStateException(ex); |
|
573 |
} |
|
574 |
return Result.FINISH; |
|
575 |
} |
|
576 |
||
577 |
} |
|
578 |
||
579 |
private final class CommandFullDocumentationTask implements CompletionTask { |
|
580 |
||
581 |
private final List<CompletionTask> todo; |
|
582 |
||
583 |
public CommandFullDocumentationTask(List<CompletionTask> todo) { |
|
584 |
this.todo = todo; |
|
585 |
} |
|
586 |
||
587 |
@Override |
|
588 |
public String description() { |
|
589 |
return repl.getResourceString("jshell.console.see.full.documentation"); |
|
590 |
} |
|
591 |
||
592 |
@Override |
|
593 |
public Result perform(String text, int cursor) throws IOException { |
|
594 |
List<String> fullDoc = repl.commandDocumentation(text, cursor, false); |
|
595 |
return doPrintFullDocumentation(todo, fullDoc, true); |
|
596 |
} |
|
597 |
||
598 |
} |
|
599 |
||
600 |
private final class ExpressionSignaturesTask implements CompletionTask { |
|
601 |
||
602 |
private final List<String> doc; |
|
603 |
||
604 |
public ExpressionSignaturesTask(List<String> doc) { |
|
605 |
this.doc = doc; |
|
606 |
} |
|
607 |
||
608 |
@Override |
|
609 |
public String description() { |
|
610 |
throw new UnsupportedOperationException("Should not get here."); |
|
611 |
} |
|
612 |
||
613 |
@Override |
|
614 |
public Result perform(String text, int cursor) throws IOException { |
|
615 |
in.println(); |
|
616 |
in.println(repl.getResourceString("jshell.console.completion.current.signatures")); |
|
617 |
in.println(doc.stream().collect(Collectors.joining(LINE_SEPARATOR))); |
|
618 |
return Result.FINISH; |
|
619 |
} |
|
620 |
||
621 |
} |
|
622 |
||
623 |
private final class ExpressionJavadocTask implements CompletionTask { |
|
624 |
||
625 |
private final List<CompletionTask> todo; |
|
626 |
||
627 |
public ExpressionJavadocTask(List<CompletionTask> todo) { |
|
628 |
this.todo = todo; |
|
629 |
} |
|
630 |
||
631 |
@Override |
|
632 |
public String description() { |
|
633 |
return repl.getResourceString("jshell.console.see.documentation"); |
|
634 |
} |
|
635 |
||
636 |
@Override |
|
637 |
public Result perform(String text, int cursor) throws IOException { |
|
638 |
//schedule showing javadoc: |
|
639 |
Terminal term = in.getTerminal(); |
|
640 |
JavadocFormatter formatter = new JavadocFormatter(term.getWidth(), |
|
641 |
term.isAnsiSupported()); |
|
642 |
Function<Documentation, String> convertor = d -> formatter.formatJavadoc(d.signature(), d.javadoc()) + |
|
643 |
(d.javadoc() == null ? repl.messageFormat("jshell.console.no.javadoc") |
|
644 |
: ""); |
|
645 |
List<String> doc = repl.analysis.documentation(prefix + text, cursor + prefix.length(), true) |
|
646 |
.stream() |
|
647 |
.map(convertor) |
|
648 |
.collect(Collectors.toList()); |
|
649 |
return doPrintFullDocumentation(todo, doc, false); |
|
650 |
} |
|
651 |
||
652 |
} |
|
653 |
||
33362 | 654 |
@Override |
655 |
public boolean terminalEditorRunning() { |
|
656 |
Terminal terminal = in.getTerminal(); |
|
43583
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
657 |
if (terminal instanceof SuspendableTerminal) |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
658 |
return ((SuspendableTerminal) terminal).isRaw(); |
33362 | 659 |
return false; |
660 |
} |
|
661 |
||
662 |
@Override |
|
663 |
public void suspend() { |
|
43583
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
664 |
Terminal terminal = in.getTerminal(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
665 |
if (terminal instanceof SuspendableTerminal) |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
666 |
((SuspendableTerminal) terminal).suspend(); |
33362 | 667 |
} |
668 |
||
669 |
@Override |
|
670 |
public void resume() { |
|
43583
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
671 |
Terminal terminal = in.getTerminal(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
672 |
if (terminal instanceof SuspendableTerminal) |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
673 |
((SuspendableTerminal) terminal).resume(); |
33362 | 674 |
} |
675 |
||
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
676 |
@Override |
33362 | 677 |
public void beforeUserCode() { |
40767 | 678 |
synchronized (this) { |
679 |
inputBytes = null; |
|
680 |
} |
|
33362 | 681 |
input.setState(State.BUFFER); |
682 |
} |
|
683 |
||
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
684 |
@Override |
33362 | 685 |
public void afterUserCode() { |
686 |
input.setState(State.WAIT); |
|
687 |
} |
|
688 |
||
689 |
@Override |
|
690 |
public void replaceLastHistoryEntry(String source) { |
|
691 |
history.fullHistoryReplace(source); |
|
692 |
} |
|
693 |
||
44459 | 694 |
private static final long ESCAPE_TIMEOUT = 100; |
695 |
||
696 |
private void fixes() { |
|
697 |
try { |
|
698 |
int c = in.readCharacter(); |
|
699 |
||
700 |
if (c == (-1)) { |
|
701 |
return ; |
|
702 |
} |
|
703 |
||
704 |
for (FixComputer computer : FIX_COMPUTERS) { |
|
705 |
if (computer.shortcut == c) { |
|
706 |
fixes(computer); |
|
707 |
return ; |
|
708 |
} |
|
709 |
} |
|
710 |
||
711 |
readOutRemainingEscape(c); |
|
712 |
||
713 |
in.beep(); |
|
714 |
in.println(); |
|
715 |
in.println(repl.getResourceString("jshell.fix.wrong.shortcut")); |
|
716 |
in.redrawLine(); |
|
717 |
in.flush(); |
|
718 |
} catch (IOException ex) { |
|
719 |
ex.printStackTrace(); |
|
720 |
} |
|
721 |
} |
|
722 |
||
723 |
private void readOutRemainingEscape(int c) throws IOException { |
|
724 |
if (c == '\033') { |
|
725 |
//escape, consume waiting input: |
|
726 |
InputStream inp = in.getInput(); |
|
727 |
||
728 |
if (inp instanceof NonBlockingInputStream) { |
|
729 |
NonBlockingInputStream nbis = (NonBlockingInputStream) inp; |
|
730 |
||
731 |
while (nbis.isNonBlockingEnabled() && nbis.peek(ESCAPE_TIMEOUT) > 0) { |
|
732 |
in.readCharacter(); |
|
733 |
} |
|
734 |
} |
|
735 |
} |
|
736 |
} |
|
737 |
||
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
738 |
//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
|
739 |
//and perform the selected one: |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
740 |
private void fixes(FixComputer computer) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
741 |
String input = prefix + in.getCursorBuffer().toString(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
742 |
int cursor = prefix.length() + in.getCursorBuffer().cursor; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
743 |
FixResult candidates = computer.compute(repl, input, cursor); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
744 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
745 |
try { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
746 |
final boolean printError = candidates.error != null && !candidates.error.isEmpty(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
747 |
if (printError) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
748 |
in.println(candidates.error); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
749 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
750 |
if (candidates.fixes.isEmpty()) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
751 |
in.beep(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
752 |
if (printError) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
753 |
in.redrawLine(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
754 |
in.flush(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
755 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
756 |
} else if (candidates.fixes.size() == 1 && !computer.showMenu) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
757 |
if (printError) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
758 |
in.redrawLine(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
759 |
in.flush(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
760 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
761 |
candidates.fixes.get(0).perform(in); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
762 |
} else { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
763 |
List<Fix> fixes = new ArrayList<>(candidates.fixes); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
764 |
fixes.add(0, new Fix() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
765 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
766 |
public String displayName() { |
36990 | 767 |
return repl.messageFormat("jshell.console.do.nothing"); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
768 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
769 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
770 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
771 |
public void perform(ConsoleReader in) throws IOException { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
772 |
in.redrawLine(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
773 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
774 |
}); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
775 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
776 |
Map<Character, Fix> char2Fix = new HashMap<>(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
777 |
in.println(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
778 |
for (int i = 0; i < fixes.size(); i++) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
779 |
Fix fix = fixes.get(i); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
780 |
char2Fix.put((char) ('0' + i), fix); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
781 |
in.println("" + i + ": " + fixes.get(i).displayName()); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
782 |
} |
36990 | 783 |
in.print(repl.messageFormat("jshell.console.choice")); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
784 |
in.flush(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
785 |
int read; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
786 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
787 |
read = in.readCharacter(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
788 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
789 |
Fix fix = char2Fix.get((char) read); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
790 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
791 |
if (fix == null) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
792 |
in.beep(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
793 |
fix = fixes.get(0); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
794 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
795 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
796 |
in.println(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
797 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
798 |
fix.perform(in); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
799 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
800 |
in.flush(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
801 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
802 |
} catch (IOException ex) { |
44459 | 803 |
throw new IllegalStateException(ex); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
804 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
805 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
806 |
|
40767 | 807 |
private byte[] inputBytes; |
808 |
private int inputBytesPointer; |
|
809 |
||
810 |
@Override |
|
41628 | 811 |
public synchronized int readUserInput() throws IOException { |
40767 | 812 |
while (inputBytes == null || inputBytes.length <= inputBytesPointer) { |
813 |
boolean prevHandleUserInterrupt = in.getHandleUserInterrupt(); |
|
814 |
History prevHistory = in.getHistory(); |
|
815 |
||
816 |
try { |
|
817 |
input.setState(State.WAIT); |
|
818 |
in.setHandleUserInterrupt(true); |
|
819 |
in.setHistory(userInputHistory); |
|
820 |
inputBytes = (in.readLine("") + System.getProperty("line.separator")).getBytes(); |
|
821 |
inputBytesPointer = 0; |
|
822 |
} catch (UserInterruptException ex) { |
|
41628 | 823 |
throw new InterruptedIOException(); |
40767 | 824 |
} finally { |
825 |
in.setHistory(prevHistory); |
|
826 |
in.setHandleUserInterrupt(prevHandleUserInterrupt); |
|
827 |
input.setState(State.BUFFER); |
|
828 |
} |
|
829 |
} |
|
830 |
return inputBytes[inputBytesPointer++]; |
|
831 |
} |
|
832 |
||
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
833 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
834 |
* 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
|
835 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
836 |
public interface Fix { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
837 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
838 |
* A name that should be shown to the user. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
839 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
840 |
public String displayName(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
841 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
842 |
* Perform the given action. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
843 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
844 |
public void perform(ConsoleReader in) throws IOException; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
845 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
846 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
847 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
848 |
* A factory for {@link Fix}es. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
849 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
850 |
public abstract static class FixComputer { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
851 |
private final char shortcut; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
852 |
private final boolean showMenu; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
853 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
854 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
855 |
* 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
|
856 |
* 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
|
857 |
* 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
|
858 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
859 |
public FixComputer(char shortcut, boolean showMenu) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
860 |
this.shortcut = shortcut; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
861 |
this.showMenu = showMenu; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
862 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
863 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
864 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
865 |
* Compute possible actions for the given code. |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
866 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
867 |
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
|
868 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
869 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
870 |
/** |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
871 |
* 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
|
872 |
*/ |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
873 |
public static class FixResult { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
874 |
public final List<Fix> fixes; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
875 |
public final String error; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
876 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
877 |
public FixResult(List<Fix> fixes, String error) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
878 |
this.fixes = fixes; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
879 |
this.error = error; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
880 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
881 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
882 |
|
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
883 |
private static final FixComputer[] FIX_COMPUTERS = new FixComputer[] { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
884 |
new FixComputer('v', false) { //compute "Introduce variable" Fix: |
41996
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
885 |
private void performToVar(ConsoleReader in, String type) throws IOException { |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
886 |
in.redrawLine(); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
887 |
in.setCursorPosition(0); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
888 |
in.putString(type + " = "); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
889 |
in.setCursorPosition(in.getCursorBuffer().cursor - 3); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
890 |
in.flush(); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
891 |
} |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
892 |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
893 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
894 |
public FixResult compute(JShellTool repl, String code, int cursor) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
895 |
String type = repl.analysis.analyzeType(code, cursor); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
896 |
if (type == null) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
897 |
return new FixResult(Collections.emptyList(), null); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
898 |
} |
41996
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
899 |
List<Fix> fixes = new ArrayList<>(); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
900 |
fixes.add(new Fix() { |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
901 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
902 |
public String displayName() { |
36990 | 903 |
return repl.messageFormat("jshell.console.create.variable"); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
904 |
} |
41996
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
905 |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
906 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
907 |
public void perform(ConsoleReader in) throws IOException { |
41996
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
908 |
performToVar(in, type); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
909 |
} |
41996
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
910 |
}); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
911 |
int idx = type.lastIndexOf("."); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
912 |
if (idx > 0) { |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
913 |
String stype = type.substring(idx + 1); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
914 |
QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length()); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
915 |
if (res.isUpToDate() && res.getNames().contains(type) |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
916 |
&& !res.isResolvable()) { |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
917 |
fixes.add(new Fix() { |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
918 |
@Override |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
919 |
public String displayName() { |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
920 |
return "import: " + type + ". " + |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
921 |
repl.messageFormat("jshell.console.create.variable"); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
922 |
} |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
923 |
|
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
924 |
@Override |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
925 |
public void perform(ConsoleReader in) throws IOException { |
47840 | 926 |
repl.processSource("import " + type + ";"); |
41996
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
927 |
in.println("Imported: " + type); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
928 |
performToVar(in, stype); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
929 |
} |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
930 |
}); |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
931 |
} |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
932 |
} |
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
933 |
return new FixResult(fixes, null); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
934 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
935 |
}, |
46185
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
936 |
new FixComputer('m', false) { //compute "Introduce method" Fix: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
937 |
private void performToMethod(ConsoleReader in, String type, String code) throws IOException { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
938 |
in.redrawLine(); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
939 |
if (!code.trim().endsWith(";")) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
940 |
in.putString(";"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
941 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
942 |
in.putString(" }"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
943 |
in.setCursorPosition(0); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
944 |
String afterCursor = type.equals("void") |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
945 |
? "() { " |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
946 |
: "() { return "; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
947 |
in.putString(type + " " + afterCursor); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
948 |
// position the cursor where the method name should be entered (before parens) |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
949 |
in.setCursorPosition(in.getCursorBuffer().cursor - afterCursor.length()); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
950 |
in.flush(); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
951 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
952 |
|
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
953 |
private FixResult reject(JShellTool repl, String messageKey) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
954 |
return new FixResult(Collections.emptyList(), repl.messageFormat(messageKey)); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
955 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
956 |
|
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
957 |
@Override |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
958 |
public FixResult compute(JShellTool repl, String code, int cursor) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
959 |
final String codeToCursor = code.substring(0, cursor); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
960 |
final String type; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
961 |
final CompletionInfo ci = repl.analysis.analyzeCompletion(codeToCursor); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
962 |
if (!ci.remaining().isEmpty()) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
963 |
return reject(repl, "jshell.console.exprstmt"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
964 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
965 |
switch (ci.completeness()) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
966 |
case COMPLETE: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
967 |
case COMPLETE_WITH_SEMI: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
968 |
case CONSIDERED_INCOMPLETE: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
969 |
break; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
970 |
case EMPTY: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
971 |
return reject(repl, "jshell.console.empty"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
972 |
case DEFINITELY_INCOMPLETE: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
973 |
case UNKNOWN: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
974 |
default: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
975 |
return reject(repl, "jshell.console.erroneous"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
976 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
977 |
List<Snippet> snl = repl.analysis.sourceToSnippets(ci.source()); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
978 |
if (snl.size() != 1) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
979 |
return reject(repl, "jshell.console.erroneous"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
980 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
981 |
Snippet sn = snl.get(0); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
982 |
switch (sn.kind()) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
983 |
case EXPRESSION: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
984 |
type = ((ExpressionSnippet) sn).typeName(); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
985 |
break; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
986 |
case STATEMENT: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
987 |
type = "void"; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
988 |
break; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
989 |
case VAR: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
990 |
if (sn.subKind() != SubKind.TEMP_VAR_EXPRESSION_SUBKIND) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
991 |
// only valid var is an expression turned into a temp var |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
992 |
return reject(repl, "jshell.console.exprstmt"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
993 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
994 |
type = ((VarSnippet) sn).typeName(); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
995 |
break; |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
996 |
case IMPORT: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
997 |
case METHOD: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
998 |
case TYPE_DECL: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
999 |
return reject(repl, "jshell.console.exprstmt"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1000 |
case ERRONEOUS: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1001 |
default: |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1002 |
return reject(repl, "jshell.console.erroneous"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1003 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1004 |
List<Fix> fixes = new ArrayList<>(); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1005 |
fixes.add(new Fix() { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1006 |
@Override |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1007 |
public String displayName() { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1008 |
return repl.messageFormat("jshell.console.create.method"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1009 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1010 |
|
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1011 |
@Override |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1012 |
public void perform(ConsoleReader in) throws IOException { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1013 |
performToMethod(in, type, codeToCursor); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1014 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1015 |
}); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1016 |
int idx = type.lastIndexOf("."); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1017 |
if (idx > 0) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1018 |
String stype = type.substring(idx + 1); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1019 |
QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length()); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1020 |
if (res.isUpToDate() && res.getNames().contains(type) |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1021 |
&& !res.isResolvable()) { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1022 |
fixes.add(new Fix() { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1023 |
@Override |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1024 |
public String displayName() { |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1025 |
return "import: " + type + ". " + |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1026 |
repl.messageFormat("jshell.console.create.method"); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1027 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1028 |
|
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1029 |
@Override |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1030 |
public void perform(ConsoleReader in) throws IOException { |
47840 | 1031 |
repl.processSource("import " + type + ";"); |
46185
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1032 |
in.println("Imported: " + type); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1033 |
performToMethod(in, stype, codeToCursor); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1034 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1035 |
}); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1036 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1037 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1038 |
return new FixResult(fixes, null); |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1039 |
} |
f4c981fc7818
8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents:
45501
diff
changeset
|
1040 |
}, |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1041 |
new FixComputer('i', true) { //compute "Add import" Fixes: |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1042 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1043 |
public FixResult compute(JShellTool repl, String code, int cursor) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1044 |
QualifiedNames res = repl.analysis.listQualifiedNames(code, cursor); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1045 |
List<Fix> fixes = new ArrayList<>(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1046 |
for (String fqn : res.getNames()) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1047 |
fixes.add(new Fix() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1048 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1049 |
public String displayName() { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1050 |
return "import: " + fqn; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1051 |
} |
41996
389212e0746c
8166333: jshell tool: shortcut var does not import its type
rfield
parents:
41934
diff
changeset
|
1052 |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1053 |
@Override |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1054 |
public void perform(ConsoleReader in) throws IOException { |
47840 | 1055 |
repl.processSource("import " + fqn + ";"); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1056 |
in.println("Imported: " + fqn); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1057 |
in.redrawLine(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1058 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1059 |
}); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1060 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1061 |
if (res.isResolvable()) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1062 |
return new FixResult(Collections.emptyList(), |
36990 | 1063 |
repl.messageFormat("jshell.console.resolvable")); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1064 |
} else { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1065 |
String error = ""; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1066 |
if (fixes.isEmpty()) { |
36990 | 1067 |
error = repl.messageFormat("jshell.console.no.candidate"); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1068 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1069 |
if (!res.isUpToDate()) { |
36990 | 1070 |
error += repl.messageFormat("jshell.console.incomplete"); |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1071 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1072 |
return new FixResult(fixes, error); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1073 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1074 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1075 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1076 |
}; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33362
diff
changeset
|
1077 |
|
43583
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1078 |
private static final class JShellUnixTerminal extends NoInterruptUnixTerminal implements SuspendableTerminal { |
33362 | 1079 |
|
1080 |
private final StopDetectingInputStream input; |
|
1081 |
||
1082 |
public JShellUnixTerminal(StopDetectingInputStream input) throws Exception { |
|
1083 |
this.input = input; |
|
1084 |
} |
|
1085 |
||
45501
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1086 |
@Override |
33362 | 1087 |
public boolean isRaw() { |
1088 |
try { |
|
1089 |
return getSettings().get("-a").contains("-icanon"); |
|
1090 |
} catch (IOException | InterruptedException ex) { |
|
1091 |
return false; |
|
1092 |
} |
|
1093 |
} |
|
1094 |
||
1095 |
@Override |
|
1096 |
public InputStream wrapInIfNeeded(InputStream in) throws IOException { |
|
1097 |
return input.setInputStream(super.wrapInIfNeeded(in)); |
|
1098 |
} |
|
1099 |
||
1100 |
@Override |
|
1101 |
public void disableInterruptCharacter() { |
|
1102 |
} |
|
1103 |
||
1104 |
@Override |
|
1105 |
public void enableInterruptCharacter() { |
|
1106 |
} |
|
1107 |
||
43583
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1108 |
@Override |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1109 |
public void suspend() { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1110 |
try { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1111 |
getSettings().restore(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1112 |
super.disableInterruptCharacter(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1113 |
} catch (Exception ex) { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1114 |
throw new IllegalStateException(ex); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1115 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1116 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1117 |
|
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1118 |
@Override |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1119 |
public void resume() { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1120 |
try { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1121 |
init(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1122 |
} catch (Exception ex) { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1123 |
throw new IllegalStateException(ex); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1124 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1125 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1126 |
|
33362 | 1127 |
} |
1128 |
||
43583
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1129 |
private static final class JShellWindowsTerminal extends WindowsTerminal implements SuspendableTerminal { |
33362 | 1130 |
|
1131 |
private final StopDetectingInputStream input; |
|
1132 |
||
1133 |
public JShellWindowsTerminal(StopDetectingInputStream input) throws Exception { |
|
1134 |
this.input = input; |
|
1135 |
} |
|
1136 |
||
1137 |
@Override |
|
1138 |
public void init() throws Exception { |
|
1139 |
super.init(); |
|
1140 |
setAnsiSupported(false); |
|
1141 |
} |
|
1142 |
||
1143 |
@Override |
|
1144 |
public InputStream wrapInIfNeeded(InputStream in) throws IOException { |
|
1145 |
return input.setInputStream(super.wrapInIfNeeded(in)); |
|
1146 |
} |
|
1147 |
||
43583
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1148 |
@Override |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1149 |
public void suspend() { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1150 |
try { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1151 |
restore(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1152 |
setConsoleMode(getConsoleMode() & ~ConsoleMode.ENABLE_PROCESSED_INPUT.code); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1153 |
} catch (Exception ex) { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1154 |
throw new IllegalStateException(ex); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1155 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1156 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1157 |
|
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1158 |
@Override |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1159 |
public void resume() { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1160 |
try { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1161 |
restore(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1162 |
init(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1163 |
} catch (Exception ex) { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1164 |
throw new IllegalStateException(ex); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1165 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1166 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1167 |
|
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1168 |
@Override |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1169 |
public boolean isRaw() { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1170 |
return (getConsoleMode() & ConsoleMode.ENABLE_LINE_INPUT.code) == 0; |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1171 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1172 |
|
33362 | 1173 |
} |
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1174 |
|
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1175 |
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
|
1176 |
|
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1177 |
private final StopDetectingInputStream input; |
45501
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1178 |
private final int height; |
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1179 |
|
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1180 |
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
|
1181 |
super(true); |
46923
0172e4350f65
8182297: jshell tool: pasting multiple lines of code truncated
jlahoda
parents:
46185
diff
changeset
|
1182 |
setAnsiSupported(Boolean.getBoolean("test.terminal.ansi.supported")); |
44459 | 1183 |
setEchoEnabled(false); |
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1184 |
this.input = input; |
45501
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1185 |
int h = DEFAULT_HEIGHT; |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1186 |
try { |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1187 |
String hp = System.getProperty("test.terminal.height"); |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1188 |
if (hp != null && !hp.isEmpty()) { |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1189 |
h = Integer.parseInt(hp); |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1190 |
} |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1191 |
} catch (Throwable ex) { |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1192 |
// ignore |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1193 |
} |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1194 |
this.height = h; |
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1195 |
} |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1196 |
|
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1197 |
@Override |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1198 |
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
|
1199 |
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
|
1200 |
} |
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1201 |
|
45501
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1202 |
@Override |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1203 |
public int getHeight() { |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1204 |
return height; |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1205 |
} |
a13e5e2ee35e
8180306: jshell tool: /help -- confusing identifier in feedback mode examples
rfield
parents:
44459
diff
changeset
|
1206 |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36501
diff
changeset
|
1207 |
} |
43363
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1208 |
|
43583
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1209 |
private interface SuspendableTerminal { |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1210 |
public void suspend(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1211 |
public void resume(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1212 |
public boolean isRaw(); |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1213 |
} |
d16e490ec827
8173653: jshell tool: ctrl-C when in external editor aborts jshell -- history lost
jlahoda
parents:
43363
diff
changeset
|
1214 |
|
43363
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1215 |
private static final class CheckCompletionKeyMap extends KeyMap { |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1216 |
|
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1217 |
private final KeyMap del; |
47050 | 1218 |
private final CompletionState completionState; |
43363
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1219 |
|
47050 | 1220 |
public CheckCompletionKeyMap(KeyMap del, CompletionState completionState) { |
43363
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1221 |
super(del.getName(), del.isViKeyMap()); |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1222 |
this.del = del; |
47050 | 1223 |
this.completionState = completionState; |
43363
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1224 |
} |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1225 |
|
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1226 |
@Override |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1227 |
public void bind(CharSequence keySeq, Object function) { |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1228 |
del.bind(keySeq, function); |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1229 |
} |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1230 |
|
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1231 |
@Override |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1232 |
public void bindIfNotBound(CharSequence keySeq, Object function) { |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1233 |
del.bindIfNotBound(keySeq, function); |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1234 |
} |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1235 |
|
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1236 |
@Override |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1237 |
public void from(KeyMap other) { |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1238 |
del.from(other); |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1239 |
} |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1240 |
|
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1241 |
@Override |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1242 |
public Object getAnotherKey() { |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1243 |
return del.getAnotherKey(); |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1244 |
} |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1245 |
|
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1246 |
@Override |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1247 |
public Object getBound(CharSequence keySeq) { |
47050 | 1248 |
this.completionState.actionCount++; |
43363
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1249 |
|
47050 | 1250 |
return del.getBound(keySeq); |
43363
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1251 |
} |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1252 |
|
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1253 |
@Override |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1254 |
public void setBlinkMatchingParen(boolean on) { |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1255 |
del.setBlinkMatchingParen(on); |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1256 |
} |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1257 |
|
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1258 |
@Override |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1259 |
public String toString() { |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1260 |
return "check: " + del.toString(); |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1261 |
} |
a4ed2006a4c5
8153759: jshell tool: Smart completion detection is not reliable
jlahoda
parents:
42843
diff
changeset
|
1262 |
} |
47050 | 1263 |
|
1264 |
private static final class CompletionState { |
|
1265 |
/**The number of actions since the last completion invocation. Will be 1 when completion is |
|
1266 |
* invoked immediately after the last completion invocation.*/ |
|
1267 |
public int actionCount; |
|
1268 |
/**Precomputed completion actions. Should only be reused if actionCount == 1.*/ |
|
1269 |
public List<CompletionTask> todo = Collections.emptyList(); |
|
44459 | 1270 |
} |
47050 | 1271 |
} |