author | rfield |
Tue, 12 Apr 2016 22:23:11 -0700 | |
changeset 37389 | 9c137b83a8b8 |
parent 37007 | 6023a9a9d58a |
child 37745 | 4b6b59f8e327 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
36494 | 2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.internal.jshell.tool; |
|
27 |
||
28 |
import java.io.BufferedWriter; |
|
29 |
import java.io.ByteArrayInputStream; |
|
30 |
import java.io.File; |
|
31 |
import java.io.FileNotFoundException; |
|
32 |
import java.io.FileReader; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.InputStream; |
|
35 |
import java.io.PrintStream; |
|
36 |
import java.io.Reader; |
|
37 |
import java.io.StringReader; |
|
38 |
import java.nio.charset.Charset; |
|
39 |
import java.nio.file.AccessDeniedException; |
|
40 |
import java.nio.file.FileSystems; |
|
41 |
import java.nio.file.Files; |
|
42 |
import java.nio.file.NoSuchFileException; |
|
43 |
import java.nio.file.Path; |
|
44 |
import java.nio.file.Paths; |
|
36990 | 45 |
import java.text.MessageFormat; |
33362 | 46 |
import java.util.ArrayList; |
47 |
import java.util.Arrays; |
|
48 |
import java.util.Collections; |
|
49 |
import java.util.Iterator; |
|
50 |
import java.util.LinkedHashMap; |
|
51 |
import java.util.LinkedHashSet; |
|
52 |
import java.util.List; |
|
36990 | 53 |
import java.util.Locale; |
33362 | 54 |
import java.util.Map; |
55 |
import java.util.Map.Entry; |
|
56 |
import java.util.Scanner; |
|
57 |
import java.util.Set; |
|
58 |
import java.util.function.Consumer; |
|
59 |
import java.util.function.Predicate; |
|
60 |
import java.util.prefs.Preferences; |
|
61 |
import java.util.regex.Matcher; |
|
62 |
import java.util.regex.Pattern; |
|
63 |
import java.util.stream.Collectors; |
|
64 |
import java.util.stream.Stream; |
|
65 |
import java.util.stream.StreamSupport; |
|
66 |
||
67 |
import jdk.internal.jshell.debug.InternalDebugControl; |
|
68 |
import jdk.internal.jshell.tool.IOContext.InputInterruptedException; |
|
36494 | 69 |
import jdk.jshell.DeclarationSnippet; |
33362 | 70 |
import jdk.jshell.Diag; |
71 |
import jdk.jshell.EvalException; |
|
36494 | 72 |
import jdk.jshell.ExpressionSnippet; |
73 |
import jdk.jshell.ImportSnippet; |
|
33362 | 74 |
import jdk.jshell.JShell; |
36494 | 75 |
import jdk.jshell.JShell.Subscription; |
33362 | 76 |
import jdk.jshell.MethodSnippet; |
77 |
import jdk.jshell.PersistentSnippet; |
|
36494 | 78 |
import jdk.jshell.Snippet; |
33362 | 79 |
import jdk.jshell.Snippet.Status; |
36494 | 80 |
import jdk.jshell.SnippetEvent; |
33362 | 81 |
import jdk.jshell.SourceCodeAnalysis; |
82 |
import jdk.jshell.SourceCodeAnalysis.CompletionInfo; |
|
83 |
import jdk.jshell.SourceCodeAnalysis.Suggestion; |
|
36494 | 84 |
import jdk.jshell.TypeDeclSnippet; |
33362 | 85 |
import jdk.jshell.UnresolvedReferenceException; |
36494 | 86 |
import jdk.jshell.VarSnippet; |
33362 | 87 |
|
88 |
import static java.nio.file.StandardOpenOption.CREATE; |
|
89 |
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; |
|
90 |
import static java.nio.file.StandardOpenOption.WRITE; |
|
91 |
import java.util.MissingResourceException; |
|
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
92 |
import java.util.Optional; |
33362 | 93 |
import java.util.ResourceBundle; |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
94 |
import java.util.Spliterators; |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
95 |
import java.util.function.Function; |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
96 |
import java.util.function.Supplier; |
36494 | 97 |
import jdk.internal.jshell.tool.Feedback.FormatAction; |
98 |
import jdk.internal.jshell.tool.Feedback.FormatCase; |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
99 |
import jdk.internal.jshell.tool.Feedback.FormatErrors; |
36494 | 100 |
import jdk.internal.jshell.tool.Feedback.FormatResolve; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
101 |
import jdk.internal.jshell.tool.Feedback.FormatUnresolved; |
36494 | 102 |
import jdk.internal.jshell.tool.Feedback.FormatWhen; |
33362 | 103 |
import static java.util.stream.Collectors.toList; |
36494 | 104 |
import static java.util.stream.Collectors.toMap; |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
105 |
import static jdk.jshell.Snippet.SubKind.VAR_VALUE_SUBKIND; |
33362 | 106 |
|
107 |
/** |
|
108 |
* Command line REPL tool for Java using the JShell API. |
|
109 |
* @author Robert Field |
|
110 |
*/ |
|
111 |
public class JShellTool { |
|
112 |
||
36494 | 113 |
private static final String LINE_SEP = System.getProperty("line.separator"); |
33362 | 114 |
private static final Pattern LINEBREAK = Pattern.compile("\\R"); |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
115 |
private static final Pattern HISTORY_ALL_START_FILENAME = Pattern.compile( |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
116 |
"((?<cmd>(all|history|start))(\\z|\\p{javaWhitespace}+))?(?<filename>.*)"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
117 |
private static final String RECORD_SEPARATOR = "\u241E"; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
118 |
private static final String RB_NAME_PREFIX = "jdk.internal.jshell.tool.resources"; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
119 |
private static final String VERSION_RB_NAME = RB_NAME_PREFIX + ".version"; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
120 |
private static final String L10N_RB_NAME = RB_NAME_PREFIX + ".l10n"; |
33362 | 121 |
|
122 |
final InputStream cmdin; |
|
123 |
final PrintStream cmdout; |
|
124 |
final PrintStream cmderr; |
|
125 |
final PrintStream console; |
|
126 |
final InputStream userin; |
|
127 |
final PrintStream userout; |
|
128 |
final PrintStream usererr; |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36499
diff
changeset
|
129 |
final Preferences prefs; |
36990 | 130 |
final Locale locale; |
33362 | 131 |
|
36494 | 132 |
final Feedback feedback = new Feedback(); |
133 |
||
33362 | 134 |
/** |
135 |
* The constructor for the tool (used by tool launch via main and by test |
|
136 |
* harnesses to capture ins and outs. |
|
137 |
* @param cmdin command line input -- snippets and commands |
|
138 |
* @param cmdout command line output, feedback including errors |
|
139 |
* @param cmderr start-up errors and debugging info |
|
140 |
* @param console console control interaction |
|
141 |
* @param userin code execution input (not yet functional) |
|
142 |
* @param userout code execution output -- System.out.printf("hi") |
|
143 |
* @param usererr code execution error stream -- System.err.printf("Oops") |
|
36990 | 144 |
* @param prefs preferences to use |
145 |
* @param locale locale to use |
|
33362 | 146 |
*/ |
147 |
public JShellTool(InputStream cmdin, PrintStream cmdout, PrintStream cmderr, |
|
148 |
PrintStream console, |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36499
diff
changeset
|
149 |
InputStream userin, PrintStream userout, PrintStream usererr, |
36990 | 150 |
Preferences prefs, Locale locale) { |
33362 | 151 |
this.cmdin = cmdin; |
152 |
this.cmdout = cmdout; |
|
153 |
this.cmderr = cmderr; |
|
154 |
this.console = console; |
|
155 |
this.userin = userin; |
|
156 |
this.userout = userout; |
|
157 |
this.usererr = usererr; |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36499
diff
changeset
|
158 |
this.prefs = prefs; |
36990 | 159 |
this.locale = locale; |
33362 | 160 |
} |
161 |
||
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
162 |
private ResourceBundle versionRB = null; |
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
163 |
private ResourceBundle outputRB = null; |
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
164 |
|
33362 | 165 |
private IOContext input = null; |
166 |
private boolean regenerateOnDeath = true; |
|
167 |
private boolean live = false; |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
168 |
private boolean feedbackInitialized = false; |
33362 | 169 |
|
170 |
SourceCodeAnalysis analysis; |
|
171 |
JShell state = null; |
|
172 |
Subscription shutdownSubscription = null; |
|
173 |
||
174 |
private boolean debug = false; |
|
175 |
public boolean testPrompt = false; |
|
176 |
private String cmdlineClasspath = null; |
|
177 |
private String cmdlineStartup = null; |
|
35812 | 178 |
private String[] editor = null; |
33362 | 179 |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
180 |
// Commands and snippets which should be replayed |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
181 |
private List<String> replayableHistory; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
182 |
private List<String> replayableHistoryPrevious; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
183 |
|
33362 | 184 |
static final String STARTUP_KEY = "STARTUP"; |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
185 |
static final String REPLAY_RESTORE_KEY = "REPLAY_RESTORE"; |
33362 | 186 |
|
187 |
static final String DEFAULT_STARTUP = |
|
188 |
"\n" + |
|
189 |
"import java.util.*;\n" + |
|
190 |
"import java.io.*;\n" + |
|
191 |
"import java.math.*;\n" + |
|
192 |
"import java.net.*;\n" + |
|
193 |
"import java.util.concurrent.*;\n" + |
|
194 |
"import java.util.prefs.*;\n" + |
|
195 |
"import java.util.regex.*;\n" + |
|
196 |
"void printf(String format, Object... args) { System.out.printf(format, args); }\n"; |
|
197 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
198 |
// Tool id (tid) mapping: the three name spaces |
33362 | 199 |
NameSpace mainNamespace; |
200 |
NameSpace startNamespace; |
|
201 |
NameSpace errorNamespace; |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
202 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
203 |
// Tool id (tid) mapping: the current name spaces |
33362 | 204 |
NameSpace currentNameSpace; |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
205 |
|
33362 | 206 |
Map<Snippet,SnippetInfo> mapSnippet; |
207 |
||
36494 | 208 |
/** |
209 |
* Is the input/output currently interactive |
|
210 |
* |
|
211 |
* @return true if console |
|
212 |
*/ |
|
213 |
boolean interactive() { |
|
214 |
return input != null && input.interactiveOutput(); |
|
215 |
} |
|
216 |
||
33362 | 217 |
void debug(String format, Object... args) { |
218 |
if (debug) { |
|
219 |
cmderr.printf(format + "\n", args); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
/** |
|
36494 | 224 |
* Base output for command output -- no pre- or post-fix |
225 |
* |
|
226 |
* @param printf format |
|
227 |
* @param printf args |
|
228 |
*/ |
|
229 |
void rawout(String format, Object... args) { |
|
230 |
cmdout.printf(format, args); |
|
231 |
} |
|
232 |
||
233 |
/** |
|
234 |
* Must show command output |
|
235 |
* |
|
236 |
* @param format printf format |
|
237 |
* @param args printf args |
|
238 |
*/ |
|
239 |
void hard(String format, Object... args) { |
|
240 |
rawout(feedback.getPre() + format + feedback.getPost(), args); |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Error command output |
|
245 |
* |
|
246 |
* @param format printf format |
|
247 |
* @param args printf args |
|
248 |
*/ |
|
249 |
void error(String format, Object... args) { |
|
250 |
rawout(feedback.getErrorPre() + format + feedback.getErrorPost(), args); |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Optional output |
|
255 |
* |
|
33362 | 256 |
* @param format printf format |
257 |
* @param args printf args |
|
258 |
*/ |
|
259 |
void fluff(String format, Object... args) { |
|
36494 | 260 |
if (feedback.shouldDisplayCommandFluff() && interactive()) { |
33362 | 261 |
hard(format, args); |
262 |
} |
|
263 |
} |
|
264 |
||
265 |
/** |
|
36494 | 266 |
* Optional output -- with embedded per- and post-fix |
267 |
* |
|
33362 | 268 |
* @param format printf format |
269 |
* @param args printf args |
|
270 |
*/ |
|
36494 | 271 |
void fluffRaw(String format, Object... args) { |
272 |
if (feedback.shouldDisplayCommandFluff() && interactive()) { |
|
273 |
rawout(format, args); |
|
274 |
} |
|
275 |
} |
|
276 |
||
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
277 |
/** |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
278 |
* Print using resource bundle look-up and adding prefix and postfix |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
279 |
* |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
280 |
* @param key the resource key |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
281 |
*/ |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
282 |
String getResourceString(String key) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
283 |
if (outputRB == null) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
284 |
try { |
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
285 |
outputRB = ResourceBundle.getBundle(L10N_RB_NAME, locale); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
286 |
} catch (MissingResourceException mre) { |
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
287 |
error("Cannot find ResourceBundle: %s for locale: %s", L10N_RB_NAME, locale); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
288 |
return ""; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
289 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
290 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
291 |
String s; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
292 |
try { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
293 |
s = outputRB.getString(key); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
294 |
} catch (MissingResourceException mre) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
295 |
error("Missing resource: %s in %s", key, L10N_RB_NAME); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
296 |
return ""; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
297 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
298 |
return s; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
299 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
300 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
301 |
/** |
36990 | 302 |
* Add prefixing to embedded newlines in a string, leading with the normal |
303 |
* prefix |
|
304 |
* |
|
305 |
* @param s the string to prefix |
|
306 |
*/ |
|
307 |
String prefix(String s) { |
|
308 |
return prefix(s, feedback.getPre()); |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* Add prefixing to embedded newlines in a string |
|
313 |
* |
|
314 |
* @param s the string to prefix |
|
315 |
* @param leading the string to prepend |
|
316 |
*/ |
|
317 |
String prefix(String s, String leading) { |
|
318 |
if (s == null || s.isEmpty()) { |
|
319 |
return ""; |
|
320 |
} |
|
321 |
return leading |
|
322 |
+ s.substring(0, s.length() - 1).replaceAll("\\R", System.getProperty("line.separator") + feedback.getPre()) |
|
323 |
+ s.substring(s.length() - 1, s.length()); |
|
324 |
} |
|
325 |
||
326 |
/** |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
327 |
* Print using resource bundle look-up and adding prefix and postfix |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
328 |
* |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
329 |
* @param key the resource key |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
330 |
*/ |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
331 |
void hardrb(String key) { |
36990 | 332 |
String s = prefix(getResourceString(key)); |
333 |
cmdout.println(s); |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* Format using resource bundle look-up using MessageFormat |
|
338 |
* |
|
339 |
* @param key the resource key |
|
340 |
* @param args |
|
341 |
*/ |
|
342 |
String messageFormat(String key, Object... args) { |
|
343 |
String rs = getResourceString(key); |
|
344 |
return MessageFormat.format(rs, args); |
|
345 |
} |
|
346 |
||
347 |
/** |
|
348 |
* Print using resource bundle look-up, MessageFormat, and add prefix and |
|
349 |
* postfix |
|
350 |
* |
|
351 |
* @param key the resource key |
|
352 |
* @param args |
|
353 |
*/ |
|
354 |
void hardmsg(String key, Object... args) { |
|
355 |
cmdout.println(prefix(messageFormat(key, args))); |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* Print error using resource bundle look-up, MessageFormat, and add prefix |
|
360 |
* and postfix |
|
361 |
* |
|
362 |
* @param key the resource key |
|
363 |
* @param args |
|
364 |
*/ |
|
365 |
void errormsg(String key, Object... args) { |
|
366 |
cmdout.println(prefix(messageFormat(key, args), feedback.getErrorPre())); |
|
367 |
} |
|
368 |
||
369 |
/** |
|
370 |
* Print command-line error using resource bundle look-up, MessageFormat |
|
371 |
* |
|
372 |
* @param key the resource key |
|
373 |
* @param args |
|
374 |
*/ |
|
375 |
void startmsg(String key, Object... args) { |
|
376 |
cmderr.println(prefix(messageFormat(key, args), "")); |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* Print (fluff) using resource bundle look-up, MessageFormat, and add |
|
381 |
* prefix and postfix |
|
382 |
* |
|
383 |
* @param key the resource key |
|
384 |
* @param args |
|
385 |
*/ |
|
386 |
void fluffmsg(String key, Object... args) { |
|
387 |
if (feedback.shouldDisplayCommandFluff() && interactive()) { |
|
388 |
hardmsg(key, args); |
|
389 |
} |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
390 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
391 |
|
36494 | 392 |
<T> void hardPairs(Stream<T> stream, Function<T, String> a, Function<T, String> b) { |
393 |
Map<String, String> a2b = stream.collect(toMap(a, b, |
|
394 |
(m1, m2) -> m1, |
|
395 |
() -> new LinkedHashMap<>())); |
|
396 |
int aLen = 0; |
|
397 |
for (String av : a2b.keySet()) { |
|
398 |
aLen = Math.max(aLen, av.length()); |
|
399 |
} |
|
400 |
String format = " %-" + aLen + "s -- %s"; |
|
401 |
String indentedNewLine = LINE_SEP + feedback.getPre() |
|
402 |
+ String.format(" %-" + (aLen + 4) + "s", ""); |
|
403 |
for (Entry<String, String> e : a2b.entrySet()) { |
|
404 |
hard(format, e.getKey(), e.getValue().replaceAll("\n", indentedNewLine)); |
|
33362 | 405 |
} |
406 |
} |
|
407 |
||
408 |
/** |
|
409 |
* Trim whitespace off end of string |
|
36494 | 410 |
* |
33362 | 411 |
* @param s |
412 |
* @return |
|
413 |
*/ |
|
414 |
static String trimEnd(String s) { |
|
415 |
int last = s.length() - 1; |
|
416 |
int i = last; |
|
417 |
while (i >= 0 && Character.isWhitespace(s.charAt(i))) { |
|
418 |
--i; |
|
419 |
} |
|
420 |
if (i != last) { |
|
421 |
return s.substring(0, i + 1); |
|
422 |
} else { |
|
423 |
return s; |
|
424 |
} |
|
425 |
} |
|
426 |
||
427 |
/** |
|
428 |
* Normal start entry point |
|
429 |
* @param args |
|
430 |
* @throws Exception |
|
431 |
*/ |
|
432 |
public static void main(String[] args) throws Exception { |
|
433 |
new JShellTool(System.in, System.out, System.err, System.out, |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36499
diff
changeset
|
434 |
new ByteArrayInputStream(new byte[0]), System.out, System.err, |
36990 | 435 |
Preferences.userRoot().node("tool/JShell"), |
436 |
Locale.getDefault()) |
|
33362 | 437 |
.start(args); |
438 |
} |
|
439 |
||
440 |
public void start(String[] args) throws Exception { |
|
441 |
List<String> loadList = processCommandArgs(args); |
|
442 |
if (loadList == null) { |
|
443 |
// Abort |
|
444 |
return; |
|
445 |
} |
|
446 |
try (IOContext in = new ConsoleIOContext(this, cmdin, console)) { |
|
447 |
start(in, loadList); |
|
448 |
} |
|
449 |
} |
|
450 |
||
451 |
private void start(IOContext in, List<String> loadList) { |
|
452 |
resetState(); // Initialize |
|
453 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
454 |
// Read replay history from last jshell session into previous history |
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36499
diff
changeset
|
455 |
String prevReplay = prefs.get(REPLAY_RESTORE_KEY, null); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
456 |
if (prevReplay != null) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
457 |
replayableHistoryPrevious = Arrays.asList(prevReplay.split(RECORD_SEPARATOR)); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
458 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
459 |
|
33362 | 460 |
for (String loadFile : loadList) { |
36990 | 461 |
runFile(loadFile, "jshell"); |
33362 | 462 |
} |
463 |
||
464 |
if (regenerateOnDeath) { |
|
36990 | 465 |
hardmsg("jshell.msg.welcome", version()); |
33362 | 466 |
} |
467 |
||
468 |
try { |
|
469 |
while (regenerateOnDeath) { |
|
470 |
if (!live) { |
|
471 |
resetState(); |
|
472 |
} |
|
473 |
run(in); |
|
474 |
} |
|
475 |
} finally { |
|
476 |
closeState(); |
|
477 |
} |
|
478 |
} |
|
479 |
||
480 |
/** |
|
481 |
* Process the command line arguments. |
|
482 |
* Set options. |
|
483 |
* @param args the command line arguments |
|
484 |
* @return the list of files to be loaded |
|
485 |
*/ |
|
486 |
private List<String> processCommandArgs(String[] args) { |
|
487 |
List<String> loadList = new ArrayList<>(); |
|
488 |
Iterator<String> ai = Arrays.asList(args).iterator(); |
|
489 |
while (ai.hasNext()) { |
|
490 |
String arg = ai.next(); |
|
491 |
if (arg.startsWith("-")) { |
|
492 |
switch (arg) { |
|
493 |
case "-classpath": |
|
494 |
case "-cp": |
|
495 |
if (cmdlineClasspath != null) { |
|
36990 | 496 |
startmsg("jshell.err.opt.classpath.conflict"); |
33362 | 497 |
return null; |
498 |
} |
|
499 |
if (ai.hasNext()) { |
|
500 |
cmdlineClasspath = ai.next(); |
|
501 |
} else { |
|
36990 | 502 |
startmsg("jshell.err.opt.classpath.arg"); |
33362 | 503 |
return null; |
504 |
} |
|
505 |
break; |
|
506 |
case "-help": |
|
507 |
printUsage(); |
|
508 |
return null; |
|
509 |
case "-version": |
|
510 |
cmdout.printf("jshell %s\n", version()); |
|
511 |
return null; |
|
512 |
case "-fullversion": |
|
513 |
cmdout.printf("jshell %s\n", fullVersion()); |
|
514 |
return null; |
|
515 |
case "-startup": |
|
516 |
if (cmdlineStartup != null) { |
|
36990 | 517 |
startmsg("jshell.err.opt.startup.conflict"); |
33362 | 518 |
return null; |
519 |
} |
|
36990 | 520 |
cmdlineStartup = readFile(ai.hasNext()? ai.next() : null, "'-startup'"); |
521 |
if (cmdlineStartup == null) { |
|
33362 | 522 |
return null; |
523 |
} |
|
524 |
break; |
|
525 |
case "-nostartup": |
|
526 |
if (cmdlineStartup != null && !cmdlineStartup.isEmpty()) { |
|
36990 | 527 |
startmsg("jshell.err.opt.startup.conflict"); |
33362 | 528 |
return null; |
529 |
} |
|
530 |
cmdlineStartup = ""; |
|
531 |
break; |
|
532 |
default: |
|
36990 | 533 |
startmsg("jshell.err.opt.unknown", arg); |
33362 | 534 |
printUsage(); |
535 |
return null; |
|
536 |
} |
|
537 |
} else { |
|
538 |
loadList.add(arg); |
|
539 |
} |
|
540 |
} |
|
541 |
return loadList; |
|
542 |
} |
|
543 |
||
544 |
private void printUsage() { |
|
36990 | 545 |
cmdout.print(getResourceString("help.usage")); |
33362 | 546 |
} |
547 |
||
548 |
private void resetState() { |
|
549 |
closeState(); |
|
550 |
||
551 |
// Initialize tool id mapping |
|
552 |
mainNamespace = new NameSpace("main", ""); |
|
553 |
startNamespace = new NameSpace("start", "s"); |
|
554 |
errorNamespace = new NameSpace("error", "e"); |
|
555 |
mapSnippet = new LinkedHashMap<>(); |
|
556 |
currentNameSpace = startNamespace; |
|
557 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
558 |
// Reset the replayable history, saving the old for restore |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
559 |
replayableHistoryPrevious = replayableHistory; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
560 |
replayableHistory = new ArrayList<>(); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
561 |
|
33362 | 562 |
state = JShell.builder() |
563 |
.in(userin) |
|
564 |
.out(userout) |
|
565 |
.err(usererr) |
|
566 |
.tempVariableNameGenerator(()-> "$" + currentNameSpace.tidNext()) |
|
567 |
.idGenerator((sn, i) -> (currentNameSpace == startNamespace || state.status(sn).isActive) |
|
568 |
? currentNameSpace.tid(sn) |
|
569 |
: errorNamespace.tid(sn)) |
|
570 |
.build(); |
|
571 |
shutdownSubscription = state.onShutdown((JShell deadState) -> { |
|
572 |
if (deadState == state) { |
|
36990 | 573 |
hardmsg("jshell.msg.terminated"); |
33362 | 574 |
live = false; |
575 |
} |
|
576 |
}); |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
577 |
analysis = state.sourceCodeAnalysis(); |
33362 | 578 |
live = true; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
579 |
if (!feedbackInitialized) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
580 |
startUpRun(getResourceString("startup.feedback")); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
581 |
feedbackInitialized = true; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
582 |
} |
33362 | 583 |
|
584 |
if (cmdlineClasspath != null) { |
|
585 |
state.addToClasspath(cmdlineClasspath); |
|
586 |
} |
|
587 |
||
588 |
String start; |
|
589 |
if (cmdlineStartup == null) { |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36499
diff
changeset
|
590 |
start = prefs.get(STARTUP_KEY, "<nada>"); |
33362 | 591 |
if (start.equals("<nada>")) { |
592 |
start = DEFAULT_STARTUP; |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36499
diff
changeset
|
593 |
prefs.put(STARTUP_KEY, DEFAULT_STARTUP); |
33362 | 594 |
} |
595 |
} else { |
|
596 |
start = cmdlineStartup; |
|
597 |
} |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
598 |
startUpRun(start); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
599 |
currentNameSpace = mainNamespace; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
600 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
601 |
//where |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
602 |
private void startUpRun(String start) { |
33362 | 603 |
try (IOContext suin = new FileScannerIOContext(new StringReader(start))) { |
604 |
run(suin); |
|
605 |
} catch (Exception ex) { |
|
36990 | 606 |
hardmsg("jshell.err.startup.unexpected.exception", ex); |
33362 | 607 |
} |
608 |
} |
|
609 |
||
610 |
private void closeState() { |
|
611 |
live = false; |
|
612 |
JShell oldState = state; |
|
613 |
if (oldState != null) { |
|
614 |
oldState.unsubscribe(shutdownSubscription); // No notification |
|
615 |
oldState.close(); |
|
616 |
} |
|
617 |
} |
|
618 |
||
619 |
/** |
|
620 |
* Main loop |
|
621 |
* @param in the line input/editing context |
|
622 |
*/ |
|
623 |
private void run(IOContext in) { |
|
624 |
IOContext oldInput = input; |
|
625 |
input = in; |
|
626 |
try { |
|
627 |
String incomplete = ""; |
|
628 |
while (live) { |
|
629 |
String prompt; |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
630 |
if (currentNameSpace == mainNamespace) { |
33362 | 631 |
prompt = testPrompt |
632 |
? incomplete.isEmpty() |
|
633 |
? "\u0005" //ENQ |
|
634 |
: "\u0006" //ACK |
|
635 |
: incomplete.isEmpty() |
|
36494 | 636 |
? feedback.getPrompt(currentNameSpace.tidNext()) |
637 |
: feedback.getContinuationPrompt(currentNameSpace.tidNext()) |
|
33362 | 638 |
; |
639 |
} else { |
|
640 |
prompt = ""; |
|
641 |
} |
|
642 |
String raw; |
|
643 |
try { |
|
644 |
raw = in.readLine(prompt, incomplete); |
|
645 |
} catch (InputInterruptedException ex) { |
|
646 |
//input interrupted - clearing current state |
|
647 |
incomplete = ""; |
|
648 |
continue; |
|
649 |
} |
|
650 |
if (raw == null) { |
|
651 |
//EOF |
|
652 |
if (in.interactiveOutput()) { |
|
653 |
// End after user ctrl-D |
|
654 |
regenerateOnDeath = false; |
|
655 |
} |
|
656 |
break; |
|
657 |
} |
|
658 |
String trimmed = trimEnd(raw); |
|
659 |
if (!trimmed.isEmpty()) { |
|
660 |
String line = incomplete + trimmed; |
|
661 |
||
662 |
// No commands in the middle of unprocessed source |
|
663 |
if (incomplete.isEmpty() && line.startsWith("/") && !line.startsWith("//") && !line.startsWith("/*")) { |
|
664 |
processCommand(line.trim()); |
|
665 |
} else { |
|
666 |
incomplete = processSourceCatchingReset(line); |
|
667 |
} |
|
668 |
} |
|
669 |
} |
|
670 |
} catch (IOException ex) { |
|
36990 | 671 |
errormsg("jshell.err.unexpected.exception", ex); |
33362 | 672 |
} finally { |
673 |
input = oldInput; |
|
674 |
} |
|
675 |
} |
|
676 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
677 |
private void addToReplayHistory(String s) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
678 |
if (currentNameSpace == mainNamespace) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
679 |
replayableHistory.add(s); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
680 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
681 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
682 |
|
33362 | 683 |
private String processSourceCatchingReset(String src) { |
684 |
try { |
|
685 |
input.beforeUserCode(); |
|
686 |
return processSource(src); |
|
687 |
} catch (IllegalStateException ex) { |
|
688 |
hard("Resetting..."); |
|
689 |
live = false; // Make double sure |
|
690 |
return ""; |
|
691 |
} finally { |
|
692 |
input.afterUserCode(); |
|
693 |
} |
|
694 |
} |
|
695 |
||
696 |
private void processCommand(String cmd) { |
|
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
697 |
if (cmd.startsWith("/-")) { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
698 |
try { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
699 |
//handle "/-[number]" |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
700 |
cmdUseHistoryEntry(Integer.parseInt(cmd.substring(1))); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
701 |
return ; |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
702 |
} catch (NumberFormatException ex) { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
703 |
//ignore |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
704 |
} |
33362 | 705 |
} |
706 |
String arg = ""; |
|
707 |
int idx = cmd.indexOf(' '); |
|
708 |
if (idx > 0) { |
|
709 |
arg = cmd.substring(idx + 1).trim(); |
|
710 |
cmd = cmd.substring(0, idx); |
|
711 |
} |
|
35812 | 712 |
Command[] candidates = findCommand(cmd, c -> c.kind.isRealCommand); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
713 |
switch (candidates.length) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
714 |
case 0: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
715 |
if (!rerunHistoryEntryById(cmd.substring(1))) { |
36990 | 716 |
errormsg("jshell.err.no.such.command.or.snippet.id", cmd); |
717 |
fluffmsg("jshell.msg.help.for.help"); |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
718 |
} break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
719 |
case 1: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
720 |
Command command = candidates[0]; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
721 |
// If comand was successful and is of a replayable kind, add it the replayable history |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
722 |
if (command.run.apply(arg) && command.kind == CommandKind.REPLAY) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
723 |
addToReplayHistory((command.command + " " + arg).trim()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
724 |
} break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
725 |
default: |
36990 | 726 |
errormsg("jshell.err.command.ambiguous", cmd, |
727 |
Arrays.stream(candidates).map(c -> c.command).collect(Collectors.joining(", "))); |
|
728 |
fluffmsg("jshell.msg.help.for.help"); |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
729 |
break; |
33362 | 730 |
} |
731 |
} |
|
732 |
||
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
733 |
private Command[] findCommand(String cmd, Predicate<Command> filter) { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
734 |
Command exact = commands.get(cmd); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
735 |
if (exact != null) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
736 |
return new Command[] {exact}; |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
737 |
|
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
738 |
return commands.values() |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
739 |
.stream() |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
740 |
.filter(filter) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
741 |
.filter(command -> command.command.startsWith(cmd)) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
742 |
.toArray(size -> new Command[size]); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
743 |
} |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
744 |
|
33362 | 745 |
private static Path toPathResolvingUserHome(String pathString) { |
746 |
if (pathString.replace(File.separatorChar, '/').startsWith("~/")) |
|
747 |
return Paths.get(System.getProperty("user.home"), pathString.substring(2)); |
|
748 |
else |
|
749 |
return Paths.get(pathString); |
|
750 |
} |
|
751 |
||
752 |
static final class Command { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
753 |
public final String command; |
36990 | 754 |
public final String helpKey; |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
755 |
public final Function<String,Boolean> run; |
33362 | 756 |
public final CompletionProvider completions; |
757 |
public final CommandKind kind; |
|
758 |
||
35812 | 759 |
// NORMAL Commands |
36990 | 760 |
public Command(String command, Function<String,Boolean> run, CompletionProvider completions) { |
761 |
this(command, run, completions, CommandKind.NORMAL); |
|
762 |
} |
|
763 |
||
764 |
// Special kinds of Commands |
|
765 |
public Command(String command, Function<String,Boolean> run, CompletionProvider completions, CommandKind kind) { |
|
766 |
this(command, "help." + command.substring(1), |
|
767 |
run, completions, kind); |
|
33362 | 768 |
} |
769 |
||
35812 | 770 |
// Documentation pseudo-commands |
36990 | 771 |
public Command(String command, String helpKey, CommandKind kind) { |
772 |
this(command, helpKey, |
|
35812 | 773 |
arg -> { throw new IllegalStateException(); }, |
774 |
EMPTY_COMPLETION_PROVIDER, |
|
775 |
kind); |
|
776 |
} |
|
777 |
||
36990 | 778 |
public Command(String command, String helpKey, Function<String,Boolean> run, CompletionProvider completions, CommandKind kind) { |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
779 |
this.command = command; |
36990 | 780 |
this.helpKey = helpKey; |
33362 | 781 |
this.run = run; |
782 |
this.completions = completions; |
|
783 |
this.kind = kind; |
|
784 |
} |
|
785 |
||
786 |
} |
|
787 |
||
788 |
interface CompletionProvider { |
|
789 |
List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor); |
|
790 |
} |
|
791 |
||
792 |
enum CommandKind { |
|
35812 | 793 |
NORMAL(true, true, true), |
794 |
REPLAY(true, true, true), |
|
795 |
HIDDEN(true, false, false), |
|
796 |
HELP_ONLY(false, true, false), |
|
797 |
HELP_SUBJECT(false, false, false); |
|
798 |
||
799 |
final boolean isRealCommand; |
|
800 |
final boolean showInHelp; |
|
801 |
final boolean shouldSuggestCompletions; |
|
802 |
private CommandKind(boolean isRealCommand, boolean showInHelp, boolean shouldSuggestCompletions) { |
|
803 |
this.isRealCommand = isRealCommand; |
|
804 |
this.showInHelp = showInHelp; |
|
805 |
this.shouldSuggestCompletions = shouldSuggestCompletions; |
|
806 |
} |
|
807 |
} |
|
808 |
||
33362 | 809 |
static final class FixedCompletionProvider implements CompletionProvider { |
810 |
||
811 |
private final String[] alternatives; |
|
812 |
||
813 |
public FixedCompletionProvider(String... alternatives) { |
|
814 |
this.alternatives = alternatives; |
|
815 |
} |
|
816 |
||
817 |
@Override |
|
818 |
public List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor) { |
|
819 |
List<Suggestion> result = new ArrayList<>(); |
|
820 |
||
821 |
for (String alternative : alternatives) { |
|
822 |
if (alternative.startsWith(input)) { |
|
823 |
result.add(new Suggestion(alternative, false)); |
|
824 |
} |
|
825 |
} |
|
826 |
||
827 |
anchor[0] = 0; |
|
828 |
||
829 |
return result; |
|
830 |
} |
|
831 |
||
832 |
} |
|
833 |
||
834 |
private static final CompletionProvider EMPTY_COMPLETION_PROVIDER = new FixedCompletionProvider(); |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
835 |
private static final CompletionProvider KEYWORD_COMPLETION_PROVIDER = new FixedCompletionProvider("all ", "start ", "history "); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
836 |
private static final CompletionProvider RELOAD_OPTIONS_COMPLETION_PROVIDER = new FixedCompletionProvider("restore", "quiet"); |
33362 | 837 |
private static final CompletionProvider FILE_COMPLETION_PROVIDER = fileCompletions(p -> true); |
838 |
private final Map<String, Command> commands = new LinkedHashMap<>(); |
|
839 |
private void registerCommand(Command cmd) { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
840 |
commands.put(cmd.command, cmd); |
33362 | 841 |
} |
842 |
private static CompletionProvider fileCompletions(Predicate<Path> accept) { |
|
843 |
return (code, cursor, anchor) -> { |
|
844 |
int lastSlash = code.lastIndexOf('/'); |
|
845 |
String path = code.substring(0, lastSlash + 1); |
|
846 |
String prefix = lastSlash != (-1) ? code.substring(lastSlash + 1) : code; |
|
847 |
Path current = toPathResolvingUserHome(path); |
|
848 |
List<Suggestion> result = new ArrayList<>(); |
|
849 |
try (Stream<Path> dir = Files.list(current)) { |
|
850 |
dir.filter(f -> accept.test(f) && f.getFileName().toString().startsWith(prefix)) |
|
851 |
.map(f -> new Suggestion(f.getFileName() + (Files.isDirectory(f) ? "/" : ""), false)) |
|
852 |
.forEach(result::add); |
|
853 |
} catch (IOException ex) { |
|
854 |
//ignore... |
|
855 |
} |
|
856 |
if (path.isEmpty()) { |
|
857 |
StreamSupport.stream(FileSystems.getDefault().getRootDirectories().spliterator(), false) |
|
858 |
.filter(root -> accept.test(root) && root.toString().startsWith(prefix)) |
|
859 |
.map(root -> new Suggestion(root.toString(), false)) |
|
860 |
.forEach(result::add); |
|
861 |
} |
|
862 |
anchor[0] = path.length(); |
|
863 |
return result; |
|
864 |
}; |
|
865 |
} |
|
866 |
||
867 |
private static CompletionProvider classPathCompletion() { |
|
868 |
return fileCompletions(p -> Files.isDirectory(p) || |
|
869 |
p.getFileName().toString().endsWith(".zip") || |
|
870 |
p.getFileName().toString().endsWith(".jar")); |
|
871 |
} |
|
872 |
||
873 |
private CompletionProvider editCompletion() { |
|
874 |
return (prefix, cursor, anchor) -> { |
|
875 |
anchor[0] = 0; |
|
876 |
return state.snippets() |
|
877 |
.stream() |
|
878 |
.flatMap(k -> (k instanceof DeclarationSnippet) |
|
879 |
? Stream.of(String.valueOf(k.id()), ((DeclarationSnippet) k).name()) |
|
880 |
: Stream.of(String.valueOf(k.id()))) |
|
881 |
.filter(k -> k.startsWith(prefix)) |
|
882 |
.map(k -> new Suggestion(k, false)) |
|
883 |
.collect(Collectors.toList()); |
|
884 |
}; |
|
885 |
} |
|
886 |
||
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
887 |
private CompletionProvider editKeywordCompletion() { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
888 |
return (code, cursor, anchor) -> { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
889 |
List<Suggestion> result = new ArrayList<>(); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
890 |
result.addAll(KEYWORD_COMPLETION_PROVIDER.completionSuggestions(code, cursor, anchor)); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
891 |
result.addAll(editCompletion().completionSuggestions(code, cursor, anchor)); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
892 |
return result; |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
893 |
}; |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
894 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
895 |
|
33362 | 896 |
private static CompletionProvider saveCompletion() { |
897 |
return (code, cursor, anchor) -> { |
|
898 |
List<Suggestion> result = new ArrayList<>(); |
|
899 |
int space = code.indexOf(' '); |
|
900 |
if (space == (-1)) { |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
901 |
result.addAll(KEYWORD_COMPLETION_PROVIDER.completionSuggestions(code, cursor, anchor)); |
33362 | 902 |
} |
903 |
result.addAll(FILE_COMPLETION_PROVIDER.completionSuggestions(code.substring(space + 1), cursor - space - 1, anchor)); |
|
904 |
anchor[0] += space + 1; |
|
905 |
return result; |
|
906 |
}; |
|
907 |
} |
|
908 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
909 |
private static CompletionProvider reloadCompletion() { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
910 |
return (code, cursor, anchor) -> { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
911 |
List<Suggestion> result = new ArrayList<>(); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
912 |
int pastSpace = code.indexOf(' ') + 1; // zero if no space |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
913 |
result.addAll(RELOAD_OPTIONS_COMPLETION_PROVIDER.completionSuggestions(code.substring(pastSpace), cursor - pastSpace, anchor)); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
914 |
anchor[0] += pastSpace; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
915 |
return result; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
916 |
}; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
917 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
918 |
|
36990 | 919 |
// Table of commands -- with command forms, argument kinds, helpKey message, implementation, ... |
33362 | 920 |
|
921 |
{ |
|
36990 | 922 |
registerCommand(new Command("/list", |
35812 | 923 |
arg -> cmdList(arg), |
924 |
editKeywordCompletion())); |
|
36990 | 925 |
registerCommand(new Command("/edit", |
35812 | 926 |
arg -> cmdEdit(arg), |
927 |
editCompletion())); |
|
36990 | 928 |
registerCommand(new Command("/drop", |
35812 | 929 |
arg -> cmdDrop(arg), |
930 |
editCompletion(), |
|
931 |
CommandKind.REPLAY)); |
|
36990 | 932 |
registerCommand(new Command("/save", |
35812 | 933 |
arg -> cmdSave(arg), |
934 |
saveCompletion())); |
|
36990 | 935 |
registerCommand(new Command("/open", |
35812 | 936 |
arg -> cmdOpen(arg), |
937 |
FILE_COMPLETION_PROVIDER)); |
|
36990 | 938 |
registerCommand(new Command("/vars", |
35812 | 939 |
arg -> cmdVars(), |
940 |
EMPTY_COMPLETION_PROVIDER)); |
|
36990 | 941 |
registerCommand(new Command("/methods", |
35812 | 942 |
arg -> cmdMethods(), |
943 |
EMPTY_COMPLETION_PROVIDER)); |
|
36990 | 944 |
registerCommand(new Command("/classes", |
35812 | 945 |
arg -> cmdClasses(), |
946 |
EMPTY_COMPLETION_PROVIDER)); |
|
36990 | 947 |
registerCommand(new Command("/imports", |
35812 | 948 |
arg -> cmdImports(), |
949 |
EMPTY_COMPLETION_PROVIDER)); |
|
36990 | 950 |
registerCommand(new Command("/exit", |
35812 | 951 |
arg -> cmdExit(), |
952 |
EMPTY_COMPLETION_PROVIDER)); |
|
36990 | 953 |
registerCommand(new Command("/reset", |
35812 | 954 |
arg -> cmdReset(), |
955 |
EMPTY_COMPLETION_PROVIDER)); |
|
36990 | 956 |
registerCommand(new Command("/reload", |
35812 | 957 |
arg -> cmdReload(arg), |
958 |
reloadCompletion())); |
|
36990 | 959 |
registerCommand(new Command("/classpath", |
35812 | 960 |
arg -> cmdClasspath(arg), |
961 |
classPathCompletion(), |
|
962 |
CommandKind.REPLAY)); |
|
36990 | 963 |
registerCommand(new Command("/history", |
35812 | 964 |
arg -> cmdHistory(), |
965 |
EMPTY_COMPLETION_PROVIDER)); |
|
36990 | 966 |
registerCommand(new Command("/debug", |
35812 | 967 |
arg -> cmdDebug(arg), |
968 |
EMPTY_COMPLETION_PROVIDER, |
|
969 |
CommandKind.HIDDEN)); |
|
36990 | 970 |
registerCommand(new Command("/help", |
35812 | 971 |
arg -> cmdHelp(arg), |
972 |
EMPTY_COMPLETION_PROVIDER)); |
|
36990 | 973 |
registerCommand(new Command("/set", |
36494 | 974 |
arg -> cmdSet(arg), |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
975 |
new FixedCompletionProvider("format", "feedback", "prompt", "newmode", "start", "editor"))); |
36990 | 976 |
registerCommand(new Command("/?", |
977 |
"help.quest", |
|
35812 | 978 |
arg -> cmdHelp(arg), |
36990 | 979 |
EMPTY_COMPLETION_PROVIDER, |
980 |
CommandKind.NORMAL)); |
|
981 |
registerCommand(new Command("/!", |
|
982 |
"help.bang", |
|
35812 | 983 |
arg -> cmdUseHistoryEntry(-1), |
36990 | 984 |
EMPTY_COMPLETION_PROVIDER, |
985 |
CommandKind.NORMAL)); |
|
35812 | 986 |
|
987 |
// Documentation pseudo-commands |
|
36990 | 988 |
registerCommand(new Command("/<id>", |
989 |
"help.id", |
|
35812 | 990 |
CommandKind.HELP_ONLY)); |
36990 | 991 |
registerCommand(new Command("/-<n>", |
992 |
"help.previous", |
|
35812 | 993 |
CommandKind.HELP_ONLY)); |
36990 | 994 |
registerCommand(new Command("intro", |
995 |
"help.intro", |
|
35812 | 996 |
CommandKind.HELP_SUBJECT)); |
36990 | 997 |
registerCommand(new Command("shortcuts", |
998 |
"help.shortcuts", |
|
35812 | 999 |
CommandKind.HELP_SUBJECT)); |
33362 | 1000 |
} |
1001 |
||
1002 |
public List<Suggestion> commandCompletionSuggestions(String code, int cursor, int[] anchor) { |
|
1003 |
String prefix = code.substring(0, cursor); |
|
1004 |
int space = prefix.indexOf(' '); |
|
1005 |
Stream<Suggestion> result; |
|
1006 |
||
1007 |
if (space == (-1)) { |
|
1008 |
result = commands.values() |
|
1009 |
.stream() |
|
1010 |
.distinct() |
|
35812 | 1011 |
.filter(cmd -> cmd.kind.shouldSuggestCompletions) |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1012 |
.map(cmd -> cmd.command) |
33362 | 1013 |
.filter(key -> key.startsWith(prefix)) |
1014 |
.map(key -> new Suggestion(key + " ", false)); |
|
1015 |
anchor[0] = 0; |
|
1016 |
} else { |
|
1017 |
String arg = prefix.substring(space + 1); |
|
1018 |
String cmd = prefix.substring(0, space); |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1019 |
Command[] candidates = findCommand(cmd, c -> true); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1020 |
if (candidates.length == 1) { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1021 |
result = candidates[0].completions.completionSuggestions(arg, cursor - space, anchor).stream(); |
33362 | 1022 |
anchor[0] += space + 1; |
1023 |
} else { |
|
1024 |
result = Stream.empty(); |
|
1025 |
} |
|
1026 |
} |
|
1027 |
||
1028 |
return result.sorted((s1, s2) -> s1.continuation.compareTo(s2.continuation)) |
|
1029 |
.collect(Collectors.toList()); |
|
1030 |
} |
|
1031 |
||
1032 |
public String commandDocumentation(String code, int cursor) { |
|
1033 |
code = code.substring(0, cursor); |
|
1034 |
int space = code.indexOf(' '); |
|
1035 |
||
1036 |
if (space != (-1)) { |
|
1037 |
String cmd = code.substring(0, space); |
|
1038 |
Command command = commands.get(cmd); |
|
1039 |
if (command != null) { |
|
36990 | 1040 |
return getResourceString(command.helpKey + ".summary"); |
33362 | 1041 |
} |
1042 |
} |
|
1043 |
||
1044 |
return null; |
|
1045 |
} |
|
1046 |
||
1047 |
// --- Command implementations --- |
|
1048 |
||
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1049 |
private static final String[] SET_SUBCOMMANDS = new String[]{ |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1050 |
"format", "feedback", "newmode", "prompt", "editor", "start"}; |
36494 | 1051 |
|
1052 |
final boolean cmdSet(String arg) { |
|
1053 |
ArgTokenizer at = new ArgTokenizer(arg.trim()); |
|
1054 |
String which = setSubCommand(at); |
|
1055 |
if (which == null) { |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1056 |
return false; |
36494 | 1057 |
} |
1058 |
switch (which) { |
|
1059 |
case "format": |
|
1060 |
return feedback.setFormat(this, at); |
|
1061 |
case "feedback": |
|
1062 |
return feedback.setFeedback(this, at); |
|
1063 |
case "newmode": |
|
1064 |
return feedback.setNewMode(this, at); |
|
1065 |
case "prompt": |
|
1066 |
return feedback.setPrompt(this, at); |
|
1067 |
case "editor": { |
|
1068 |
String prog = at.next(); |
|
1069 |
if (prog == null) { |
|
36990 | 1070 |
errormsg("jshell.err.set.editor.arg"); |
36494 | 1071 |
return false; |
1072 |
} else { |
|
1073 |
List<String> ed = new ArrayList<>(); |
|
1074 |
ed.add(prog); |
|
1075 |
String n; |
|
1076 |
while ((n = at.next()) != null) { |
|
1077 |
ed.add(n); |
|
1078 |
} |
|
1079 |
editor = ed.toArray(new String[ed.size()]); |
|
37389 | 1080 |
fluffmsg("jshell.msg.set.editor.set", prog); |
36494 | 1081 |
return true; |
1082 |
} |
|
1083 |
} |
|
1084 |
case "start": { |
|
37389 | 1085 |
String init = readFile(at.next(), "/set start"); |
36990 | 1086 |
if (init == null) { |
1087 |
return false; |
|
36494 | 1088 |
} else { |
36990 | 1089 |
prefs.put(STARTUP_KEY, init); |
1090 |
return true; |
|
36494 | 1091 |
} |
1092 |
} |
|
1093 |
default: |
|
36990 | 1094 |
errormsg("jshell.err.arg", "/set", at.val()); |
36494 | 1095 |
return false; |
33362 | 1096 |
} |
1097 |
} |
|
1098 |
||
36494 | 1099 |
boolean printSetHelp(ArgTokenizer at) { |
1100 |
String which = setSubCommand(at); |
|
1101 |
if (which == null) { |
|
1102 |
return false; |
|
1103 |
} |
|
36990 | 1104 |
hardrb("help.set." + which); |
1105 |
return true; |
|
36494 | 1106 |
} |
1107 |
||
1108 |
String setSubCommand(ArgTokenizer at) { |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1109 |
String[] matches = at.next(SET_SUBCOMMANDS); |
36494 | 1110 |
if (matches == null) { |
36990 | 1111 |
errormsg("jshell.err.set.arg"); |
36494 | 1112 |
return null; |
36990 | 1113 |
} |
1114 |
if (matches.length == 0) { |
|
1115 |
errormsg("jshell.err.arg", "/set", at.val()); |
|
1116 |
fluffmsg("jshell.msg.use.one.of", Arrays.stream(SET_SUBCOMMANDS) |
|
36494 | 1117 |
.collect(Collectors.joining(", ")) |
1118 |
); |
|
1119 |
return null; |
|
36990 | 1120 |
} |
1121 |
if (matches.length > 1) { |
|
1122 |
errormsg("jshell.err.set.ambiguous", at.val()); |
|
1123 |
fluffmsg("jshell.msg.use.one.of", Arrays.stream(matches) |
|
36494 | 1124 |
.collect(Collectors.joining(", ")) |
1125 |
); |
|
1126 |
return null; |
|
1127 |
} |
|
1128 |
return matches[0]; |
|
1129 |
} |
|
1130 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1131 |
boolean cmdClasspath(String arg) { |
33362 | 1132 |
if (arg.isEmpty()) { |
36990 | 1133 |
errormsg("jshell.err.classpath.arg"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1134 |
return false; |
33362 | 1135 |
} else { |
1136 |
state.addToClasspath(toPathResolvingUserHome(arg).toString()); |
|
36990 | 1137 |
fluffmsg("jshell.msg.classpath", arg); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1138 |
return true; |
33362 | 1139 |
} |
1140 |
} |
|
1141 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1142 |
boolean cmdDebug(String arg) { |
33362 | 1143 |
if (arg.isEmpty()) { |
1144 |
debug = !debug; |
|
1145 |
InternalDebugControl.setDebugFlags(state, debug ? InternalDebugControl.DBG_GEN : 0); |
|
1146 |
fluff("Debugging %s", debug ? "on" : "off"); |
|
1147 |
} else { |
|
1148 |
int flags = 0; |
|
1149 |
for (char ch : arg.toCharArray()) { |
|
1150 |
switch (ch) { |
|
1151 |
case '0': |
|
1152 |
flags = 0; |
|
1153 |
debug = false; |
|
1154 |
fluff("Debugging off"); |
|
1155 |
break; |
|
1156 |
case 'r': |
|
1157 |
debug = true; |
|
1158 |
fluff("REPL tool debugging on"); |
|
1159 |
break; |
|
1160 |
case 'g': |
|
1161 |
flags |= InternalDebugControl.DBG_GEN; |
|
1162 |
fluff("General debugging on"); |
|
1163 |
break; |
|
1164 |
case 'f': |
|
1165 |
flags |= InternalDebugControl.DBG_FMGR; |
|
1166 |
fluff("File manager debugging on"); |
|
1167 |
break; |
|
1168 |
case 'c': |
|
1169 |
flags |= InternalDebugControl.DBG_COMPA; |
|
1170 |
fluff("Completion analysis debugging on"); |
|
1171 |
break; |
|
1172 |
case 'd': |
|
1173 |
flags |= InternalDebugControl.DBG_DEP; |
|
1174 |
fluff("Dependency debugging on"); |
|
1175 |
break; |
|
1176 |
case 'e': |
|
1177 |
flags |= InternalDebugControl.DBG_EVNT; |
|
1178 |
fluff("Event debugging on"); |
|
1179 |
break; |
|
1180 |
default: |
|
1181 |
hard("Unknown debugging option: %c", ch); |
|
1182 |
fluff("Use: 0 r g f c d"); |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1183 |
return false; |
33362 | 1184 |
} |
1185 |
} |
|
1186 |
InternalDebugControl.setDebugFlags(state, flags); |
|
1187 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1188 |
return true; |
33362 | 1189 |
} |
1190 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1191 |
private boolean cmdExit() { |
33362 | 1192 |
regenerateOnDeath = false; |
1193 |
live = false; |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1194 |
if (!replayableHistory.isEmpty()) { |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1195 |
// Prevent history overflow by calculating what will fit, starting |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1196 |
// with must recent |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1197 |
int sepLen = RECORD_SEPARATOR.length(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1198 |
int length = 0; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1199 |
int first = replayableHistory.size(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1200 |
while(length < Preferences.MAX_VALUE_LENGTH && --first >= 0) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1201 |
length += replayableHistory.get(first).length() + sepLen; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1202 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1203 |
String hist = replayableHistory |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1204 |
.subList(first + 1, replayableHistory.size()) |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1205 |
.stream() |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1206 |
.reduce( (a, b) -> a + RECORD_SEPARATOR + b) |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1207 |
.get(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1208 |
prefs.put(REPLAY_RESTORE_KEY, hist); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1209 |
} |
36990 | 1210 |
fluffmsg("jshell.msg.goodbye"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1211 |
return true; |
33362 | 1212 |
} |
1213 |
||
36494 | 1214 |
boolean cmdHelp(String arg) { |
1215 |
ArgTokenizer at = new ArgTokenizer(arg); |
|
1216 |
String subject = at.next(); |
|
1217 |
if (subject != null) { |
|
1218 |
Command[] matches = commands.values().stream() |
|
1219 |
.filter(c -> c.command.startsWith(subject)) |
|
1220 |
.toArray(size -> new Command[size]); |
|
1221 |
at.mark(); |
|
1222 |
String sub = at.next(); |
|
1223 |
if (sub != null && matches.length == 1 && matches[0].command.equals("/set")) { |
|
1224 |
at.rewind(); |
|
1225 |
return printSetHelp(at); |
|
1226 |
} |
|
1227 |
if (matches.length > 0) { |
|
1228 |
for (Command c : matches) { |
|
1229 |
hard(""); |
|
1230 |
hard("%s", c.command); |
|
1231 |
hard(""); |
|
36990 | 1232 |
hardrb(c.helpKey); |
36494 | 1233 |
} |
1234 |
return true; |
|
1235 |
} else { |
|
36990 | 1236 |
errormsg("jshell.err.help.arg", arg); |
36494 | 1237 |
} |
33362 | 1238 |
} |
36990 | 1239 |
hardmsg("jshell.msg.help.begin"); |
36494 | 1240 |
hardPairs(commands.values().stream() |
1241 |
.filter(cmd -> cmd.kind.showInHelp), |
|
36990 | 1242 |
cmd -> cmd.command + " " + getResourceString(cmd.helpKey + ".args"), |
1243 |
cmd -> getResourceString(cmd.helpKey + ".summary") |
|
36494 | 1244 |
); |
36990 | 1245 |
hardmsg("jshell.msg.help.subject"); |
36494 | 1246 |
hardPairs(commands.values().stream() |
1247 |
.filter(cmd -> cmd.kind == CommandKind.HELP_SUBJECT), |
|
1248 |
cmd -> cmd.command, |
|
36990 | 1249 |
cmd -> getResourceString(cmd.helpKey + ".summary") |
36494 | 1250 |
); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1251 |
return true; |
33362 | 1252 |
} |
1253 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1254 |
private boolean cmdHistory() { |
33362 | 1255 |
cmdout.println(); |
1256 |
for (String s : input.currentSessionHistory()) { |
|
1257 |
// No number prefix, confusing with snippet ids |
|
1258 |
cmdout.printf("%s\n", s); |
|
1259 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1260 |
return true; |
33362 | 1261 |
} |
1262 |
||
1263 |
/** |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1264 |
* Avoid parameterized varargs possible heap pollution warning. |
33362 | 1265 |
*/ |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1266 |
private interface SnippetPredicate extends Predicate<Snippet> { } |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1267 |
|
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1268 |
/** |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1269 |
* Apply filters to a stream until one that is non-empty is found. |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1270 |
* Adapted from Stuart Marks |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1271 |
* |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1272 |
* @param supplier Supply the Snippet stream to filter |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1273 |
* @param filters Filters to attempt |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1274 |
* @return The non-empty filtered Stream, or null |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1275 |
*/ |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1276 |
private static Stream<Snippet> nonEmptyStream(Supplier<Stream<Snippet>> supplier, |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1277 |
SnippetPredicate... filters) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1278 |
for (SnippetPredicate filt : filters) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1279 |
Iterator<Snippet> iterator = supplier.get().filter(filt).iterator(); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1280 |
if (iterator.hasNext()) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1281 |
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false); |
33362 | 1282 |
} |
1283 |
} |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1284 |
return null; |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1285 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1286 |
|
35812 | 1287 |
private boolean inStartUp(Snippet sn) { |
1288 |
return mapSnippet.get(sn).space == startNamespace; |
|
1289 |
} |
|
1290 |
||
1291 |
private boolean isActive(Snippet sn) { |
|
1292 |
return state.status(sn).isActive; |
|
1293 |
} |
|
1294 |
||
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1295 |
private boolean mainActive(Snippet sn) { |
35812 | 1296 |
return !inStartUp(sn) && isActive(sn); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1297 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1298 |
|
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1299 |
private boolean matchingDeclaration(Snippet sn, String name) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1300 |
return sn instanceof DeclarationSnippet |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1301 |
&& ((DeclarationSnippet) sn).name().equals(name); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1302 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1303 |
|
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1304 |
/** |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1305 |
* Convert a user argument to a Stream of snippets referenced by that argument |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1306 |
* (or lack of argument). |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1307 |
* |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1308 |
* @param arg The user's argument to the command, maybe be the empty string |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1309 |
* @return a Stream of referenced snippets or null if no matches to specific arg |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1310 |
*/ |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1311 |
private Stream<Snippet> argToSnippets(String arg, boolean allowAll) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1312 |
List<Snippet> snippets = state.snippets(); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1313 |
if (allowAll && arg.equals("all")) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1314 |
// all snippets including start-up, failed, and overwritten |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1315 |
return snippets.stream(); |
35812 | 1316 |
} else if (allowAll && arg.equals("start")) { |
1317 |
// start-up snippets |
|
1318 |
return snippets.stream() |
|
1319 |
.filter(this::inStartUp); |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1320 |
} else if (arg.isEmpty()) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1321 |
// Default is all active user snippets |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1322 |
return snippets.stream() |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1323 |
.filter(this::mainActive); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1324 |
} else { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1325 |
Stream<Snippet> result = |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1326 |
nonEmptyStream( |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1327 |
() -> snippets.stream(), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1328 |
// look for active user declarations matching the name |
35812 | 1329 |
sn -> isActive(sn) && matchingDeclaration(sn, arg), |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1330 |
// else, look for any declarations matching the name |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1331 |
sn -> matchingDeclaration(sn, arg), |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1332 |
// else, look for an id of this name |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1333 |
sn -> sn.id().equals(arg) |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1334 |
); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1335 |
return result; |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1336 |
} |
33362 | 1337 |
} |
1338 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1339 |
private boolean cmdDrop(String arg) { |
33362 | 1340 |
if (arg.isEmpty()) { |
36990 | 1341 |
errormsg("jshell.err.drop.arg"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1342 |
return false; |
33362 | 1343 |
} |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1344 |
Stream<Snippet> stream = argToSnippets(arg, false); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1345 |
if (stream == null) { |
36990 | 1346 |
errormsg("jshell.err.def.or.id.not.found", arg); |
1347 |
fluffmsg("jshell.msg.see.classes.etc"); |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1348 |
return false; |
33362 | 1349 |
} |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1350 |
List<Snippet> snippets = stream |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1351 |
.filter(sn -> state.status(sn).isActive && sn instanceof PersistentSnippet) |
33362 | 1352 |
.collect(toList()); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1353 |
if (snippets.isEmpty()) { |
37389 | 1354 |
errormsg("jshell.err.drop.not.active"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1355 |
return false; |
33362 | 1356 |
} |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1357 |
if (snippets.size() > 1) { |
36990 | 1358 |
errormsg("jshell.err.drop.ambiguous"); |
1359 |
fluffmsg("jshell.msg.use.one.of", snippets.stream() |
|
1360 |
.map(sn -> String.format("\n%4s : %s", sn.id(), sn.source().replace("\n", "\n "))) |
|
1361 |
.collect(Collectors.joining(", ")) |
|
1362 |
); |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1363 |
return false; |
33362 | 1364 |
} |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1365 |
PersistentSnippet psn = (PersistentSnippet) snippets.get(0); |
33362 | 1366 |
state.drop(psn).forEach(this::handleEvent); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1367 |
return true; |
33362 | 1368 |
} |
1369 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1370 |
private boolean cmdEdit(String arg) { |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1371 |
Stream<Snippet> stream = argToSnippets(arg, true); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1372 |
if (stream == null) { |
36990 | 1373 |
errormsg("jshell.err.def.or.id.not.found", arg); |
1374 |
fluffmsg("jshell.msg.see.classes.etc"); |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1375 |
return false; |
33362 | 1376 |
} |
1377 |
Set<String> srcSet = new LinkedHashSet<>(); |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1378 |
stream.forEachOrdered(sn -> { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1379 |
String src = sn.source(); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1380 |
switch (sn.subKind()) { |
33362 | 1381 |
case VAR_VALUE_SUBKIND: |
1382 |
break; |
|
1383 |
case ASSIGNMENT_SUBKIND: |
|
1384 |
case OTHER_EXPRESSION_SUBKIND: |
|
1385 |
case TEMP_VAR_EXPRESSION_SUBKIND: |
|
1386 |
if (!src.endsWith(";")) { |
|
1387 |
src = src + ";"; |
|
1388 |
} |
|
1389 |
srcSet.add(src); |
|
1390 |
break; |
|
1391 |
default: |
|
1392 |
srcSet.add(src); |
|
1393 |
break; |
|
1394 |
} |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1395 |
}); |
33362 | 1396 |
StringBuilder sb = new StringBuilder(); |
1397 |
for (String s : srcSet) { |
|
1398 |
sb.append(s); |
|
1399 |
sb.append('\n'); |
|
1400 |
} |
|
1401 |
String src = sb.toString(); |
|
1402 |
Consumer<String> saveHandler = new SaveHandler(src, srcSet); |
|
1403 |
Consumer<String> errorHandler = s -> hard("Edit Error: %s", s); |
|
1404 |
if (editor == null) { |
|
37007
6023a9a9d58a
8153716: JShell tool: should warn when failed to launch editor
rfield
parents:
36992
diff
changeset
|
1405 |
try { |
6023a9a9d58a
8153716: JShell tool: should warn when failed to launch editor
rfield
parents:
36992
diff
changeset
|
1406 |
EditPad.edit(errorHandler, src, saveHandler); |
6023a9a9d58a
8153716: JShell tool: should warn when failed to launch editor
rfield
parents:
36992
diff
changeset
|
1407 |
} catch (RuntimeException ex) { |
6023a9a9d58a
8153716: JShell tool: should warn when failed to launch editor
rfield
parents:
36992
diff
changeset
|
1408 |
errormsg("jshell.err.cant.launch.editor", ex); |
6023a9a9d58a
8153716: JShell tool: should warn when failed to launch editor
rfield
parents:
36992
diff
changeset
|
1409 |
fluffmsg("jshell.msg.try.set.editor"); |
6023a9a9d58a
8153716: JShell tool: should warn when failed to launch editor
rfield
parents:
36992
diff
changeset
|
1410 |
return false; |
6023a9a9d58a
8153716: JShell tool: should warn when failed to launch editor
rfield
parents:
36992
diff
changeset
|
1411 |
} |
33362 | 1412 |
} else { |
1413 |
ExternalEditor.edit(editor, errorHandler, src, saveHandler, input); |
|
1414 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1415 |
return true; |
33362 | 1416 |
} |
1417 |
//where |
|
1418 |
// receives editor requests to save |
|
1419 |
private class SaveHandler implements Consumer<String> { |
|
1420 |
||
1421 |
String src; |
|
1422 |
Set<String> currSrcs; |
|
1423 |
||
1424 |
SaveHandler(String src, Set<String> ss) { |
|
1425 |
this.src = src; |
|
1426 |
this.currSrcs = ss; |
|
1427 |
} |
|
1428 |
||
1429 |
@Override |
|
1430 |
public void accept(String s) { |
|
1431 |
if (!s.equals(src)) { // quick check first |
|
1432 |
src = s; |
|
1433 |
try { |
|
1434 |
Set<String> nextSrcs = new LinkedHashSet<>(); |
|
1435 |
boolean failed = false; |
|
1436 |
while (true) { |
|
1437 |
CompletionInfo an = analysis.analyzeCompletion(s); |
|
1438 |
if (!an.completeness.isComplete) { |
|
1439 |
break; |
|
1440 |
} |
|
1441 |
String tsrc = trimNewlines(an.source); |
|
1442 |
if (!failed && !currSrcs.contains(tsrc)) { |
|
1443 |
failed = processCompleteSource(tsrc); |
|
1444 |
} |
|
1445 |
nextSrcs.add(tsrc); |
|
1446 |
if (an.remaining.isEmpty()) { |
|
1447 |
break; |
|
1448 |
} |
|
1449 |
s = an.remaining; |
|
1450 |
} |
|
1451 |
currSrcs = nextSrcs; |
|
1452 |
} catch (IllegalStateException ex) { |
|
36990 | 1453 |
hardmsg("jshell.msg.resetting"); |
33362 | 1454 |
resetState(); |
1455 |
currSrcs = new LinkedHashSet<>(); // re-process everything |
|
1456 |
} |
|
1457 |
} |
|
1458 |
} |
|
1459 |
||
1460 |
private String trimNewlines(String s) { |
|
1461 |
int b = 0; |
|
1462 |
while (b < s.length() && s.charAt(b) == '\n') { |
|
1463 |
++b; |
|
1464 |
} |
|
1465 |
int e = s.length() -1; |
|
1466 |
while (e >= 0 && s.charAt(e) == '\n') { |
|
1467 |
--e; |
|
1468 |
} |
|
1469 |
return s.substring(b, e + 1); |
|
1470 |
} |
|
1471 |
} |
|
1472 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1473 |
private boolean cmdList(String arg) { |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1474 |
if (arg.equals("history")) { |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1475 |
return cmdHistory(); |
33362 | 1476 |
} |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1477 |
Stream<Snippet> stream = argToSnippets(arg, true); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1478 |
if (stream == null) { |
36990 | 1479 |
errormsg("jshell.err.def.or.id.not.found", arg); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1480 |
// Check if there are any definitions at all |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1481 |
if (argToSnippets("", false).iterator().hasNext()) { |
36990 | 1482 |
fluffmsg("jshell.msg.try.list.without.args"); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1483 |
} else { |
36990 | 1484 |
hardmsg("jshell.msg.no.active"); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1485 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1486 |
return false; |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1487 |
} |
33362 | 1488 |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1489 |
// prevent double newline on empty list |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1490 |
boolean[] hasOutput = new boolean[1]; |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1491 |
stream.forEachOrdered(sn -> { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1492 |
if (!hasOutput[0]) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1493 |
cmdout.println(); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1494 |
hasOutput[0] = true; |
33362 | 1495 |
} |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1496 |
cmdout.printf("%4s : %s\n", sn.id(), sn.source().replace("\n", "\n ")); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1497 |
}); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1498 |
return true; |
33362 | 1499 |
} |
1500 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1501 |
private boolean cmdOpen(String filename) { |
37389 | 1502 |
return runFile(filename, "/open"); |
36990 | 1503 |
} |
1504 |
||
1505 |
private boolean runFile(String filename, String context) { |
|
1506 |
if (!filename.isEmpty()) { |
|
33362 | 1507 |
try { |
1508 |
run(new FileScannerIOContext(toPathResolvingUserHome(filename).toString())); |
|
36990 | 1509 |
return true; |
33362 | 1510 |
} catch (FileNotFoundException e) { |
36990 | 1511 |
errormsg("jshell.err.file.not.found", context, filename, e.getMessage()); |
33362 | 1512 |
} catch (Exception e) { |
36990 | 1513 |
errormsg("jshell.err.file.exception", context, filename, e); |
33362 | 1514 |
} |
36990 | 1515 |
} else { |
1516 |
errormsg("jshell.err.file.filename", context); |
|
33362 | 1517 |
} |
36990 | 1518 |
return false; |
1519 |
} |
|
1520 |
||
1521 |
/** |
|
1522 |
* Read an external file. Error messages accessed via keyPrefix |
|
1523 |
* |
|
1524 |
* @param filename file to access or null |
|
1525 |
* @param context printable non-natural language context for errors |
|
1526 |
* @return contents of file as string |
|
1527 |
*/ |
|
1528 |
String readFile(String filename, String context) { |
|
1529 |
if (filename != null) { |
|
1530 |
try { |
|
1531 |
byte[] encoded = Files.readAllBytes(Paths.get(filename)); |
|
1532 |
return new String(encoded); |
|
1533 |
} catch (AccessDeniedException e) { |
|
1534 |
errormsg("jshell.err.file.not.accessible", context, filename, e.getMessage()); |
|
1535 |
} catch (NoSuchFileException e) { |
|
37389 | 1536 |
errormsg("jshell.err.file.not.found", context, filename); |
36990 | 1537 |
} catch (Exception e) { |
1538 |
errormsg("jshell.err.file.exception", context, filename, e); |
|
1539 |
} |
|
1540 |
} else { |
|
1541 |
errormsg("jshell.err.file.filename", context); |
|
1542 |
} |
|
1543 |
return null; |
|
1544 |
||
33362 | 1545 |
} |
1546 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1547 |
private boolean cmdReset() { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1548 |
live = false; |
36990 | 1549 |
fluffmsg("jshell.msg.resetting.state"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1550 |
return true; |
33362 | 1551 |
} |
1552 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1553 |
private boolean cmdReload(String arg) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1554 |
Iterable<String> history = replayableHistory; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1555 |
boolean echo = true; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1556 |
if (arg.length() > 0) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1557 |
if ("restore".startsWith(arg)) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1558 |
if (replayableHistoryPrevious == null) { |
36990 | 1559 |
errormsg("jshell.err.reload.no.previous"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1560 |
return false; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1561 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1562 |
history = replayableHistoryPrevious; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1563 |
} else if ("quiet".startsWith(arg)) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1564 |
echo = false; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1565 |
} else { |
36990 | 1566 |
errormsg("jshell.err.arg", "/reload", arg); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1567 |
return false; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1568 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1569 |
} |
36990 | 1570 |
fluffmsg(history == replayableHistoryPrevious |
1571 |
? "jshell.err.reload.restarting.previous.state" |
|
1572 |
: "jshell.err.reload.restarting.state"); |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1573 |
resetState(); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1574 |
run(new ReloadIOContext(history, |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1575 |
echo? cmdout : null)); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1576 |
return true; |
33362 | 1577 |
} |
1578 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1579 |
private boolean cmdSave(String arg_filename) { |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1580 |
Matcher mat = HISTORY_ALL_START_FILENAME.matcher(arg_filename); |
33362 | 1581 |
if (!mat.find()) { |
36990 | 1582 |
errormsg("jshell.err.arg", arg_filename); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1583 |
return false; |
33362 | 1584 |
} |
1585 |
boolean useHistory = false; |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1586 |
String saveAll = ""; |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1587 |
boolean saveStart = false; |
33362 | 1588 |
String cmd = mat.group("cmd"); |
1589 |
if (cmd != null) switch (cmd) { |
|
1590 |
case "all": |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1591 |
saveAll = "all"; |
33362 | 1592 |
break; |
1593 |
case "history": |
|
1594 |
useHistory = true; |
|
1595 |
break; |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1596 |
case "start": |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1597 |
saveStart = true; |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1598 |
break; |
33362 | 1599 |
} |
1600 |
String filename = mat.group("filename"); |
|
1601 |
if (filename == null ||filename.isEmpty()) { |
|
36990 | 1602 |
errormsg("jshell.err.file.filename", "/save"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1603 |
return false; |
33362 | 1604 |
} |
1605 |
try (BufferedWriter writer = Files.newBufferedWriter(toPathResolvingUserHome(filename), |
|
1606 |
Charset.defaultCharset(), |
|
1607 |
CREATE, TRUNCATE_EXISTING, WRITE)) { |
|
1608 |
if (useHistory) { |
|
1609 |
for (String s : input.currentSessionHistory()) { |
|
1610 |
writer.write(s); |
|
1611 |
writer.write("\n"); |
|
1612 |
} |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1613 |
} else if (saveStart) { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1614 |
writer.append(DEFAULT_STARTUP); |
33362 | 1615 |
} else { |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1616 |
Stream<Snippet> stream = argToSnippets(saveAll, true); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1617 |
if (stream != null) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1618 |
for (Snippet sn : stream.collect(toList())) { |
33362 | 1619 |
writer.write(sn.source()); |
1620 |
writer.write("\n"); |
|
1621 |
} |
|
1622 |
} |
|
1623 |
} |
|
1624 |
} catch (FileNotFoundException e) { |
|
36990 | 1625 |
errormsg("jshell.err.file.not.found", "/save", filename, e.getMessage()); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1626 |
return false; |
33362 | 1627 |
} catch (Exception e) { |
36990 | 1628 |
errormsg("jshell.err.file.exception", "/save", filename, e); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1629 |
return false; |
33362 | 1630 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1631 |
return true; |
33362 | 1632 |
} |
1633 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1634 |
private boolean cmdVars() { |
33362 | 1635 |
for (VarSnippet vk : state.variables()) { |
1636 |
String val = state.status(vk) == Status.VALID |
|
1637 |
? state.varValue(vk) |
|
36990 | 1638 |
: "jshell.msg.vars.not.active"; |
33362 | 1639 |
hard(" %s %s = %s", vk.typeName(), vk.name(), val); |
1640 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1641 |
return true; |
33362 | 1642 |
} |
1643 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1644 |
private boolean cmdMethods() { |
33362 | 1645 |
for (MethodSnippet mk : state.methods()) { |
1646 |
hard(" %s %s", mk.name(), mk.signature()); |
|
1647 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1648 |
return true; |
33362 | 1649 |
} |
1650 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1651 |
private boolean cmdClasses() { |
33362 | 1652 |
for (TypeDeclSnippet ck : state.types()) { |
1653 |
String kind; |
|
1654 |
switch (ck.subKind()) { |
|
1655 |
case INTERFACE_SUBKIND: |
|
1656 |
kind = "interface"; |
|
1657 |
break; |
|
1658 |
case CLASS_SUBKIND: |
|
1659 |
kind = "class"; |
|
1660 |
break; |
|
1661 |
case ENUM_SUBKIND: |
|
1662 |
kind = "enum"; |
|
1663 |
break; |
|
1664 |
case ANNOTATION_TYPE_SUBKIND: |
|
1665 |
kind = "@interface"; |
|
1666 |
break; |
|
1667 |
default: |
|
1668 |
assert false : "Wrong kind" + ck.subKind(); |
|
1669 |
kind = "class"; |
|
1670 |
break; |
|
1671 |
} |
|
1672 |
hard(" %s %s", kind, ck.name()); |
|
1673 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1674 |
return true; |
33362 | 1675 |
} |
1676 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1677 |
private boolean cmdImports() { |
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
1678 |
state.imports().forEach(ik -> { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
1679 |
hard(" import %s%s", ik.isStatic() ? "static " : "", ik.fullname()); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
1680 |
}); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1681 |
return true; |
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
1682 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
1683 |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1684 |
private boolean cmdUseHistoryEntry(int index) { |
33362 | 1685 |
List<Snippet> keys = state.snippets(); |
1686 |
if (index < 0) |
|
1687 |
index += keys.size(); |
|
1688 |
else |
|
1689 |
index--; |
|
1690 |
if (index >= 0 && index < keys.size()) { |
|
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1691 |
rerunSnippet(keys.get(index)); |
33362 | 1692 |
} else { |
36990 | 1693 |
errormsg("jshell.err.out.of.range"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1694 |
return false; |
33362 | 1695 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1696 |
return true; |
33362 | 1697 |
} |
1698 |
||
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1699 |
private boolean rerunHistoryEntryById(String id) { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1700 |
Optional<Snippet> snippet = state.snippets().stream() |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1701 |
.filter(s -> s.id().equals(id)) |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1702 |
.findFirst(); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1703 |
return snippet.map(s -> { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1704 |
rerunSnippet(s); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1705 |
return true; |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1706 |
}).orElse(false); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1707 |
} |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1708 |
|
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1709 |
private void rerunSnippet(Snippet snippet) { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1710 |
String source = snippet.source(); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1711 |
cmdout.printf("%s\n", source); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1712 |
input.replaceLastHistoryEntry(source); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1713 |
processSourceCatchingReset(source); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1714 |
} |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1715 |
|
33362 | 1716 |
/** |
1717 |
* Filter diagnostics for only errors (no warnings, ...) |
|
1718 |
* @param diagnostics input list |
|
1719 |
* @return filtered list |
|
1720 |
*/ |
|
1721 |
List<Diag> errorsOnly(List<Diag> diagnostics) { |
|
1722 |
return diagnostics.stream() |
|
1723 |
.filter(d -> d.isError()) |
|
1724 |
.collect(toList()); |
|
1725 |
} |
|
1726 |
||
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1727 |
void displayDiagnostics(String source, Diag diag, List<String> toDisplay) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1728 |
for (String line : diag.getMessage(null).split("\\r?\\n")) { // TODO: Internationalize |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1729 |
if (!line.trim().startsWith("location:")) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1730 |
toDisplay.add(line); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1731 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1732 |
} |
33362 | 1733 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1734 |
int pstart = (int) diag.getStartPosition(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1735 |
int pend = (int) diag.getEndPosition(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1736 |
Matcher m = LINEBREAK.matcher(source); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1737 |
int pstartl = 0; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1738 |
int pendl = -2; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1739 |
while (m.find(pstartl)) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1740 |
pendl = m.start(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1741 |
if (pendl >= pstart) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1742 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1743 |
} else { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1744 |
pstartl = m.end(); |
33362 | 1745 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1746 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1747 |
if (pendl < pstart) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1748 |
pendl = source.length(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1749 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1750 |
toDisplay.add(source.substring(pstartl, pendl)); |
33362 | 1751 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1752 |
StringBuilder sb = new StringBuilder(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1753 |
int start = pstart - pstartl; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1754 |
for (int i = 0; i < start; ++i) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1755 |
sb.append(' '); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1756 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1757 |
sb.append('^'); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1758 |
boolean multiline = pend > pendl; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1759 |
int end = (multiline ? pendl : pend) - pstartl - 1; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1760 |
if (end > start) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1761 |
for (int i = start + 1; i < end; ++i) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1762 |
sb.append('-'); |
33362 | 1763 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1764 |
if (multiline) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1765 |
sb.append("-..."); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1766 |
} else { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1767 |
sb.append('^'); |
33362 | 1768 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1769 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1770 |
toDisplay.add(sb.toString()); |
33362 | 1771 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1772 |
debug("printDiagnostics start-pos = %d ==> %d -- wrap = %s", diag.getStartPosition(), start, this); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1773 |
debug("Code: %s", diag.getCode()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1774 |
debug("Pos: %d (%d - %d)", diag.getPosition(), |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1775 |
diag.getStartPosition(), diag.getEndPosition()); |
33362 | 1776 |
} |
1777 |
||
1778 |
private String processSource(String srcInput) throws IllegalStateException { |
|
1779 |
while (true) { |
|
1780 |
CompletionInfo an = analysis.analyzeCompletion(srcInput); |
|
1781 |
if (!an.completeness.isComplete) { |
|
1782 |
return an.remaining; |
|
1783 |
} |
|
1784 |
boolean failed = processCompleteSource(an.source); |
|
1785 |
if (failed || an.remaining.isEmpty()) { |
|
1786 |
return ""; |
|
1787 |
} |
|
1788 |
srcInput = an.remaining; |
|
1789 |
} |
|
1790 |
} |
|
1791 |
//where |
|
1792 |
private boolean processCompleteSource(String source) throws IllegalStateException { |
|
1793 |
debug("Compiling: %s", source); |
|
1794 |
boolean failed = false; |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1795 |
boolean isActive = false; |
33362 | 1796 |
List<SnippetEvent> events = state.eval(source); |
1797 |
for (SnippetEvent e : events) { |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1798 |
// Report the event, recording failure |
33362 | 1799 |
failed |= handleEvent(e); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1800 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1801 |
// If any main snippet is active, this should be replayable |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1802 |
// also ignore var value queries |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1803 |
isActive |= e.causeSnippet() == null && |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1804 |
e.status().isActive && |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1805 |
e.snippet().subKind() != VAR_VALUE_SUBKIND; |
33362 | 1806 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1807 |
// If this is an active snippet and it didn't cause the backend to die, |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1808 |
// add it to the replayable history |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1809 |
if (isActive && live) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1810 |
addToReplayHistory(source); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1811 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1812 |
|
33362 | 1813 |
return failed; |
1814 |
} |
|
1815 |
||
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1816 |
// Handle incoming snippet events -- return true on failure |
33362 | 1817 |
private boolean handleEvent(SnippetEvent ste) { |
1818 |
Snippet sn = ste.snippet(); |
|
1819 |
if (sn == null) { |
|
1820 |
debug("Event with null key: %s", ste); |
|
1821 |
return false; |
|
1822 |
} |
|
1823 |
List<Diag> diagnostics = state.diagnostics(sn); |
|
1824 |
String source = sn.source(); |
|
1825 |
if (ste.causeSnippet() == null) { |
|
1826 |
// main event |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1827 |
for (Diag d : diagnostics) { |
36990 | 1828 |
hardmsg(d.isError()? "jshell.msg.error" : "jshell.msg.warning"); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1829 |
List<String> disp = new ArrayList<>(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1830 |
displayDiagnostics(source, d, disp); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1831 |
disp.stream() |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1832 |
.forEach(l -> hard(l)); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1833 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1834 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1835 |
if (ste.status() != Status.REJECTED) { |
33362 | 1836 |
if (ste.exception() != null) { |
1837 |
if (ste.exception() instanceof EvalException) { |
|
1838 |
printEvalException((EvalException) ste.exception()); |
|
1839 |
return true; |
|
1840 |
} else if (ste.exception() instanceof UnresolvedReferenceException) { |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1841 |
printUnresolvedException((UnresolvedReferenceException) ste.exception()); |
33362 | 1842 |
} else { |
1843 |
hard("Unexpected execution exception: %s", ste.exception()); |
|
1844 |
return true; |
|
1845 |
} |
|
1846 |
} else { |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1847 |
new DisplayEvent(ste, false, ste.value(), diagnostics).displayDeclarationAndValue(); |
33362 | 1848 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1849 |
} else { |
33362 | 1850 |
if (diagnostics.isEmpty()) { |
36990 | 1851 |
errormsg("jshell.err.failed"); |
33362 | 1852 |
} |
1853 |
return true; |
|
1854 |
} |
|
1855 |
} else { |
|
1856 |
// Update |
|
36494 | 1857 |
if (sn instanceof DeclarationSnippet) { |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1858 |
List<Diag> other = errorsOnly(diagnostics); |
36494 | 1859 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1860 |
// display update information |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1861 |
new DisplayEvent(ste, true, ste.value(), other).displayDeclarationAndValue(); |
33362 | 1862 |
} |
1863 |
} |
|
1864 |
return false; |
|
1865 |
} |
|
1866 |
//where |
|
1867 |
void printStackTrace(StackTraceElement[] stes) { |
|
1868 |
for (StackTraceElement ste : stes) { |
|
1869 |
StringBuilder sb = new StringBuilder(); |
|
1870 |
String cn = ste.getClassName(); |
|
1871 |
if (!cn.isEmpty()) { |
|
1872 |
int dot = cn.lastIndexOf('.'); |
|
1873 |
if (dot > 0) { |
|
1874 |
sb.append(cn.substring(dot + 1)); |
|
1875 |
} else { |
|
1876 |
sb.append(cn); |
|
1877 |
} |
|
1878 |
sb.append("."); |
|
1879 |
} |
|
1880 |
if (!ste.getMethodName().isEmpty()) { |
|
1881 |
sb.append(ste.getMethodName()); |
|
1882 |
sb.append(" "); |
|
1883 |
} |
|
1884 |
String fileName = ste.getFileName(); |
|
1885 |
int lineNumber = ste.getLineNumber(); |
|
1886 |
String loc = ste.isNativeMethod() |
|
36990 | 1887 |
? getResourceString("jshell.msg.native.method") |
33362 | 1888 |
: fileName == null |
36990 | 1889 |
? getResourceString("jshell.msg.unknown.source") |
33362 | 1890 |
: lineNumber >= 0 |
1891 |
? fileName + ":" + lineNumber |
|
1892 |
: fileName; |
|
1893 |
hard(" at %s(%s)", sb, loc); |
|
1894 |
||
1895 |
} |
|
1896 |
} |
|
1897 |
//where |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1898 |
void printUnresolvedException(UnresolvedReferenceException ex) { |
36499 | 1899 |
DeclarationSnippet corralled = ex.getSnippet(); |
33362 | 1900 |
List<Diag> otherErrors = errorsOnly(state.diagnostics(corralled)); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1901 |
new DisplayEvent(corralled, state.status(corralled), FormatAction.USED, true, null, otherErrors) |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1902 |
.displayDeclarationAndValue(); |
33362 | 1903 |
} |
1904 |
//where |
|
1905 |
void printEvalException(EvalException ex) { |
|
1906 |
if (ex.getMessage() == null) { |
|
1907 |
hard("%s thrown", ex.getExceptionClassName()); |
|
1908 |
} else { |
|
1909 |
hard("%s thrown: %s", ex.getExceptionClassName(), ex.getMessage()); |
|
1910 |
} |
|
1911 |
printStackTrace(ex.getStackTrace()); |
|
1912 |
} |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1913 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1914 |
private FormatAction toAction(Status status, Status previousStatus, boolean isSignatureChange) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1915 |
FormatAction act; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1916 |
switch (status) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1917 |
case VALID: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1918 |
case RECOVERABLE_DEFINED: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1919 |
case RECOVERABLE_NOT_DEFINED: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1920 |
if (previousStatus.isActive) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1921 |
act = isSignatureChange |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1922 |
? FormatAction.REPLACED |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1923 |
: FormatAction.MODIFIED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1924 |
} else { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1925 |
act = FormatAction.ADDED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1926 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1927 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1928 |
case OVERWRITTEN: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1929 |
act = FormatAction.OVERWROTE; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1930 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1931 |
case DROPPED: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1932 |
act = FormatAction.DROPPED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1933 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1934 |
case REJECTED: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1935 |
case NONEXISTENT: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1936 |
default: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1937 |
// Should not occur |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1938 |
error("Unexpected status: " + previousStatus.toString() + "=>" + status.toString()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1939 |
act = FormatAction.DROPPED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1940 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1941 |
return act; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1942 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1943 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1944 |
class DisplayEvent { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1945 |
private final Snippet sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1946 |
private final FormatAction action; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1947 |
private final boolean update; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1948 |
private final String value; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1949 |
private final List<String> errorLines; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1950 |
private final FormatResolve resolution; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1951 |
private final String unresolved; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1952 |
private final FormatUnresolved unrcnt; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1953 |
private final FormatErrors errcnt; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1954 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1955 |
DisplayEvent(SnippetEvent ste, boolean update, String value, List<Diag> errors) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1956 |
this(ste.snippet(), ste.status(), toAction(ste.status(), ste.previousStatus(), ste.isSignatureChange()), update, value, errors); |
33362 | 1957 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1958 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1959 |
DisplayEvent(Snippet sn, Status status, FormatAction action, boolean update, String value, List<Diag> errors) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1960 |
this.sn = sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1961 |
this.action = action; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1962 |
this.update = update; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1963 |
this.value = value; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1964 |
this.errorLines = new ArrayList<>(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1965 |
for (Diag d : errors) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1966 |
displayDiagnostics(sn.source(), d, errorLines); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1967 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1968 |
int unresolvedCount; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1969 |
if (sn instanceof DeclarationSnippet && (status == Status.RECOVERABLE_DEFINED || status == Status.RECOVERABLE_NOT_DEFINED)) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1970 |
resolution = (status == Status.RECOVERABLE_NOT_DEFINED) |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1971 |
? FormatResolve.NOTDEFINED |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1972 |
: FormatResolve.DEFINED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1973 |
unresolved = unresolved((DeclarationSnippet) sn); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1974 |
unresolvedCount = state.unresolvedDependencies((DeclarationSnippet) sn).size(); |
33362 | 1975 |
} else { |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1976 |
resolution = FormatResolve.OK; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1977 |
unresolved = ""; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1978 |
unresolvedCount = 0; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1979 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1980 |
unrcnt = unresolvedCount == 0 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1981 |
? FormatUnresolved.UNRESOLVED0 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1982 |
: unresolvedCount == 1 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1983 |
? FormatUnresolved.UNRESOLVED1 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1984 |
: FormatUnresolved.UNRESOLVED2; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1985 |
errcnt = errors.isEmpty() |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1986 |
? FormatErrors.ERROR0 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1987 |
: errors.size() == 1 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1988 |
? FormatErrors.ERROR1 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1989 |
: FormatErrors.ERROR2; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1990 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1991 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1992 |
private String unresolved(DeclarationSnippet key) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1993 |
List<String> unr = state.unresolvedDependencies(key); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1994 |
StringBuilder sb = new StringBuilder(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1995 |
int fromLast = unr.size(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1996 |
if (fromLast > 0) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1997 |
sb.append(" "); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1998 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1999 |
for (String u : unr) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2000 |
--fromLast; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2001 |
sb.append(u); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2002 |
switch (fromLast) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2003 |
// No suffix |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2004 |
case 0: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2005 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2006 |
case 1: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2007 |
sb.append(", and "); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2008 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2009 |
default: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2010 |
sb.append(", "); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2011 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2012 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2013 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2014 |
return sb.toString(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2015 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2016 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2017 |
private void custom(FormatCase fcase, String name) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2018 |
custom(fcase, name, null); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2019 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2020 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2021 |
private void custom(FormatCase fcase, String name, String type) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2022 |
String display = feedback.format(fcase, action, (update ? FormatWhen.UPDATE : FormatWhen.PRIMARY), |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2023 |
resolution, unrcnt, errcnt, |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2024 |
name, type, value, unresolved, errorLines); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2025 |
if (interactive()) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2026 |
cmdout.print(display); |
33362 | 2027 |
} |
2028 |
} |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2029 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2030 |
@SuppressWarnings("fallthrough") |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2031 |
private void displayDeclarationAndValue() { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2032 |
switch (sn.subKind()) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2033 |
case CLASS_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2034 |
custom(FormatCase.CLASS, ((TypeDeclSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2035 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2036 |
case INTERFACE_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2037 |
custom(FormatCase.INTERFACE, ((TypeDeclSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2038 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2039 |
case ENUM_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2040 |
custom(FormatCase.ENUM, ((TypeDeclSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2041 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2042 |
case ANNOTATION_TYPE_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2043 |
custom(FormatCase.ANNOTATION, ((TypeDeclSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2044 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2045 |
case METHOD_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2046 |
custom(FormatCase.METHOD, ((MethodSnippet) sn).name(), ((MethodSnippet) sn).parameterTypes()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2047 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2048 |
case VAR_DECLARATION_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2049 |
VarSnippet vk = (VarSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2050 |
custom(FormatCase.VARDECL, vk.name(), vk.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2051 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2052 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2053 |
case VAR_DECLARATION_WITH_INITIALIZER_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2054 |
VarSnippet vk = (VarSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2055 |
custom(FormatCase.VARINIT, vk.name(), vk.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2056 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2057 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2058 |
case TEMP_VAR_EXPRESSION_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2059 |
VarSnippet vk = (VarSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2060 |
custom(FormatCase.EXPRESSION, vk.name(), vk.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2061 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2062 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2063 |
case OTHER_EXPRESSION_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2064 |
error("Unexpected expression form -- value is: %s", (value)); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2065 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2066 |
case VAR_VALUE_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2067 |
ExpressionSnippet ek = (ExpressionSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2068 |
custom(FormatCase.VARVALUE, ek.name(), ek.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2069 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2070 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2071 |
case ASSIGNMENT_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2072 |
ExpressionSnippet ek = (ExpressionSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2073 |
custom(FormatCase.ASSIGNMENT, ek.name(), ek.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2074 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2075 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2076 |
case SINGLE_TYPE_IMPORT_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2077 |
case TYPE_IMPORT_ON_DEMAND_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2078 |
case SINGLE_STATIC_IMPORT_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2079 |
case STATIC_IMPORT_ON_DEMAND_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2080 |
custom(FormatCase.IMPORT, ((ImportSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2081 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2082 |
case STATEMENT_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2083 |
custom(FormatCase.STATEMENT, null); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2084 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2085 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
2086 |
} |
33362 | 2087 |
} |
2088 |
||
2089 |
/** The current version number as a string. |
|
2090 |
*/ |
|
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
2091 |
String version() { |
33362 | 2092 |
return version("release"); // mm.nn.oo[-milestone] |
2093 |
} |
|
2094 |
||
2095 |
/** The current full version number as a string. |
|
2096 |
*/ |
|
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
2097 |
String fullVersion() { |
33362 | 2098 |
return version("full"); // mm.mm.oo[-milestone]-build |
2099 |
} |
|
2100 |
||
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
2101 |
private String version(String key) { |
33362 | 2102 |
if (versionRB == null) { |
2103 |
try { |
|
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
2104 |
versionRB = ResourceBundle.getBundle(VERSION_RB_NAME, locale); |
33362 | 2105 |
} catch (MissingResourceException e) { |
2106 |
return "(version info not available)"; |
|
2107 |
} |
|
2108 |
} |
|
2109 |
try { |
|
2110 |
return versionRB.getString(key); |
|
2111 |
} |
|
2112 |
catch (MissingResourceException e) { |
|
2113 |
return "(version info not available)"; |
|
2114 |
} |
|
2115 |
} |
|
2116 |
||
2117 |
class NameSpace { |
|
2118 |
final String spaceName; |
|
2119 |
final String prefix; |
|
2120 |
private int nextNum; |
|
2121 |
||
2122 |
NameSpace(String spaceName, String prefix) { |
|
2123 |
this.spaceName = spaceName; |
|
2124 |
this.prefix = prefix; |
|
2125 |
this.nextNum = 1; |
|
2126 |
} |
|
2127 |
||
2128 |
String tid(Snippet sn) { |
|
2129 |
String tid = prefix + nextNum++; |
|
2130 |
mapSnippet.put(sn, new SnippetInfo(sn, this, tid)); |
|
2131 |
return tid; |
|
2132 |
} |
|
2133 |
||
2134 |
String tidNext() { |
|
2135 |
return prefix + nextNum; |
|
2136 |
} |
|
2137 |
} |
|
2138 |
||
2139 |
static class SnippetInfo { |
|
2140 |
final Snippet snippet; |
|
2141 |
final NameSpace space; |
|
2142 |
final String tid; |
|
2143 |
||
2144 |
SnippetInfo(Snippet snippet, NameSpace space, String tid) { |
|
2145 |
this.snippet = snippet; |
|
2146 |
this.space = space; |
|
2147 |
this.tid = tid; |
|
2148 |
} |
|
2149 |
} |
|
2150 |
} |
|
2151 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2152 |
abstract class NonInteractiveIOContext extends IOContext { |
33362 | 2153 |
|
2154 |
@Override |
|
2155 |
public boolean interactiveOutput() { |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2156 |
return false; |
33362 | 2157 |
} |
2158 |
||
2159 |
@Override |
|
2160 |
public Iterable<String> currentSessionHistory() { |
|
2161 |
return Collections.emptyList(); |
|
2162 |
} |
|
2163 |
||
2164 |
@Override |
|
2165 |
public boolean terminalEditorRunning() { |
|
2166 |
return false; |
|
2167 |
} |
|
2168 |
||
2169 |
@Override |
|
2170 |
public void suspend() { |
|
2171 |
} |
|
2172 |
||
2173 |
@Override |
|
2174 |
public void resume() { |
|
2175 |
} |
|
2176 |
||
2177 |
@Override |
|
2178 |
public void beforeUserCode() { |
|
2179 |
} |
|
2180 |
||
2181 |
@Override |
|
2182 |
public void afterUserCode() { |
|
2183 |
} |
|
2184 |
||
2185 |
@Override |
|
2186 |
public void replaceLastHistoryEntry(String source) { |
|
2187 |
} |
|
2188 |
} |
|
2189 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2190 |
class ScannerIOContext extends NonInteractiveIOContext { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2191 |
private final Scanner scannerIn; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2192 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2193 |
ScannerIOContext(Scanner scannerIn) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2194 |
this.scannerIn = scannerIn; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2195 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2196 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2197 |
@Override |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2198 |
public String readLine(String prompt, String prefix) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2199 |
if (scannerIn.hasNextLine()) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2200 |
return scannerIn.nextLine(); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2201 |
} else { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2202 |
return null; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2203 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2204 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2205 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2206 |
@Override |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2207 |
public void close() { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2208 |
scannerIn.close(); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2209 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2210 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2211 |
|
33362 | 2212 |
class FileScannerIOContext extends ScannerIOContext { |
2213 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2214 |
FileScannerIOContext(String fn) throws FileNotFoundException { |
33362 | 2215 |
this(new FileReader(fn)); |
2216 |
} |
|
2217 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2218 |
FileScannerIOContext(Reader rdr) throws FileNotFoundException { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2219 |
super(new Scanner(rdr)); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2220 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2221 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2222 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2223 |
class ReloadIOContext extends NonInteractiveIOContext { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2224 |
private final Iterator<String> it; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2225 |
private final PrintStream echoStream; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2226 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2227 |
ReloadIOContext(Iterable<String> history, PrintStream echoStream) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2228 |
this.it = history.iterator(); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2229 |
this.echoStream = echoStream; |
33362 | 2230 |
} |
2231 |
||
2232 |
@Override |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2233 |
public String readLine(String prompt, String prefix) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2234 |
String s = it.hasNext() |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2235 |
? it.next() |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2236 |
: null; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2237 |
if (echoStream != null && s != null) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2238 |
String p = "-: "; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2239 |
String p2 = "\n "; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2240 |
echoStream.printf("%s%s\n", p, s.replace("\n", p2)); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2241 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2242 |
return s; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2243 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2244 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2245 |
@Override |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2246 |
public void close() { |
33362 | 2247 |
} |
2248 |
} |