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