author | rfield |
Tue, 03 Apr 2018 13:27:58 -0700 | |
changeset 49515 | 083318155ad1 |
parent 48939 | ba545e52b932 |
child 49856 | 5f63af8f9d7f |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
48349
diff
changeset
|
2 |
* Copyright (c) 2014, 2018, 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 |
||
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
28 |
import java.io.BufferedReader; |
33362 | 29 |
import java.io.BufferedWriter; |
47840 | 30 |
import java.io.EOFException; |
33362 | 31 |
import java.io.File; |
32 |
import java.io.FileNotFoundException; |
|
33 |
import java.io.FileReader; |
|
34 |
import java.io.IOException; |
|
35 |
import java.io.InputStream; |
|
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
36 |
import java.io.InputStreamReader; |
33362 | 37 |
import java.io.PrintStream; |
38 |
import java.io.Reader; |
|
39 |
import java.io.StringReader; |
|
47837
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
40 |
import java.lang.module.ModuleDescriptor; |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
41 |
import java.lang.module.ModuleFinder; |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
42 |
import java.lang.module.ModuleReference; |
33362 | 43 |
import java.nio.charset.Charset; |
44 |
import java.nio.file.FileSystems; |
|
45 |
import java.nio.file.Files; |
|
48259
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
46 |
import java.nio.file.InvalidPathException; |
33362 | 47 |
import java.nio.file.Path; |
48 |
import java.nio.file.Paths; |
|
36990 | 49 |
import java.text.MessageFormat; |
33362 | 50 |
import java.util.ArrayList; |
51 |
import java.util.Arrays; |
|
43038 | 52 |
import java.util.Collection; |
33362 | 53 |
import java.util.Collections; |
43038 | 54 |
import java.util.HashMap; |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
55 |
import java.util.HashSet; |
33362 | 56 |
import java.util.Iterator; |
57 |
import java.util.LinkedHashMap; |
|
58 |
import java.util.LinkedHashSet; |
|
59 |
import java.util.List; |
|
36990 | 60 |
import java.util.Locale; |
33362 | 61 |
import java.util.Map; |
62 |
import java.util.Map.Entry; |
|
47837
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
63 |
import java.util.Optional; |
33362 | 64 |
import java.util.Scanner; |
65 |
import java.util.Set; |
|
66 |
import java.util.function.Consumer; |
|
67 |
import java.util.function.Predicate; |
|
68 |
import java.util.prefs.Preferences; |
|
69 |
import java.util.regex.Matcher; |
|
70 |
import java.util.regex.Pattern; |
|
71 |
import java.util.stream.Collectors; |
|
72 |
import java.util.stream.Stream; |
|
73 |
import java.util.stream.StreamSupport; |
|
74 |
||
75 |
import jdk.internal.jshell.debug.InternalDebugControl; |
|
76 |
import jdk.internal.jshell.tool.IOContext.InputInterruptedException; |
|
36494 | 77 |
import jdk.jshell.DeclarationSnippet; |
33362 | 78 |
import jdk.jshell.Diag; |
79 |
import jdk.jshell.EvalException; |
|
36494 | 80 |
import jdk.jshell.ExpressionSnippet; |
81 |
import jdk.jshell.ImportSnippet; |
|
33362 | 82 |
import jdk.jshell.JShell; |
36494 | 83 |
import jdk.jshell.JShell.Subscription; |
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
84 |
import jdk.jshell.JShellException; |
33362 | 85 |
import jdk.jshell.MethodSnippet; |
36494 | 86 |
import jdk.jshell.Snippet; |
47840 | 87 |
import jdk.jshell.Snippet.Kind; |
33362 | 88 |
import jdk.jshell.Snippet.Status; |
36494 | 89 |
import jdk.jshell.SnippetEvent; |
33362 | 90 |
import jdk.jshell.SourceCodeAnalysis; |
91 |
import jdk.jshell.SourceCodeAnalysis.CompletionInfo; |
|
47840 | 92 |
import jdk.jshell.SourceCodeAnalysis.Completeness; |
33362 | 93 |
import jdk.jshell.SourceCodeAnalysis.Suggestion; |
36494 | 94 |
import jdk.jshell.TypeDeclSnippet; |
33362 | 95 |
import jdk.jshell.UnresolvedReferenceException; |
36494 | 96 |
import jdk.jshell.VarSnippet; |
33362 | 97 |
|
98 |
import static java.nio.file.StandardOpenOption.CREATE; |
|
99 |
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; |
|
100 |
import static java.nio.file.StandardOpenOption.WRITE; |
|
47931 | 101 |
import java.util.AbstractMap.SimpleEntry; |
33362 | 102 |
import java.util.MissingResourceException; |
103 |
import java.util.ResourceBundle; |
|
41934
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
104 |
import java.util.ServiceLoader; |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
105 |
import java.util.Spliterators; |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
106 |
import java.util.function.Function; |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
107 |
import java.util.function.Supplier; |
40588
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
40516
diff
changeset
|
108 |
import jdk.internal.joptsimple.*; |
36494 | 109 |
import jdk.internal.jshell.tool.Feedback.FormatAction; |
110 |
import jdk.internal.jshell.tool.Feedback.FormatCase; |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
111 |
import jdk.internal.jshell.tool.Feedback.FormatErrors; |
36494 | 112 |
import jdk.internal.jshell.tool.Feedback.FormatResolve; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
113 |
import jdk.internal.jshell.tool.Feedback.FormatUnresolved; |
36494 | 114 |
import jdk.internal.jshell.tool.Feedback.FormatWhen; |
41934
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
115 |
import jdk.internal.editor.spi.BuildInEditorProvider; |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
116 |
import jdk.internal.editor.external.ExternalEditor; |
40588
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
40516
diff
changeset
|
117 |
import static java.util.Arrays.asList; |
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
40516
diff
changeset
|
118 |
import static java.util.Arrays.stream; |
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
119 |
import static java.util.Collections.singletonList; |
40588
b5c32bfa9710
8160089: jshell tool: use new double-dash long-form command-line options
rfield
parents:
40516
diff
changeset
|
120 |
import static java.util.stream.Collectors.joining; |
33362 | 121 |
import static java.util.stream.Collectors.toList; |
47840 | 122 |
import static jdk.jshell.Snippet.SubKind.TEMP_VAR_EXPRESSION_SUBKIND; |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
123 |
import static jdk.jshell.Snippet.SubKind.VAR_VALUE_SUBKIND; |
36494 | 124 |
import static java.util.stream.Collectors.toMap; |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
125 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_COMPA; |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
126 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_DEP; |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
127 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_EVNT; |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
128 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_FMGR; |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
129 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN; |
43134
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
43038
diff
changeset
|
130 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_WRAP; |
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
131 |
import static jdk.internal.jshell.tool.ContinuousCompletionProvider.STARTSWITH_MATCHER; |
33362 | 132 |
|
133 |
/** |
|
134 |
* Command line REPL tool for Java using the JShell API. |
|
135 |
* @author Robert Field |
|
136 |
*/ |
|
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
137 |
public class JShellTool implements MessageHandler { |
33362 | 138 |
|
139 |
private static final Pattern LINEBREAK = Pattern.compile("\\R"); |
|
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
140 |
private static final Pattern ID = Pattern.compile("[se]?\\d+([-\\s].*)?"); |
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
141 |
private static final Pattern RERUN_ID = Pattern.compile("/" + ID.pattern()); |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
142 |
private static final Pattern RERUN_PREVIOUS = Pattern.compile("/\\-\\d+( .*)?"); |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
143 |
private static final Pattern SET_SUB = Pattern.compile("/?set .*"); |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
144 |
static final String RECORD_SEPARATOR = "\u241E"; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
145 |
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
|
146 |
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
|
147 |
private static final String L10N_RB_NAME = RB_NAME_PREFIX + ".l10n"; |
33362 | 148 |
|
149 |
final InputStream cmdin; |
|
150 |
final PrintStream cmdout; |
|
151 |
final PrintStream cmderr; |
|
152 |
final PrintStream console; |
|
153 |
final InputStream userin; |
|
154 |
final PrintStream userout; |
|
155 |
final PrintStream usererr; |
|
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
156 |
final PersistentStorage prefs; |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
157 |
final Map<String, String> envvars; |
36990 | 158 |
final Locale locale; |
33362 | 159 |
|
36494 | 160 |
final Feedback feedback = new Feedback(); |
161 |
||
33362 | 162 |
/** |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
163 |
* The complete constructor for the tool (used by test harnesses). |
33362 | 164 |
* @param cmdin command line input -- snippets and commands |
165 |
* @param cmdout command line output, feedback including errors |
|
166 |
* @param cmderr start-up errors and debugging info |
|
167 |
* @param console console control interaction |
|
40767 | 168 |
* @param userin code execution input, or null to use IOContext |
33362 | 169 |
* @param userout code execution output -- System.out.printf("hi") |
170 |
* @param usererr code execution error stream -- System.err.printf("Oops") |
|
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
171 |
* @param prefs persistence implementation to use |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
172 |
* @param envvars environment variable mapping to use |
36990 | 173 |
* @param locale locale to use |
33362 | 174 |
*/ |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
175 |
JShellTool(InputStream cmdin, PrintStream cmdout, PrintStream cmderr, |
33362 | 176 |
PrintStream console, |
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36499
diff
changeset
|
177 |
InputStream userin, PrintStream userout, PrintStream usererr, |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
178 |
PersistentStorage prefs, Map<String, String> envvars, Locale locale) { |
33362 | 179 |
this.cmdin = cmdin; |
180 |
this.cmdout = cmdout; |
|
181 |
this.cmderr = cmderr; |
|
182 |
this.console = console; |
|
40767 | 183 |
this.userin = userin != null ? userin : new InputStream() { |
184 |
@Override |
|
185 |
public int read() throws IOException { |
|
186 |
return input.readUserInput(); |
|
187 |
} |
|
188 |
}; |
|
33362 | 189 |
this.userout = userout; |
190 |
this.usererr = usererr; |
|
36715
ae6fa9280e0b
8152296: langtools/test/jdk/jshell/ToolReloadTest.java failing if there is not persisted history
jlahoda
parents:
36499
diff
changeset
|
191 |
this.prefs = prefs; |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
192 |
this.envvars = envvars; |
36990 | 193 |
this.locale = locale; |
33362 | 194 |
} |
195 |
||
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
196 |
private ResourceBundle versionRB = null; |
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
197 |
private ResourceBundle outputRB = null; |
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
198 |
|
33362 | 199 |
private IOContext input = null; |
200 |
private boolean regenerateOnDeath = true; |
|
201 |
private boolean live = false; |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
202 |
private boolean interactiveModeBegun = false; |
43038 | 203 |
private Options options; |
33362 | 204 |
|
205 |
SourceCodeAnalysis analysis; |
|
44188
3f2047e62102
8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents:
44065
diff
changeset
|
206 |
private JShell state = null; |
33362 | 207 |
Subscription shutdownSubscription = null; |
208 |
||
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
209 |
static final EditorSetting BUILT_IN_EDITOR = new EditorSetting(null, false); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
210 |
|
33362 | 211 |
private boolean debug = false; |
212 |
public boolean testPrompt = false; |
|
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
213 |
private Startup startup = null; |
44065
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
214 |
private boolean isCurrentlyRunningStartup = false; |
42969
a48d4f74d322
8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents:
42843
diff
changeset
|
215 |
private String executionControlSpec = null; |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
216 |
private EditorSetting editor = BUILT_IN_EDITOR; |
47840 | 217 |
private int exitCode = 0; |
33362 | 218 |
|
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
219 |
private static final String[] EDITOR_ENV_VARS = new String[] { |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
220 |
"JSHELLEDITOR", "VISUAL", "EDITOR"}; |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
221 |
|
43564 | 222 |
// Commands and snippets which can be replayed |
223 |
private ReplayableHistory replayableHistory; |
|
224 |
private ReplayableHistory replayableHistoryPrevious; |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
225 |
|
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
226 |
static final String STARTUP_KEY = "STARTUP"; |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
227 |
static final String EDITOR_KEY = "EDITOR"; |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
228 |
static final String FEEDBACK_KEY = "FEEDBACK"; |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
229 |
static final String MODE_KEY = "MODE"; |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
230 |
static final String REPLAY_RESTORE_KEY = "REPLAY_RESTORE"; |
33362 | 231 |
|
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
232 |
static final Pattern BUILTIN_FILE_PATTERN = Pattern.compile("\\w+"); |
43136
24d62ba7ad5e
8172414: jshell not working in exploded JDK build
jlahoda
parents:
43134
diff
changeset
|
233 |
static final String BUILTIN_FILE_PATH_FORMAT = "/jdk/jshell/tool/resources/%s.jsh"; |
47840 | 234 |
static final String INT_PREFIX = "int $$exit$$ = "; |
33362 | 235 |
|
48273
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
236 |
static final int OUTPUT_WIDTH = 72; |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
237 |
|
43038 | 238 |
// match anything followed by whitespace |
239 |
private static final Pattern OPTION_PRE_PATTERN = |
|
240 |
Pattern.compile("\\s*(\\S+\\s+)*?"); |
|
241 |
// match a (possibly incomplete) option flag with optional double-dash and/or internal dashes |
|
242 |
private static final Pattern OPTION_PATTERN = |
|
243 |
Pattern.compile(OPTION_PRE_PATTERN.pattern() + "(?<dd>-??)(?<flag>-([a-z][a-z\\-]*)?)"); |
|
244 |
// match an option flag and a (possibly missing or incomplete) value |
|
245 |
private static final Pattern OPTION_VALUE_PATTERN = |
|
246 |
Pattern.compile(OPTION_PATTERN.pattern() + "\\s+(?<val>\\S*)"); |
|
247 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
248 |
// Tool id (tid) mapping: the three name spaces |
33362 | 249 |
NameSpace mainNamespace; |
250 |
NameSpace startNamespace; |
|
251 |
NameSpace errorNamespace; |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
252 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
253 |
// Tool id (tid) mapping: the current name spaces |
33362 | 254 |
NameSpace currentNameSpace; |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
255 |
|
43038 | 256 |
Map<Snippet, SnippetInfo> mapSnippet; |
257 |
||
258 |
// Kinds of compiler/runtime init options |
|
259 |
private enum OptionKind { |
|
260 |
CLASS_PATH("--class-path", true), |
|
261 |
MODULE_PATH("--module-path", true), |
|
262 |
ADD_MODULES("--add-modules", false), |
|
263 |
ADD_EXPORTS("--add-exports", false), |
|
264 |
TO_COMPILER("-C", false, false, true, false), |
|
265 |
TO_REMOTE_VM("-R", false, false, false, true),; |
|
266 |
final String optionFlag; |
|
267 |
final boolean onlyOne; |
|
268 |
final boolean passFlag; |
|
269 |
final boolean toCompiler; |
|
270 |
final boolean toRemoteVm; |
|
271 |
||
272 |
private OptionKind(String optionFlag, boolean onlyOne) { |
|
273 |
this(optionFlag, onlyOne, true, true, true); |
|
274 |
} |
|
275 |
||
276 |
private OptionKind(String optionFlag, boolean onlyOne, boolean passFlag, |
|
277 |
boolean toCompiler, boolean toRemoteVm) { |
|
278 |
this.optionFlag = optionFlag; |
|
279 |
this.onlyOne = onlyOne; |
|
280 |
this.passFlag = passFlag; |
|
281 |
this.toCompiler = toCompiler; |
|
282 |
this.toRemoteVm = toRemoteVm; |
|
283 |
} |
|
284 |
||
285 |
} |
|
286 |
||
287 |
// compiler/runtime init option values |
|
288 |
private static class Options { |
|
289 |
||
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
290 |
private final Map<OptionKind, List<String>> optMap; |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
291 |
|
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
292 |
// New blank Options |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
293 |
Options() { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
294 |
optMap = new HashMap<>(); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
295 |
} |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
296 |
|
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
297 |
// Options as a copy |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
298 |
private Options(Options opts) { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
299 |
optMap = new HashMap<>(opts.optMap); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
300 |
} |
43038 | 301 |
|
302 |
private String[] selectOptions(Predicate<Entry<OptionKind, List<String>>> pred) { |
|
303 |
return optMap.entrySet().stream() |
|
304 |
.filter(pred) |
|
305 |
.flatMap(e -> e.getValue().stream()) |
|
306 |
.toArray(String[]::new); |
|
307 |
} |
|
308 |
||
309 |
String[] remoteVmOptions() { |
|
310 |
return selectOptions(e -> e.getKey().toRemoteVm); |
|
311 |
} |
|
312 |
||
313 |
String[] compilerOptions() { |
|
314 |
return selectOptions(e -> e.getKey().toCompiler); |
|
315 |
} |
|
316 |
||
317 |
String[] commonOptions() { |
|
318 |
return selectOptions(e -> e.getKey().passFlag); |
|
319 |
} |
|
320 |
||
321 |
void addAll(OptionKind kind, Collection<String> vals) { |
|
322 |
optMap.computeIfAbsent(kind, k -> new ArrayList<>()) |
|
323 |
.addAll(vals); |
|
324 |
} |
|
325 |
||
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
326 |
// return a new Options, with parameter options overriding receiver options |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
327 |
Options override(Options newer) { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
328 |
Options result = new Options(this); |
43038 | 329 |
newer.optMap.entrySet().stream() |
330 |
.forEach(e -> { |
|
331 |
if (e.getKey().onlyOne) { |
|
332 |
// Only one allowed, override last |
|
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
333 |
result.optMap.put(e.getKey(), e.getValue()); |
43038 | 334 |
} else { |
335 |
// Additive |
|
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
336 |
result.addAll(e.getKey(), e.getValue()); |
43038 | 337 |
} |
338 |
}); |
|
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
339 |
return result; |
43038 | 340 |
} |
341 |
} |
|
342 |
||
343 |
// base option parsing of /env, /reload, and /reset and command-line options |
|
344 |
private class OptionParserBase { |
|
345 |
||
346 |
final OptionParser parser = new OptionParser(); |
|
347 |
private final OptionSpec<String> argClassPath = parser.accepts("class-path").withRequiredArg(); |
|
348 |
private final OptionSpec<String> argModulePath = parser.accepts("module-path").withRequiredArg(); |
|
349 |
private final OptionSpec<String> argAddModules = parser.accepts("add-modules").withRequiredArg(); |
|
350 |
private final OptionSpec<String> argAddExports = parser.accepts("add-exports").withRequiredArg(); |
|
351 |
private final NonOptionArgumentSpec<String> argNonOptions = parser.nonOptions(); |
|
352 |
||
353 |
private Options opts = new Options(); |
|
354 |
private List<String> nonOptions; |
|
355 |
private boolean failed = false; |
|
356 |
||
357 |
List<String> nonOptions() { |
|
358 |
return nonOptions; |
|
359 |
} |
|
360 |
||
361 |
void msg(String key, Object... args) { |
|
362 |
errormsg(key, args); |
|
363 |
} |
|
364 |
||
365 |
Options parse(String[] args) throws OptionException { |
|
366 |
try { |
|
367 |
OptionSet oset = parser.parse(args); |
|
368 |
nonOptions = oset.valuesOf(argNonOptions); |
|
369 |
return parse(oset); |
|
370 |
} catch (OptionException ex) { |
|
371 |
if (ex.options().isEmpty()) { |
|
372 |
msg("jshell.err.opt.invalid", stream(args).collect(joining(", "))); |
|
373 |
} else { |
|
374 |
boolean isKnown = parser.recognizedOptions().containsKey(ex.options().iterator().next()); |
|
375 |
msg(isKnown |
|
376 |
? "jshell.err.opt.arg" |
|
377 |
: "jshell.err.opt.unknown", |
|
378 |
ex.options() |
|
379 |
.stream() |
|
380 |
.collect(joining(", "))); |
|
381 |
} |
|
47840 | 382 |
exitCode = 1; |
43038 | 383 |
return null; |
384 |
} |
|
385 |
} |
|
386 |
||
43856
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
387 |
// check that the supplied string represent valid class/module paths |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
388 |
// converting any ~/ to user home |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
389 |
private Collection<String> validPaths(Collection<String> vals, String context, boolean isModulePath) { |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
390 |
Stream<String> result = vals.stream() |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
391 |
.map(s -> Arrays.stream(s.split(File.pathSeparator)) |
48259
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
392 |
.flatMap(sp -> toPathImpl(sp, context)) |
43856
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
393 |
.filter(p -> checkValidPathEntry(p, context, isModulePath)) |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
394 |
.map(p -> p.toString()) |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
395 |
.collect(Collectors.joining(File.pathSeparator))); |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
396 |
if (failed) { |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
397 |
return Collections.emptyList(); |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
398 |
} else { |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
399 |
return result.collect(toList()); |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
400 |
} |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
401 |
} |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
402 |
|
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
403 |
// Adapted from compiler method Locations.checkValidModulePathEntry |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
404 |
private boolean checkValidPathEntry(Path p, String context, boolean isModulePath) { |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
405 |
if (!Files.exists(p)) { |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
406 |
msg("jshell.err.file.not.found", context, p); |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
407 |
failed = true; |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
408 |
return false; |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
409 |
} |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
410 |
if (Files.isDirectory(p)) { |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
411 |
// if module-path, either an exploded module or a directory of modules |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
412 |
return true; |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
413 |
} |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
414 |
|
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
415 |
String name = p.getFileName().toString(); |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
416 |
int lastDot = name.lastIndexOf("."); |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
417 |
if (lastDot > 0) { |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
418 |
switch (name.substring(lastDot)) { |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
419 |
case ".jar": |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
420 |
return true; |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
421 |
case ".jmod": |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
422 |
if (isModulePath) { |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
423 |
return true; |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
424 |
} |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
425 |
} |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
426 |
} |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
427 |
msg("jshell.err.arg", context, p); |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
428 |
failed = true; |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
429 |
return false; |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
430 |
} |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
431 |
|
48259
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
432 |
private Stream<Path> toPathImpl(String path, String context) { |
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
433 |
try { |
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
434 |
return Stream.of(toPathResolvingUserHome(path)); |
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
435 |
} catch (InvalidPathException ex) { |
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
436 |
msg("jshell.err.file.not.found", context, path); |
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
437 |
failed = true; |
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
438 |
return Stream.empty(); |
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
439 |
} |
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
440 |
} |
c0bf7d8af037
8191636: [Windows] jshell tool: Wrong character in /env class-path command crashes jshell
jlahoda
parents:
47931
diff
changeset
|
441 |
|
43038 | 442 |
Options parse(OptionSet options) { |
43856
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
443 |
addOptions(OptionKind.CLASS_PATH, |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
444 |
validPaths(options.valuesOf(argClassPath), "--class-path", false)); |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
445 |
addOptions(OptionKind.MODULE_PATH, |
fcdebb803c62
8174797: jshell tool: invalid module path crashes tool
rfield
parents:
43759
diff
changeset
|
446 |
validPaths(options.valuesOf(argModulePath), "--module-path", true)); |
43038 | 447 |
addOptions(OptionKind.ADD_MODULES, options.valuesOf(argAddModules)); |
448 |
addOptions(OptionKind.ADD_EXPORTS, options.valuesOf(argAddExports).stream() |
|
449 |
.map(mp -> mp.contains("=") ? mp : mp + "=ALL-UNNAMED") |
|
450 |
.collect(toList()) |
|
451 |
); |
|
452 |
||
47840 | 453 |
if (failed) { |
454 |
exitCode = 1; |
|
455 |
return null; |
|
456 |
} else { |
|
457 |
return opts; |
|
458 |
} |
|
43038 | 459 |
} |
460 |
||
461 |
void addOptions(OptionKind kind, Collection<String> vals) { |
|
462 |
if (!vals.isEmpty()) { |
|
463 |
if (kind.onlyOne && vals.size() > 1) { |
|
464 |
msg("jshell.err.opt.one", kind.optionFlag); |
|
465 |
failed = true; |
|
466 |
return; |
|
467 |
} |
|
468 |
if (kind.passFlag) { |
|
469 |
vals = vals.stream() |
|
470 |
.flatMap(mp -> Stream.of(kind.optionFlag, mp)) |
|
471 |
.collect(toList()); |
|
472 |
} |
|
473 |
opts.addAll(kind, vals); |
|
474 |
} |
|
475 |
} |
|
476 |
} |
|
477 |
||
478 |
// option parsing for /reload (adds -restore -quiet) |
|
479 |
private class OptionParserReload extends OptionParserBase { |
|
480 |
||
481 |
private final OptionSpecBuilder argRestore = parser.accepts("restore"); |
|
482 |
private final OptionSpecBuilder argQuiet = parser.accepts("quiet"); |
|
483 |
||
484 |
private boolean restore = false; |
|
485 |
private boolean quiet = false; |
|
486 |
||
487 |
boolean restore() { |
|
488 |
return restore; |
|
489 |
} |
|
490 |
||
491 |
boolean quiet() { |
|
492 |
return quiet; |
|
493 |
} |
|
494 |
||
495 |
@Override |
|
496 |
Options parse(OptionSet options) { |
|
497 |
if (options.has(argRestore)) { |
|
498 |
restore = true; |
|
499 |
} |
|
500 |
if (options.has(argQuiet)) { |
|
501 |
quiet = true; |
|
502 |
} |
|
503 |
return super.parse(options); |
|
504 |
} |
|
505 |
} |
|
506 |
||
507 |
// option parsing for command-line |
|
508 |
private class OptionParserCommandLine extends OptionParserBase { |
|
509 |
||
510 |
private final OptionSpec<String> argStart = parser.accepts("startup").withRequiredArg(); |
|
511 |
private final OptionSpecBuilder argNoStart = parser.acceptsAll(asList("n", "no-startup")); |
|
512 |
private final OptionSpec<String> argFeedback = parser.accepts("feedback").withRequiredArg(); |
|
513 |
private final OptionSpec<String> argExecution = parser.accepts("execution").withRequiredArg(); |
|
514 |
private final OptionSpecBuilder argQ = parser.accepts("q"); |
|
515 |
private final OptionSpecBuilder argS = parser.accepts("s"); |
|
516 |
private final OptionSpecBuilder argV = parser.accepts("v"); |
|
517 |
private final OptionSpec<String> argR = parser.accepts("R").withRequiredArg(); |
|
518 |
private final OptionSpec<String> argC = parser.accepts("C").withRequiredArg(); |
|
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
48349
diff
changeset
|
519 |
private final OptionSpecBuilder argHelp = parser.acceptsAll(asList("?", "h", "help")); |
43038 | 520 |
private final OptionSpecBuilder argVersion = parser.accepts("version"); |
521 |
private final OptionSpecBuilder argFullVersion = parser.accepts("full-version"); |
|
43367
7797472a9ed5
8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents:
43274
diff
changeset
|
522 |
private final OptionSpecBuilder argShowVersion = parser.accepts("show-version"); |
7797472a9ed5
8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents:
43274
diff
changeset
|
523 |
private final OptionSpecBuilder argHelpExtra = parser.acceptsAll(asList("X", "help-extra")); |
43038 | 524 |
|
525 |
private String feedbackMode = null; |
|
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
526 |
private Startup initialStartup = null; |
43038 | 527 |
|
528 |
String feedbackMode() { |
|
529 |
return feedbackMode; |
|
530 |
} |
|
531 |
||
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
532 |
Startup startup() { |
43038 | 533 |
return initialStartup; |
534 |
} |
|
535 |
||
536 |
@Override |
|
537 |
void msg(String key, Object... args) { |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
538 |
errormsg(key, args); |
43038 | 539 |
} |
540 |
||
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
541 |
/** |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
542 |
* Parse the command line options. |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
543 |
* @return the options as an Options object, or null if error |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
544 |
*/ |
43038 | 545 |
@Override |
546 |
Options parse(OptionSet options) { |
|
547 |
if (options.has(argHelp)) { |
|
548 |
printUsage(); |
|
549 |
return null; |
|
550 |
} |
|
43367
7797472a9ed5
8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents:
43274
diff
changeset
|
551 |
if (options.has(argHelpExtra)) { |
43038 | 552 |
printUsageX(); |
553 |
return null; |
|
554 |
} |
|
555 |
if (options.has(argVersion)) { |
|
556 |
cmdout.printf("jshell %s\n", version()); |
|
557 |
return null; |
|
558 |
} |
|
559 |
if (options.has(argFullVersion)) { |
|
560 |
cmdout.printf("jshell %s\n", fullVersion()); |
|
561 |
return null; |
|
562 |
} |
|
43367
7797472a9ed5
8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents:
43274
diff
changeset
|
563 |
if (options.has(argShowVersion)) { |
7797472a9ed5
8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents:
43274
diff
changeset
|
564 |
cmdout.printf("jshell %s\n", version()); |
7797472a9ed5
8171343: jshell tool: missing options: --help-extra --show-version
rfield
parents:
43274
diff
changeset
|
565 |
} |
43038 | 566 |
if ((options.valuesOf(argFeedback).size() + |
567 |
(options.has(argQ) ? 1 : 0) + |
|
568 |
(options.has(argS) ? 1 : 0) + |
|
569 |
(options.has(argV) ? 1 : 0)) > 1) { |
|
570 |
msg("jshell.err.opt.feedback.one"); |
|
47840 | 571 |
exitCode = 1; |
43038 | 572 |
return null; |
573 |
} else if (options.has(argFeedback)) { |
|
574 |
feedbackMode = options.valueOf(argFeedback); |
|
575 |
} else if (options.has("q")) { |
|
576 |
feedbackMode = "concise"; |
|
577 |
} else if (options.has("s")) { |
|
578 |
feedbackMode = "silent"; |
|
579 |
} else if (options.has("v")) { |
|
580 |
feedbackMode = "verbose"; |
|
581 |
} |
|
582 |
if (options.has(argStart)) { |
|
583 |
List<String> sts = options.valuesOf(argStart); |
|
584 |
if (options.has("no-startup")) { |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
585 |
msg("jshell.err.opt.startup.conflict"); |
47840 | 586 |
exitCode = 1; |
43038 | 587 |
return null; |
588 |
} |
|
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
589 |
initialStartup = Startup.fromFileList(sts, "--startup", new InitMessageHandler()); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
590 |
if (initialStartup == null) { |
47840 | 591 |
exitCode = 1; |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
592 |
return null; |
43038 | 593 |
} |
594 |
} else if (options.has(argNoStart)) { |
|
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
595 |
initialStartup = Startup.noStartup(); |
43038 | 596 |
} else { |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
597 |
String packedStartup = prefs.get(STARTUP_KEY); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
598 |
initialStartup = Startup.unpack(packedStartup, new InitMessageHandler()); |
43038 | 599 |
} |
600 |
if (options.has(argExecution)) { |
|
601 |
executionControlSpec = options.valueOf(argExecution); |
|
602 |
} |
|
603 |
addOptions(OptionKind.TO_REMOTE_VM, options.valuesOf(argR)); |
|
604 |
addOptions(OptionKind.TO_COMPILER, options.valuesOf(argC)); |
|
605 |
return super.parse(options); |
|
606 |
} |
|
607 |
} |
|
33362 | 608 |
|
36494 | 609 |
/** |
43564 | 610 |
* Encapsulate a history of snippets and commands which can be replayed. |
611 |
*/ |
|
612 |
private static class ReplayableHistory { |
|
613 |
||
614 |
// the history |
|
615 |
private List<String> hist; |
|
616 |
||
617 |
// the length of the history as of last save |
|
618 |
private int lastSaved; |
|
619 |
||
620 |
private ReplayableHistory(List<String> hist) { |
|
621 |
this.hist = hist; |
|
622 |
this.lastSaved = 0; |
|
623 |
} |
|
624 |
||
625 |
// factory for empty histories |
|
626 |
static ReplayableHistory emptyHistory() { |
|
627 |
return new ReplayableHistory(new ArrayList<>()); |
|
628 |
} |
|
629 |
||
630 |
// factory for history stored in persistent storage |
|
631 |
static ReplayableHistory fromPrevious(PersistentStorage prefs) { |
|
632 |
// Read replay history from last jshell session |
|
633 |
String prevReplay = prefs.get(REPLAY_RESTORE_KEY); |
|
634 |
if (prevReplay == null) { |
|
635 |
return null; |
|
636 |
} else { |
|
637 |
return new ReplayableHistory(Arrays.asList(prevReplay.split(RECORD_SEPARATOR))); |
|
638 |
} |
|
639 |
||
640 |
} |
|
641 |
||
642 |
// store the history in persistent storage |
|
643 |
void storeHistory(PersistentStorage prefs) { |
|
644 |
if (hist.size() > lastSaved) { |
|
645 |
// Prevent history overflow by calculating what will fit, starting |
|
646 |
// with most recent |
|
647 |
int sepLen = RECORD_SEPARATOR.length(); |
|
648 |
int length = 0; |
|
649 |
int first = hist.size(); |
|
650 |
while (length < Preferences.MAX_VALUE_LENGTH && --first >= 0) { |
|
651 |
length += hist.get(first).length() + sepLen; |
|
652 |
} |
|
653 |
if (first >= 0) { |
|
654 |
hist = hist.subList(first + 1, hist.size()); |
|
655 |
} |
|
656 |
String shist = String.join(RECORD_SEPARATOR, hist); |
|
657 |
prefs.put(REPLAY_RESTORE_KEY, shist); |
|
658 |
markSaved(); |
|
659 |
} |
|
660 |
prefs.flush(); |
|
661 |
} |
|
662 |
||
663 |
// add a snippet or command to the history |
|
664 |
void add(String s) { |
|
665 |
hist.add(s); |
|
666 |
} |
|
667 |
||
668 |
// return history to reloaded |
|
669 |
Iterable<String> iterable() { |
|
670 |
return hist; |
|
671 |
} |
|
672 |
||
673 |
// mark that persistent storage and current history are in sync |
|
674 |
void markSaved() { |
|
675 |
lastSaved = hist.size(); |
|
676 |
} |
|
677 |
} |
|
678 |
||
679 |
/** |
|
36494 | 680 |
* Is the input/output currently interactive |
681 |
* |
|
682 |
* @return true if console |
|
683 |
*/ |
|
684 |
boolean interactive() { |
|
685 |
return input != null && input.interactiveOutput(); |
|
686 |
} |
|
687 |
||
33362 | 688 |
void debug(String format, Object... args) { |
689 |
if (debug) { |
|
690 |
cmderr.printf(format + "\n", args); |
|
691 |
} |
|
692 |
} |
|
693 |
||
694 |
/** |
|
36494 | 695 |
* Must show command output |
696 |
* |
|
697 |
* @param format printf format |
|
698 |
* @param args printf args |
|
699 |
*/ |
|
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
700 |
@Override |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
701 |
public void hard(String format, Object... args) { |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
702 |
cmdout.printf(prefix(format), args); |
36494 | 703 |
} |
704 |
||
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
705 |
/** |
36494 | 706 |
* Error command output |
707 |
* |
|
708 |
* @param format printf format |
|
709 |
* @param args printf args |
|
710 |
*/ |
|
711 |
void error(String format, Object... args) { |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
712 |
(interactiveModeBegun? cmdout : cmderr).printf(prefixError(format), args); |
36494 | 713 |
} |
714 |
||
715 |
/** |
|
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
716 |
* Should optional informative be displayed? |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
717 |
* @return true if they should be displayed |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
718 |
*/ |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
719 |
@Override |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
720 |
public boolean showFluff() { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
721 |
return feedback.shouldDisplayCommandFluff() && interactive(); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
722 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
723 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
724 |
/** |
36494 | 725 |
* Optional output |
726 |
* |
|
33362 | 727 |
* @param format printf format |
728 |
* @param args printf args |
|
729 |
*/ |
|
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
730 |
@Override |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
731 |
public void fluff(String format, Object... args) { |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
732 |
if (showFluff()) { |
33362 | 733 |
hard(format, args); |
734 |
} |
|
735 |
} |
|
736 |
||
737 |
/** |
|
41934
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
738 |
* Resource bundle look-up |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
739 |
* |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
740 |
* @param key the resource key |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
741 |
*/ |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
742 |
String getResourceString(String key) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
743 |
if (outputRB == null) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
744 |
try { |
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
745 |
outputRB = ResourceBundle.getBundle(L10N_RB_NAME, locale); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
746 |
} catch (MissingResourceException mre) { |
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
747 |
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
|
748 |
return ""; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
749 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
750 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
751 |
String s; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
752 |
try { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
753 |
s = outputRB.getString(key); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
754 |
} catch (MissingResourceException mre) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
755 |
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
|
756 |
return ""; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
757 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
758 |
return s; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
759 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
760 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
761 |
/** |
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
762 |
* Add normal prefixing/postfixing to embedded newlines in a string, |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
763 |
* bracketing with normal prefix/postfix |
36990 | 764 |
* |
765 |
* @param s the string to prefix |
|
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
766 |
* @return the pre/post-fixed and bracketed string |
36990 | 767 |
*/ |
768 |
String prefix(String s) { |
|
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
769 |
return prefix(s, feedback.getPre(), feedback.getPost()); |
36990 | 770 |
} |
771 |
||
772 |
/** |
|
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
773 |
* Add error prefixing/postfixing to embedded newlines in a string, |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
774 |
* bracketing with error prefix/postfix |
36990 | 775 |
* |
776 |
* @param s the string to prefix |
|
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
777 |
* @return the pre/post-fixed and bracketed string |
36990 | 778 |
*/ |
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
779 |
String prefixError(String s) { |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
780 |
return prefix(s, feedback.getErrorPre(), feedback.getErrorPost()); |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
781 |
} |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
782 |
|
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
783 |
/** |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
784 |
* Add prefixing/postfixing to embedded newlines in a string, |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
785 |
* bracketing with prefix/postfix. No prefixing when non-interactive. |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
786 |
* Result is expected to be the format for a printf. |
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
787 |
* |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
788 |
* @param s the string to prefix |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
789 |
* @param pre the string to prepend to each line |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
790 |
* @param post the string to append to each line (replacing newline) |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
791 |
* @return the pre/post-fixed and bracketed string |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
792 |
*/ |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
793 |
String prefix(String s, String pre, String post) { |
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
794 |
if (s == null) { |
36990 | 795 |
return ""; |
796 |
} |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
797 |
if (!interactiveModeBegun) { |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
798 |
// messages expect to be new-line terminated (even when not prefixed) |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
799 |
return s + "%n"; |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
800 |
} |
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
801 |
String pp = s.replaceAll("\\R", post + pre); |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
802 |
if (pp.endsWith(post + pre)) { |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
803 |
// prevent an extra prefix char and blank line when the string |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
804 |
// already terminates with newline |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
805 |
pp = pp.substring(0, pp.length() - (post + pre).length()); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
806 |
} |
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
807 |
return pre + pp + post; |
36990 | 808 |
} |
809 |
||
810 |
/** |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
811 |
* 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
|
812 |
* |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
813 |
* @param key the resource key |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
814 |
*/ |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
815 |
void hardrb(String key) { |
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
816 |
hard(getResourceString(key)); |
36990 | 817 |
} |
818 |
||
819 |
/** |
|
820 |
* Format using resource bundle look-up using MessageFormat |
|
821 |
* |
|
822 |
* @param key the resource key |
|
823 |
* @param args |
|
824 |
*/ |
|
825 |
String messageFormat(String key, Object... args) { |
|
826 |
String rs = getResourceString(key); |
|
827 |
return MessageFormat.format(rs, args); |
|
828 |
} |
|
829 |
||
830 |
/** |
|
831 |
* Print using resource bundle look-up, MessageFormat, and add prefix and |
|
832 |
* postfix |
|
833 |
* |
|
834 |
* @param key the resource key |
|
835 |
* @param args |
|
836 |
*/ |
|
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
837 |
@Override |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
838 |
public void hardmsg(String key, Object... args) { |
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
839 |
hard(messageFormat(key, args)); |
36990 | 840 |
} |
841 |
||
842 |
/** |
|
843 |
* Print error using resource bundle look-up, MessageFormat, and add prefix |
|
844 |
* and postfix |
|
845 |
* |
|
846 |
* @param key the resource key |
|
847 |
* @param args |
|
848 |
*/ |
|
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
849 |
@Override |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
850 |
public void errormsg(String key, Object... args) { |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
851 |
error(messageFormat(key, args)); |
36990 | 852 |
} |
853 |
||
854 |
/** |
|
855 |
* Print (fluff) using resource bundle look-up, MessageFormat, and add |
|
856 |
* prefix and postfix |
|
857 |
* |
|
858 |
* @param key the resource key |
|
859 |
* @param args |
|
860 |
*/ |
|
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
861 |
@Override |
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
862 |
public void fluffmsg(String key, Object... args) { |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
863 |
if (showFluff()) { |
36990 | 864 |
hardmsg(key, args); |
865 |
} |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
866 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
867 |
|
36494 | 868 |
<T> void hardPairs(Stream<T> stream, Function<T, String> a, Function<T, String> b) { |
869 |
Map<String, String> a2b = stream.collect(toMap(a, b, |
|
870 |
(m1, m2) -> m1, |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
871 |
LinkedHashMap::new)); |
36494 | 872 |
for (Entry<String, String> e : a2b.entrySet()) { |
42412
ca6f4f1914b2
8169828: jdk/jshell/ExternalEditorTest.java testStatementMush() fails frequently on all platform
rfield
parents:
42265
diff
changeset
|
873 |
hard("%s", e.getKey()); |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
874 |
cmdout.printf(prefix(e.getValue(), feedback.getPre() + "\t", feedback.getPost())); |
33362 | 875 |
} |
876 |
} |
|
877 |
||
878 |
/** |
|
879 |
* Trim whitespace off end of string |
|
36494 | 880 |
* |
33362 | 881 |
* @param s |
882 |
* @return |
|
883 |
*/ |
|
884 |
static String trimEnd(String s) { |
|
885 |
int last = s.length() - 1; |
|
886 |
int i = last; |
|
887 |
while (i >= 0 && Character.isWhitespace(s.charAt(i))) { |
|
888 |
--i; |
|
889 |
} |
|
890 |
if (i != last) { |
|
891 |
return s.substring(0, i + 1); |
|
892 |
} else { |
|
893 |
return s; |
|
894 |
} |
|
895 |
} |
|
896 |
||
43757 | 897 |
/** |
898 |
* The entry point into the JShell tool. |
|
899 |
* |
|
900 |
* @param args the command-line arguments |
|
901 |
* @throws Exception catastrophic fatal exception |
|
47840 | 902 |
* @return the exit code |
43757 | 903 |
*/ |
47840 | 904 |
public int start(String[] args) throws Exception { |
43038 | 905 |
OptionParserCommandLine commandLineArgs = new OptionParserCommandLine(); |
906 |
options = commandLineArgs.parse(args); |
|
907 |
if (options == null) { |
|
47840 | 908 |
// A null means end immediately, this may be an error or because |
909 |
// of options like --version. Exit code has been set. |
|
910 |
return exitCode; |
|
33362 | 911 |
} |
43038 | 912 |
startup = commandLineArgs.startup(); |
913 |
// initialize editor settings |
|
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
914 |
configEditor(); |
43038 | 915 |
// initialize JShell instance |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
916 |
try { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
917 |
resetState(); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
918 |
} catch (IllegalStateException ex) { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
919 |
// Display just the cause (not a exception backtrace) |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
920 |
cmderr.println(ex.getMessage()); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
921 |
//abort |
47840 | 922 |
return 1; |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
923 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
924 |
// Read replay history from last jshell session into previous history |
43564 | 925 |
replayableHistoryPrevious = ReplayableHistory.fromPrevious(prefs); |
43038 | 926 |
// load snippet/command files given on command-line |
927 |
for (String loadFile : commandLineArgs.nonOptions()) { |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
928 |
if (!runFile(loadFile, "jshell")) { |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
929 |
// Load file failed -- abort |
47840 | 930 |
return 1; |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
931 |
} |
33362 | 932 |
} |
43038 | 933 |
// if we survived that... |
934 |
if (regenerateOnDeath) { |
|
935 |
// initialize the predefined feedback modes |
|
936 |
initFeedback(commandLineArgs.feedbackMode()); |
|
937 |
} |
|
938 |
// check again, as feedback setting could have failed |
|
939 |
if (regenerateOnDeath) { |
|
940 |
// if we haven't died, and the feedback mode wants fluff, print welcome |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
941 |
interactiveModeBegun = true; |
43038 | 942 |
if (feedback.shouldDisplayCommandFluff()) { |
943 |
hardmsg("jshell.msg.welcome", version()); |
|
944 |
} |
|
43564 | 945 |
// Be sure history is always saved so that user code isn't lost |
43757 | 946 |
Thread shutdownHook = new Thread() { |
43564 | 947 |
@Override |
948 |
public void run() { |
|
949 |
replayableHistory.storeHistory(prefs); |
|
950 |
} |
|
43757 | 951 |
}; |
952 |
Runtime.getRuntime().addShutdownHook(shutdownHook); |
|
43038 | 953 |
// execute from user input |
954 |
try (IOContext in = new ConsoleIOContext(this, cmdin, console)) { |
|
43757 | 955 |
while (regenerateOnDeath) { |
956 |
if (!live) { |
|
957 |
resetState(); |
|
958 |
} |
|
959 |
run(in); |
|
960 |
} |
|
961 |
} finally { |
|
962 |
replayableHistory.storeHistory(prefs); |
|
963 |
closeState(); |
|
964 |
try { |
|
965 |
Runtime.getRuntime().removeShutdownHook(shutdownHook); |
|
966 |
} catch (Exception ex) { |
|
967 |
// ignore, this probably caused by VM aready being shutdown |
|
968 |
// and this is the last act anyhow |
|
969 |
} |
|
43038 | 970 |
} |
971 |
} |
|
43757 | 972 |
closeState(); |
47840 | 973 |
return exitCode; |
33362 | 974 |
} |
975 |
||
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
976 |
private EditorSetting configEditor() { |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
977 |
// Read retained editor setting (if any) |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
978 |
editor = EditorSetting.fromPrefs(prefs); |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
979 |
if (editor != null) { |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
980 |
return editor; |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
981 |
} |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
982 |
// Try getting editor setting from OS environment variables |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
983 |
for (String envvar : EDITOR_ENV_VARS) { |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
984 |
String v = envvars.get(envvar); |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
985 |
if (v != null) { |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
986 |
return editor = new EditorSetting(v.split("\\s+"), false); |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
987 |
} |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
988 |
} |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
989 |
// Default to the built-in editor |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
990 |
return editor = BUILT_IN_EDITOR; |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
991 |
} |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
992 |
|
33362 | 993 |
private void printUsage() { |
36990 | 994 |
cmdout.print(getResourceString("help.usage")); |
33362 | 995 |
} |
996 |
||
41248 | 997 |
private void printUsageX() { |
998 |
cmdout.print(getResourceString("help.usage.x")); |
|
999 |
} |
|
1000 |
||
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1001 |
/** |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1002 |
* Message handler to use during initial start-up. |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1003 |
*/ |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1004 |
private class InitMessageHandler implements MessageHandler { |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1005 |
|
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1006 |
@Override |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1007 |
public void fluff(String format, Object... args) { |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1008 |
//ignore |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1009 |
} |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1010 |
|
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1011 |
@Override |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1012 |
public void fluffmsg(String messageKey, Object... args) { |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1013 |
//ignore |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1014 |
} |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1015 |
|
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1016 |
@Override |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1017 |
public void hard(String format, Object... args) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1018 |
//ignore |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1019 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1020 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1021 |
@Override |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1022 |
public void hardmsg(String messageKey, Object... args) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1023 |
//ignore |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1024 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1025 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1026 |
@Override |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1027 |
public void errormsg(String messageKey, Object... args) { |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
1028 |
JShellTool.this.errormsg(messageKey, args); |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1029 |
} |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1030 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1031 |
@Override |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1032 |
public boolean showFluff() { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1033 |
return false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1034 |
} |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1035 |
} |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1036 |
|
33362 | 1037 |
private void resetState() { |
1038 |
closeState(); |
|
1039 |
||
1040 |
// Initialize tool id mapping |
|
1041 |
mainNamespace = new NameSpace("main", ""); |
|
1042 |
startNamespace = new NameSpace("start", "s"); |
|
1043 |
errorNamespace = new NameSpace("error", "e"); |
|
1044 |
mapSnippet = new LinkedHashMap<>(); |
|
1045 |
currentNameSpace = startNamespace; |
|
1046 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1047 |
// 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
|
1048 |
replayableHistoryPrevious = replayableHistory; |
43564 | 1049 |
replayableHistory = ReplayableHistory.emptyHistory(); |
42969
a48d4f74d322
8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents:
42843
diff
changeset
|
1050 |
JShell.Builder builder = |
a48d4f74d322
8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents:
42843
diff
changeset
|
1051 |
JShell.builder() |
33362 | 1052 |
.in(userin) |
1053 |
.out(userout) |
|
1054 |
.err(usererr) |
|
42969
a48d4f74d322
8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents:
42843
diff
changeset
|
1055 |
.tempVariableNameGenerator(() -> "$" + currentNameSpace.tidNext()) |
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38613
diff
changeset
|
1056 |
.idGenerator((sn, i) -> (currentNameSpace == startNamespace || state.status(sn).isActive()) |
33362 | 1057 |
? currentNameSpace.tid(sn) |
1058 |
: errorNamespace.tid(sn)) |
|
43038 | 1059 |
.remoteVMOptions(options.remoteVmOptions()) |
1060 |
.compilerOptions(options.compilerOptions()); |
|
42969
a48d4f74d322
8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents:
42843
diff
changeset
|
1061 |
if (executionControlSpec != null) { |
a48d4f74d322
8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents:
42843
diff
changeset
|
1062 |
builder.executionEngine(executionControlSpec); |
a48d4f74d322
8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents:
42843
diff
changeset
|
1063 |
} |
a48d4f74d322
8168615: JShell API: jdk.jshell.spi should be a pluggable ServiceLoader SPI
rfield
parents:
42843
diff
changeset
|
1064 |
state = builder.build(); |
33362 | 1065 |
shutdownSubscription = state.onShutdown((JShell deadState) -> { |
1066 |
if (deadState == state) { |
|
36990 | 1067 |
hardmsg("jshell.msg.terminated"); |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
1068 |
fluffmsg("jshell.msg.terminated.restore"); |
33362 | 1069 |
live = false; |
1070 |
} |
|
1071 |
}); |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1072 |
analysis = state.sourceCodeAnalysis(); |
33362 | 1073 |
live = true; |
1074 |
||
44065
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1075 |
// Run the start-up script. |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1076 |
// Avoid an infinite loop running start-up while running start-up. |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1077 |
// This could, otherwise, occur when /env /reset or /reload commands are |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1078 |
// in the start-up script. |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1079 |
if (!isCurrentlyRunningStartup) { |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1080 |
try { |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1081 |
isCurrentlyRunningStartup = true; |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1082 |
startUpRun(startup.toString()); |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1083 |
} finally { |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1084 |
isCurrentlyRunningStartup = false; |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1085 |
} |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1086 |
} |
d0a8a4c41c85
8175304: JShell tool: The /reset command hangs after setting a startup script
rfield
parents:
43856
diff
changeset
|
1087 |
// Record subsequent snippets in the main namespace. |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1088 |
currentNameSpace = mainNamespace; |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1089 |
} |
38539 | 1090 |
|
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1091 |
//where -- one-time per run initialization of feedback modes |
43038 | 1092 |
private void initFeedback(String initMode) { |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1093 |
// No fluff, no prefix, for init failures |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1094 |
MessageHandler initmh = new InitMessageHandler(); |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1095 |
// Execute the feedback initialization code in the resource file |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1096 |
startUpRun(getResourceString("startup.feedback")); |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1097 |
// These predefined modes are read-only |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1098 |
feedback.markModesReadOnly(); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1099 |
// Restore user defined modes retained on previous run with /set mode -retain |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
1100 |
String encoded = prefs.get(MODE_KEY); |
38539 | 1101 |
if (encoded != null && !encoded.isEmpty()) { |
1102 |
if (!feedback.restoreEncodedModes(initmh, encoded)) { |
|
1103 |
// Catastrophic corruption -- remove the retained modes |
|
1104 |
prefs.remove(MODE_KEY); |
|
1105 |
} |
|
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1106 |
} |
43038 | 1107 |
if (initMode != null) { |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1108 |
// The feedback mode to use was specified on the command line, use it |
43038 | 1109 |
if (!setFeedback(initmh, new ArgTokenizer("--feedback", initMode))) { |
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
1110 |
regenerateOnDeath = false; |
47840 | 1111 |
exitCode = 1; |
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
1112 |
} |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1113 |
} else { |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
1114 |
String fb = prefs.get(FEEDBACK_KEY); |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1115 |
if (fb != null) { |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1116 |
// Restore the feedback mode to use that was retained |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1117 |
// on a previous run with /set feedback -retain |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1118 |
setFeedback(initmh, new ArgTokenizer("previous retain feedback", "-retain " + fb)); |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1119 |
} |
37745
4b6b59f8e327
8150382: JShell API: Allow setting remote JVM arguments
rfield
parents:
37389
diff
changeset
|
1120 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1121 |
} |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1122 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1123 |
//where |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1124 |
private void startUpRun(String start) { |
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
1125 |
try (IOContext suin = new ScannerIOContext(new StringReader(start))) { |
33362 | 1126 |
run(suin); |
1127 |
} catch (Exception ex) { |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
1128 |
errormsg("jshell.err.startup.unexpected.exception", ex); |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
1129 |
ex.printStackTrace(cmderr); |
33362 | 1130 |
} |
1131 |
} |
|
1132 |
||
1133 |
private void closeState() { |
|
1134 |
live = false; |
|
1135 |
JShell oldState = state; |
|
1136 |
if (oldState != null) { |
|
43757 | 1137 |
state = null; |
1138 |
analysis = null; |
|
33362 | 1139 |
oldState.unsubscribe(shutdownSubscription); // No notification |
1140 |
oldState.close(); |
|
1141 |
} |
|
1142 |
} |
|
1143 |
||
1144 |
/** |
|
1145 |
* Main loop |
|
47840 | 1146 |
* |
33362 | 1147 |
* @param in the line input/editing context |
1148 |
*/ |
|
1149 |
private void run(IOContext in) { |
|
1150 |
IOContext oldInput = input; |
|
1151 |
input = in; |
|
1152 |
try { |
|
47840 | 1153 |
// remaining is the source left after one snippet is evaluated |
1154 |
String remaining = ""; |
|
33362 | 1155 |
while (live) { |
47840 | 1156 |
// Get a line(s) of input |
1157 |
String src = getInput(remaining); |
|
1158 |
// Process the snippet or command, returning the remaining source |
|
1159 |
remaining = processInput(src); |
|
33362 | 1160 |
} |
47840 | 1161 |
} catch (EOFException ex) { |
1162 |
// Just exit loop |
|
33362 | 1163 |
} catch (IOException ex) { |
36990 | 1164 |
errormsg("jshell.err.unexpected.exception", ex); |
33362 | 1165 |
} finally { |
1166 |
input = oldInput; |
|
1167 |
} |
|
1168 |
} |
|
1169 |
||
47840 | 1170 |
/** |
1171 |
* Process an input command or snippet. |
|
1172 |
* |
|
1173 |
* @param src the source to process |
|
1174 |
* @return any remaining input to processed |
|
1175 |
*/ |
|
1176 |
private String processInput(String src) { |
|
1177 |
if (isCommand(src)) { |
|
1178 |
// It is a command |
|
1179 |
processCommand(src.trim()); |
|
1180 |
// No remaining input after a command |
|
1181 |
return ""; |
|
1182 |
} else { |
|
1183 |
// It is a snipet. Separate the source from the remaining. Evaluate |
|
1184 |
// the source |
|
1185 |
CompletionInfo an = analysis.analyzeCompletion(src); |
|
1186 |
if (processSourceCatchingReset(trimEnd(an.source()))) { |
|
1187 |
// Snippet was successful use any leftover source |
|
1188 |
return an.remaining(); |
|
1189 |
} else { |
|
1190 |
// Snippet failed, throw away any remaining source |
|
1191 |
return ""; |
|
1192 |
} |
|
1193 |
} |
|
1194 |
} |
|
1195 |
||
1196 |
/** |
|
1197 |
* Get the input line (or, if incomplete, lines). |
|
1198 |
* |
|
1199 |
* @param initial leading input (left over after last snippet) |
|
1200 |
* @return the complete input snippet or command |
|
1201 |
* @throws IOException on unexpected I/O error |
|
1202 |
*/ |
|
1203 |
private String getInput(String initial) throws IOException{ |
|
1204 |
String src = initial; |
|
1205 |
while (live) { // loop while incomplete (and live) |
|
1206 |
if (!src.isEmpty()) { |
|
1207 |
// We have some source, see if it is complete, if so, use it |
|
1208 |
String check; |
|
1209 |
||
1210 |
if (isCommand(src)) { |
|
1211 |
// A command can only be incomplete if it is a /exit with |
|
1212 |
// an argument |
|
1213 |
int sp = src.indexOf(" "); |
|
1214 |
if (sp < 0) return src; |
|
1215 |
check = src.substring(sp).trim(); |
|
1216 |
if (check.isEmpty()) return src; |
|
1217 |
String cmd = src.substring(0, sp); |
|
1218 |
Command[] match = findCommand(cmd, c -> c.kind.isRealCommand); |
|
1219 |
if (match.length != 1 || !match[0].command.equals("/exit")) { |
|
1220 |
// A command with no snippet arg, so no multi-line input |
|
1221 |
return src; |
|
1222 |
} |
|
1223 |
} else { |
|
1224 |
// For a snippet check the whole source |
|
1225 |
check = src; |
|
1226 |
} |
|
1227 |
Completeness comp = analysis.analyzeCompletion(check).completeness(); |
|
1228 |
if (comp.isComplete() || comp == Completeness.EMPTY) { |
|
1229 |
return src; |
|
1230 |
} |
|
1231 |
} |
|
1232 |
String prompt = interactive() |
|
1233 |
? testPrompt |
|
1234 |
? src.isEmpty() |
|
1235 |
? "\u0005" //ENQ -- test prompt |
|
1236 |
: "\u0006" //ACK -- test continuation prompt |
|
1237 |
: src.isEmpty() |
|
1238 |
? feedback.getPrompt(currentNameSpace.tidNext()) |
|
1239 |
: feedback.getContinuationPrompt(currentNameSpace.tidNext()) |
|
1240 |
: "" // Non-interactive -- no prompt |
|
1241 |
; |
|
1242 |
String line; |
|
1243 |
try { |
|
1244 |
line = input.readLine(prompt, src); |
|
1245 |
} catch (InputInterruptedException ex) { |
|
1246 |
//input interrupted - clearing current state |
|
1247 |
src = ""; |
|
1248 |
continue; |
|
1249 |
} |
|
1250 |
if (line == null) { |
|
1251 |
//EOF |
|
1252 |
if (input.interactiveOutput()) { |
|
1253 |
// End after user ctrl-D |
|
1254 |
regenerateOnDeath = false; |
|
1255 |
} |
|
1256 |
throw new EOFException(); // no more input |
|
1257 |
} |
|
1258 |
src = src.isEmpty() |
|
1259 |
? line |
|
1260 |
: src + "\n" + line; |
|
1261 |
} |
|
1262 |
throw new EOFException(); // not longer live |
|
1263 |
} |
|
1264 |
||
1265 |
private boolean isCommand(String line) { |
|
1266 |
return line.startsWith("/") && !line.startsWith("//") && !line.startsWith("/*"); |
|
1267 |
} |
|
1268 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1269 |
private void addToReplayHistory(String s) { |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
1270 |
if (!isCurrentlyRunningStartup) { |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1271 |
replayableHistory.add(s); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1272 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1273 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1274 |
|
47840 | 1275 |
/** |
1276 |
* Process a source snippet. |
|
1277 |
* |
|
1278 |
* @param src the snippet source to process |
|
1279 |
* @return true on success, false on failure |
|
1280 |
*/ |
|
1281 |
private boolean processSourceCatchingReset(String src) { |
|
33362 | 1282 |
try { |
1283 |
input.beforeUserCode(); |
|
1284 |
return processSource(src); |
|
1285 |
} catch (IllegalStateException ex) { |
|
1286 |
hard("Resetting..."); |
|
1287 |
live = false; // Make double sure |
|
47840 | 1288 |
return false; |
33362 | 1289 |
} finally { |
1290 |
input.afterUserCode(); |
|
1291 |
} |
|
1292 |
} |
|
1293 |
||
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1294 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1295 |
* Process a command (as opposed to a snippet) -- things that start with |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1296 |
* slash. |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1297 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1298 |
* @param input |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1299 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1300 |
private void processCommand(String input) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1301 |
if (input.startsWith("/-")) { |
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1302 |
try { |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1303 |
//handle "/-[number]" |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1304 |
cmdUseHistoryEntry(Integer.parseInt(input.substring(1))); |
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1305 |
return ; |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1306 |
} 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
|
1307 |
//ignore |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
1308 |
} |
33362 | 1309 |
} |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1310 |
String cmd; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1311 |
String arg; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1312 |
int idx = input.indexOf(' '); |
33362 | 1313 |
if (idx > 0) { |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1314 |
arg = input.substring(idx + 1).trim(); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1315 |
cmd = input.substring(0, idx); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1316 |
} else { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1317 |
cmd = input; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1318 |
arg = ""; |
33362 | 1319 |
} |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1320 |
// find the command as a "real command", not a pseudo-command or doc subject |
35812 | 1321 |
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
|
1322 |
switch (candidates.length) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1323 |
case 0: |
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1324 |
// not found, it is either a rerun-ID command or an error |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1325 |
if (RERUN_ID.matcher(cmd).matches()) { |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1326 |
// it is in the form of a snipppet id, see if it is a valid history reference |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1327 |
rerunHistoryEntriesById(input); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1328 |
} else { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1329 |
errormsg("jshell.err.invalid.command", cmd); |
36990 | 1330 |
fluffmsg("jshell.msg.help.for.help"); |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1331 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1332 |
break; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1333 |
case 1: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1334 |
Command command = candidates[0]; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1335 |
// 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
|
1336 |
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
|
1337 |
addToReplayHistory((command.command + " " + arg).trim()); |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1338 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1339 |
break; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1340 |
default: |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1341 |
// command if too short (ambigous), show the possibly matches |
36990 | 1342 |
errormsg("jshell.err.command.ambiguous", cmd, |
1343 |
Arrays.stream(candidates).map(c -> c.command).collect(Collectors.joining(", "))); |
|
1344 |
fluffmsg("jshell.msg.help.for.help"); |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1345 |
break; |
33362 | 1346 |
} |
1347 |
} |
|
1348 |
||
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1349 |
private Command[] findCommand(String cmd, Predicate<Command> filter) { |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1350 |
Command exact = commands.get(cmd); |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1351 |
if (exact != null) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1352 |
return new Command[] {exact}; |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1353 |
|
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1354 |
return commands.values() |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1355 |
.stream() |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1356 |
.filter(filter) |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1357 |
.filter(command -> command.command.startsWith(cmd)) |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1358 |
.toArray(Command[]::new); |
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1359 |
} |
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1360 |
|
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
1361 |
static Path toPathResolvingUserHome(String pathString) { |
33362 | 1362 |
if (pathString.replace(File.separatorChar, '/').startsWith("~/")) |
1363 |
return Paths.get(System.getProperty("user.home"), pathString.substring(2)); |
|
1364 |
else |
|
1365 |
return Paths.get(pathString); |
|
1366 |
} |
|
1367 |
||
1368 |
static final class Command { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1369 |
public final String command; |
36990 | 1370 |
public final String helpKey; |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1371 |
public final Function<String,Boolean> run; |
33362 | 1372 |
public final CompletionProvider completions; |
1373 |
public final CommandKind kind; |
|
1374 |
||
35812 | 1375 |
// NORMAL Commands |
36990 | 1376 |
public Command(String command, Function<String,Boolean> run, CompletionProvider completions) { |
1377 |
this(command, run, completions, CommandKind.NORMAL); |
|
1378 |
} |
|
1379 |
||
1380 |
// Special kinds of Commands |
|
1381 |
public Command(String command, Function<String,Boolean> run, CompletionProvider completions, CommandKind kind) { |
|
1382 |
this(command, "help." + command.substring(1), |
|
1383 |
run, completions, kind); |
|
33362 | 1384 |
} |
1385 |
||
35812 | 1386 |
// Documentation pseudo-commands |
36990 | 1387 |
public Command(String command, String helpKey, CommandKind kind) { |
1388 |
this(command, helpKey, |
|
35812 | 1389 |
arg -> { throw new IllegalStateException(); }, |
1390 |
EMPTY_COMPLETION_PROVIDER, |
|
1391 |
kind); |
|
1392 |
} |
|
1393 |
||
36990 | 1394 |
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
|
1395 |
this.command = command; |
36990 | 1396 |
this.helpKey = helpKey; |
33362 | 1397 |
this.run = run; |
1398 |
this.completions = completions; |
|
1399 |
this.kind = kind; |
|
1400 |
} |
|
1401 |
||
1402 |
} |
|
1403 |
||
1404 |
interface CompletionProvider { |
|
1405 |
List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor); |
|
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1406 |
|
33362 | 1407 |
} |
1408 |
||
1409 |
enum CommandKind { |
|
35812 | 1410 |
NORMAL(true, true, true), |
1411 |
REPLAY(true, true, true), |
|
1412 |
HIDDEN(true, false, false), |
|
1413 |
HELP_ONLY(false, true, false), |
|
1414 |
HELP_SUBJECT(false, false, false); |
|
1415 |
||
1416 |
final boolean isRealCommand; |
|
1417 |
final boolean showInHelp; |
|
1418 |
final boolean shouldSuggestCompletions; |
|
1419 |
private CommandKind(boolean isRealCommand, boolean showInHelp, boolean shouldSuggestCompletions) { |
|
1420 |
this.isRealCommand = isRealCommand; |
|
1421 |
this.showInHelp = showInHelp; |
|
1422 |
this.shouldSuggestCompletions = shouldSuggestCompletions; |
|
1423 |
} |
|
1424 |
} |
|
1425 |
||
33362 | 1426 |
static final class FixedCompletionProvider implements CompletionProvider { |
1427 |
||
1428 |
private final String[] alternatives; |
|
1429 |
||
1430 |
public FixedCompletionProvider(String... alternatives) { |
|
1431 |
this.alternatives = alternatives; |
|
1432 |
} |
|
1433 |
||
43038 | 1434 |
// Add more options to an existing provider |
1435 |
public FixedCompletionProvider(FixedCompletionProvider base, String... alternatives) { |
|
1436 |
List<String> l = new ArrayList<>(Arrays.asList(base.alternatives)); |
|
1437 |
l.addAll(Arrays.asList(alternatives)); |
|
1438 |
this.alternatives = l.toArray(new String[l.size()]); |
|
1439 |
} |
|
1440 |
||
33362 | 1441 |
@Override |
1442 |
public List<Suggestion> completionSuggestions(String input, int cursor, int[] anchor) { |
|
1443 |
List<Suggestion> result = new ArrayList<>(); |
|
1444 |
||
1445 |
for (String alternative : alternatives) { |
|
1446 |
if (alternative.startsWith(input)) { |
|
40498
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
1447 |
result.add(new ArgSuggestion(alternative)); |
33362 | 1448 |
} |
1449 |
} |
|
1450 |
||
1451 |
anchor[0] = 0; |
|
1452 |
||
1453 |
return result; |
|
1454 |
} |
|
1455 |
||
1456 |
} |
|
1457 |
||
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1458 |
static final CompletionProvider EMPTY_COMPLETION_PROVIDER = new FixedCompletionProvider(); |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1459 |
private static final CompletionProvider SNIPPET_HISTORY_OPTION_COMPLETION_PROVIDER = new FixedCompletionProvider("-all", "-start ", "-history"); |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1460 |
private static final CompletionProvider SAVE_OPTION_COMPLETION_PROVIDER = new FixedCompletionProvider("-all ", "-start ", "-history "); |
48939
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
1461 |
private static final CompletionProvider HISTORY_OPTION_COMPLETION_PROVIDER = new FixedCompletionProvider("-all"); |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1462 |
private static final CompletionProvider SNIPPET_OPTION_COMPLETION_PROVIDER = new FixedCompletionProvider("-all", "-start " ); |
43038 | 1463 |
private static final FixedCompletionProvider COMMAND_LINE_LIKE_OPTIONS_COMPLETION_PROVIDER = new FixedCompletionProvider( |
1464 |
"-class-path ", "-module-path ", "-add-modules ", "-add-exports "); |
|
1465 |
private static final CompletionProvider RELOAD_OPTIONS_COMPLETION_PROVIDER = new FixedCompletionProvider( |
|
1466 |
COMMAND_LINE_LIKE_OPTIONS_COMPLETION_PROVIDER, |
|
1467 |
"-restore ", "-quiet "); |
|
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1468 |
private static final CompletionProvider SET_MODE_OPTIONS_COMPLETION_PROVIDER = new FixedCompletionProvider("-command", "-quiet", "-delete"); |
33362 | 1469 |
private static final CompletionProvider FILE_COMPLETION_PROVIDER = fileCompletions(p -> true); |
43038 | 1470 |
private static final Map<String, CompletionProvider> ARG_OPTIONS = new HashMap<>(); |
1471 |
static { |
|
1472 |
ARG_OPTIONS.put("-class-path", classPathCompletion()); |
|
1473 |
ARG_OPTIONS.put("-module-path", fileCompletions(Files::isDirectory)); |
|
1474 |
ARG_OPTIONS.put("-add-modules", EMPTY_COMPLETION_PROVIDER); |
|
1475 |
ARG_OPTIONS.put("-add-exports", EMPTY_COMPLETION_PROVIDER); |
|
1476 |
} |
|
33362 | 1477 |
private final Map<String, Command> commands = new LinkedHashMap<>(); |
1478 |
private void registerCommand(Command cmd) { |
|
34475
7af94fd75ede
8143037: JShell should determine commands by prefix
jlahoda
parents:
33714
diff
changeset
|
1479 |
commands.put(cmd.command, cmd); |
33362 | 1480 |
} |
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1481 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1482 |
private static CompletionProvider skipWordThenCompletion(CompletionProvider completionProvider) { |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1483 |
return (input, cursor, anchor) -> { |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1484 |
List<Suggestion> result = Collections.emptyList(); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1485 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1486 |
int space = input.indexOf(' '); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1487 |
if (space != -1) { |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1488 |
String rest = input.substring(space + 1); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1489 |
result = completionProvider.completionSuggestions(rest, cursor - space - 1, anchor); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1490 |
anchor[0] += space + 1; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1491 |
} |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1492 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1493 |
return result; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1494 |
}; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1495 |
} |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1496 |
|
33362 | 1497 |
private static CompletionProvider fileCompletions(Predicate<Path> accept) { |
1498 |
return (code, cursor, anchor) -> { |
|
1499 |
int lastSlash = code.lastIndexOf('/'); |
|
1500 |
String path = code.substring(0, lastSlash + 1); |
|
1501 |
String prefix = lastSlash != (-1) ? code.substring(lastSlash + 1) : code; |
|
1502 |
Path current = toPathResolvingUserHome(path); |
|
1503 |
List<Suggestion> result = new ArrayList<>(); |
|
1504 |
try (Stream<Path> dir = Files.list(current)) { |
|
1505 |
dir.filter(f -> accept.test(f) && f.getFileName().toString().startsWith(prefix)) |
|
40498
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
1506 |
.map(f -> new ArgSuggestion(f.getFileName() + (Files.isDirectory(f) ? "/" : ""))) |
33362 | 1507 |
.forEach(result::add); |
1508 |
} catch (IOException ex) { |
|
1509 |
//ignore... |
|
1510 |
} |
|
1511 |
if (path.isEmpty()) { |
|
1512 |
StreamSupport.stream(FileSystems.getDefault().getRootDirectories().spliterator(), false) |
|
42415
85388bae723d
8165564: langtools\test\jdk\jshell\CommandCompletionTest.java fails on some windows
jlahoda
parents:
42412
diff
changeset
|
1513 |
.filter(root -> Files.exists(root)) |
33362 | 1514 |
.filter(root -> accept.test(root) && root.toString().startsWith(prefix)) |
40498
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
1515 |
.map(root -> new ArgSuggestion(root.toString())) |
33362 | 1516 |
.forEach(result::add); |
1517 |
} |
|
1518 |
anchor[0] = path.length(); |
|
1519 |
return result; |
|
1520 |
}; |
|
1521 |
} |
|
1522 |
||
1523 |
private static CompletionProvider classPathCompletion() { |
|
1524 |
return fileCompletions(p -> Files.isDirectory(p) || |
|
1525 |
p.getFileName().toString().endsWith(".zip") || |
|
1526 |
p.getFileName().toString().endsWith(".jar")); |
|
1527 |
} |
|
1528 |
||
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1529 |
// Completion based on snippet supplier |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1530 |
private CompletionProvider snippetCompletion(Supplier<Stream<? extends Snippet>> snippetsSupplier) { |
33362 | 1531 |
return (prefix, cursor, anchor) -> { |
1532 |
anchor[0] = 0; |
|
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1533 |
int space = prefix.lastIndexOf(' '); |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1534 |
Set<String> prior = new HashSet<>(Arrays.asList(prefix.split(" "))); |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1535 |
if (prior.contains("-all") || prior.contains("-history")) { |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1536 |
return Collections.emptyList(); |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1537 |
} |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1538 |
String argPrefix = prefix.substring(space + 1); |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1539 |
return snippetsSupplier.get() |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1540 |
.filter(k -> !prior.contains(String.valueOf(k.id())) |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1541 |
&& (!(k instanceof DeclarationSnippet) |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1542 |
|| !prior.contains(((DeclarationSnippet) k).name()))) |
33362 | 1543 |
.flatMap(k -> (k instanceof DeclarationSnippet) |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1544 |
? Stream.of(String.valueOf(k.id()) + " ", ((DeclarationSnippet) k).name() + " ") |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1545 |
: Stream.of(String.valueOf(k.id()) + " ")) |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1546 |
.filter(k -> k.startsWith(argPrefix)) |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1547 |
.map(ArgSuggestion::new) |
33362 | 1548 |
.collect(Collectors.toList()); |
1549 |
}; |
|
1550 |
} |
|
1551 |
||
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1552 |
// Completion based on snippet supplier with -all -start (and sometimes -history) options |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1553 |
private CompletionProvider snippetWithOptionCompletion(CompletionProvider optionProvider, |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1554 |
Supplier<Stream<? extends Snippet>> snippetsSupplier) { |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1555 |
return (code, cursor, anchor) -> { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1556 |
List<Suggestion> result = new ArrayList<>(); |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1557 |
int pastSpace = code.lastIndexOf(' ') + 1; // zero if no space |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1558 |
if (pastSpace == 0) { |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1559 |
result.addAll(optionProvider.completionSuggestions(code, cursor, anchor)); |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1560 |
} |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1561 |
result.addAll(snippetCompletion(snippetsSupplier).completionSuggestions(code, cursor, anchor)); |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1562 |
anchor[0] += pastSpace; |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1563 |
return result; |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1564 |
}; |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1565 |
} |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1566 |
|
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1567 |
// Completion of help, commands and subjects |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1568 |
private CompletionProvider helpCompletion() { |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1569 |
return (code, cursor, anchor) -> { |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1570 |
List<Suggestion> result; |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1571 |
int pastSpace = code.indexOf(' ') + 1; // zero if no space |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1572 |
if (pastSpace == 0) { |
44454
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
1573 |
// initially suggest commands (with slash) and subjects, |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
1574 |
// however, if their subject starts without slash, include |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
1575 |
// commands without slash |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
1576 |
boolean noslash = code.length() > 0 && !code.startsWith("/"); |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1577 |
result = new FixedCompletionProvider(commands.values().stream() |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1578 |
.filter(cmd -> cmd.kind.showInHelp || cmd.kind == CommandKind.HELP_SUBJECT) |
44454
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
1579 |
.map(c -> ((noslash && c.command.startsWith("/")) |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
1580 |
? c.command.substring(1) |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
1581 |
: c.command) + " ") |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1582 |
.toArray(String[]::new)) |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1583 |
.completionSuggestions(code, cursor, anchor); |
44569
b1accf8b2aed
8178013: Finetuning of merged tab and shift tab completion
jlahoda
parents:
44459
diff
changeset
|
1584 |
} else if (code.startsWith("/se") || code.startsWith("se")) { |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1585 |
result = new FixedCompletionProvider(SET_SUBCOMMANDS) |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1586 |
.completionSuggestions(code.substring(pastSpace), cursor - pastSpace, anchor); |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1587 |
} else { |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1588 |
result = Collections.emptyList(); |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1589 |
} |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1590 |
anchor[0] += pastSpace; |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1591 |
return result; |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1592 |
}; |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1593 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
1594 |
|
33362 | 1595 |
private static CompletionProvider saveCompletion() { |
1596 |
return (code, cursor, anchor) -> { |
|
1597 |
List<Suggestion> result = new ArrayList<>(); |
|
1598 |
int space = code.indexOf(' '); |
|
1599 |
if (space == (-1)) { |
|
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1600 |
result.addAll(SAVE_OPTION_COMPLETION_PROVIDER.completionSuggestions(code, cursor, anchor)); |
33362 | 1601 |
} |
1602 |
result.addAll(FILE_COMPLETION_PROVIDER.completionSuggestions(code.substring(space + 1), cursor - space - 1, anchor)); |
|
1603 |
anchor[0] += space + 1; |
|
1604 |
return result; |
|
1605 |
}; |
|
1606 |
} |
|
1607 |
||
43038 | 1608 |
// command-line-like option completion -- options with values |
1609 |
private static CompletionProvider optionCompletion(CompletionProvider provider) { |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1610 |
return (code, cursor, anchor) -> { |
43038 | 1611 |
Matcher ovm = OPTION_VALUE_PATTERN.matcher(code); |
1612 |
if (ovm.matches()) { |
|
1613 |
String flag = ovm.group("flag"); |
|
1614 |
List<CompletionProvider> ps = ARG_OPTIONS.entrySet().stream() |
|
1615 |
.filter(es -> es.getKey().startsWith(flag)) |
|
1616 |
.map(es -> es.getValue()) |
|
1617 |
.collect(toList()); |
|
1618 |
if (ps.size() == 1) { |
|
1619 |
int pastSpace = ovm.start("val"); |
|
1620 |
List<Suggestion> result = ps.get(0).completionSuggestions( |
|
1621 |
ovm.group("val"), cursor - pastSpace, anchor); |
|
1622 |
anchor[0] += pastSpace; |
|
1623 |
return result; |
|
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1624 |
} |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1625 |
} |
43038 | 1626 |
Matcher om = OPTION_PATTERN.matcher(code); |
1627 |
if (om.matches()) { |
|
1628 |
int pastSpace = om.start("flag"); |
|
1629 |
List<Suggestion> result = provider.completionSuggestions( |
|
1630 |
om.group("flag"), cursor - pastSpace, anchor); |
|
1631 |
if (!om.group("dd").isEmpty()) { |
|
1632 |
result = result.stream() |
|
1633 |
.map(sug -> new Suggestion() { |
|
1634 |
@Override |
|
1635 |
public String continuation() { |
|
1636 |
return "-" + sug.continuation(); |
|
1637 |
} |
|
1638 |
||
1639 |
@Override |
|
1640 |
public boolean matchesType() { |
|
1641 |
return false; |
|
1642 |
} |
|
1643 |
}) |
|
1644 |
.collect(toList()); |
|
1645 |
--pastSpace; |
|
1646 |
} |
|
1647 |
anchor[0] += pastSpace; |
|
1648 |
return result; |
|
1649 |
} |
|
1650 |
Matcher opp = OPTION_PRE_PATTERN.matcher(code); |
|
1651 |
if (opp.matches()) { |
|
1652 |
int pastSpace = opp.end(); |
|
1653 |
List<Suggestion> result = provider.completionSuggestions( |
|
1654 |
"", cursor - pastSpace, anchor); |
|
1655 |
anchor[0] += pastSpace; |
|
1656 |
return result; |
|
1657 |
} |
|
1658 |
return Collections.emptyList(); |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1659 |
}; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1660 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1661 |
|
48939
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
1662 |
// /history command completion |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
1663 |
private static CompletionProvider historyCompletion() { |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
1664 |
return optionCompletion(HISTORY_OPTION_COMPLETION_PROVIDER); |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
1665 |
} |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
1666 |
|
43038 | 1667 |
// /reload command completion |
1668 |
private static CompletionProvider reloadCompletion() { |
|
1669 |
return optionCompletion(RELOAD_OPTIONS_COMPLETION_PROVIDER); |
|
1670 |
} |
|
1671 |
||
1672 |
// /env command completion |
|
1673 |
private static CompletionProvider envCompletion() { |
|
1674 |
return optionCompletion(COMMAND_LINE_LIKE_OPTIONS_COMPLETION_PROVIDER); |
|
1675 |
} |
|
1676 |
||
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1677 |
private static CompletionProvider orMostSpecificCompletion( |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1678 |
CompletionProvider left, CompletionProvider right) { |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1679 |
return (code, cursor, anchor) -> { |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1680 |
int[] leftAnchor = {-1}; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1681 |
int[] rightAnchor = {-1}; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1682 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1683 |
List<Suggestion> leftSuggestions = left.completionSuggestions(code, cursor, leftAnchor); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1684 |
List<Suggestion> rightSuggestions = right.completionSuggestions(code, cursor, rightAnchor); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1685 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1686 |
List<Suggestion> suggestions = new ArrayList<>(); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1687 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1688 |
if (leftAnchor[0] >= rightAnchor[0]) { |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1689 |
anchor[0] = leftAnchor[0]; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1690 |
suggestions.addAll(leftSuggestions); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1691 |
} |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1692 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1693 |
if (leftAnchor[0] <= rightAnchor[0]) { |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1694 |
anchor[0] = rightAnchor[0]; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1695 |
suggestions.addAll(rightSuggestions); |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1696 |
} |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1697 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1698 |
return suggestions; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1699 |
}; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1700 |
} |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1701 |
|
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1702 |
// Snippet lists |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1703 |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1704 |
Stream<Snippet> allSnippets() { |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1705 |
return state.snippets(); |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1706 |
} |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1707 |
|
41514
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
41248
diff
changeset
|
1708 |
Stream<Snippet> dropableSnippets() { |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1709 |
return state.snippets() |
41514
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
41248
diff
changeset
|
1710 |
.filter(sn -> state.status(sn).isActive()); |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1711 |
} |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1712 |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1713 |
Stream<VarSnippet> allVarSnippets() { |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1714 |
return state.snippets() |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1715 |
.filter(sn -> sn.kind() == Snippet.Kind.VAR) |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1716 |
.map(sn -> (VarSnippet) sn); |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1717 |
} |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1718 |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1719 |
Stream<MethodSnippet> allMethodSnippets() { |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1720 |
return state.snippets() |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1721 |
.filter(sn -> sn.kind() == Snippet.Kind.METHOD) |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1722 |
.map(sn -> (MethodSnippet) sn); |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1723 |
} |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1724 |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1725 |
Stream<TypeDeclSnippet> allTypeSnippets() { |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1726 |
return state.snippets() |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1727 |
.filter(sn -> sn.kind() == Snippet.Kind.TYPE_DECL) |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
1728 |
.map(sn -> (TypeDeclSnippet) sn); |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1729 |
} |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1730 |
|
36990 | 1731 |
// Table of commands -- with command forms, argument kinds, helpKey message, implementation, ... |
33362 | 1732 |
|
1733 |
{ |
|
36990 | 1734 |
registerCommand(new Command("/list", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1735 |
this::cmdList, |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1736 |
snippetWithOptionCompletion(SNIPPET_HISTORY_OPTION_COMPLETION_PROVIDER, |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1737 |
this::allSnippets))); |
36990 | 1738 |
registerCommand(new Command("/edit", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1739 |
this::cmdEdit, |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1740 |
snippetWithOptionCompletion(SNIPPET_OPTION_COMPLETION_PROVIDER, |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1741 |
this::allSnippets))); |
36990 | 1742 |
registerCommand(new Command("/drop", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1743 |
this::cmdDrop, |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1744 |
snippetCompletion(this::dropableSnippets), |
35812 | 1745 |
CommandKind.REPLAY)); |
36990 | 1746 |
registerCommand(new Command("/save", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1747 |
this::cmdSave, |
35812 | 1748 |
saveCompletion())); |
36990 | 1749 |
registerCommand(new Command("/open", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1750 |
this::cmdOpen, |
35812 | 1751 |
FILE_COMPLETION_PROVIDER)); |
36990 | 1752 |
registerCommand(new Command("/vars", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1753 |
this::cmdVars, |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1754 |
snippetWithOptionCompletion(SNIPPET_OPTION_COMPLETION_PROVIDER, |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1755 |
this::allVarSnippets))); |
36990 | 1756 |
registerCommand(new Command("/methods", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1757 |
this::cmdMethods, |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1758 |
snippetWithOptionCompletion(SNIPPET_OPTION_COMPLETION_PROVIDER, |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1759 |
this::allMethodSnippets))); |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
1760 |
registerCommand(new Command("/types", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1761 |
this::cmdTypes, |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1762 |
snippetWithOptionCompletion(SNIPPET_OPTION_COMPLETION_PROVIDER, |
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1763 |
this::allTypeSnippets))); |
36990 | 1764 |
registerCommand(new Command("/imports", |
35812 | 1765 |
arg -> cmdImports(), |
1766 |
EMPTY_COMPLETION_PROVIDER)); |
|
36990 | 1767 |
registerCommand(new Command("/exit", |
47840 | 1768 |
arg -> cmdExit(arg), |
1769 |
(sn, c, a) -> { |
|
1770 |
if (analysis == null || sn.isEmpty()) { |
|
1771 |
// No completions if uninitialized or snippet not started |
|
1772 |
return Collections.emptyList(); |
|
1773 |
} else { |
|
1774 |
// Give exit code an int context by prefixing the arg |
|
1775 |
List<Suggestion> suggestions = analysis.completionSuggestions(INT_PREFIX + sn, |
|
1776 |
INT_PREFIX.length() + c, a); |
|
1777 |
a[0] -= INT_PREFIX.length(); |
|
1778 |
return suggestions; |
|
1779 |
} |
|
1780 |
})); |
|
43038 | 1781 |
registerCommand(new Command("/env", |
1782 |
arg -> cmdEnv(arg), |
|
1783 |
envCompletion())); |
|
36990 | 1784 |
registerCommand(new Command("/reset", |
43038 | 1785 |
arg -> cmdReset(arg), |
1786 |
envCompletion())); |
|
36990 | 1787 |
registerCommand(new Command("/reload", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1788 |
this::cmdReload, |
35812 | 1789 |
reloadCompletion())); |
36990 | 1790 |
registerCommand(new Command("/history", |
48939
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
1791 |
this::cmdHistory, |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
1792 |
historyCompletion())); |
36990 | 1793 |
registerCommand(new Command("/debug", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1794 |
this::cmdDebug, |
35812 | 1795 |
EMPTY_COMPLETION_PROVIDER, |
1796 |
CommandKind.HIDDEN)); |
|
36990 | 1797 |
registerCommand(new Command("/help", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1798 |
this::cmdHelp, |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1799 |
helpCompletion())); |
36990 | 1800 |
registerCommand(new Command("/set", |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1801 |
this::cmdSet, |
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1802 |
new ContinuousCompletionProvider(Map.of( |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1803 |
// need more completion for format for usability |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1804 |
"format", feedback.modeCompletions(), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1805 |
"truncation", feedback.modeCompletions(), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1806 |
"feedback", feedback.modeCompletions(), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1807 |
"mode", skipWordThenCompletion(orMostSpecificCompletion( |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1808 |
feedback.modeCompletions(SET_MODE_OPTIONS_COMPLETION_PROVIDER), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1809 |
SET_MODE_OPTIONS_COMPLETION_PROVIDER)), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1810 |
"prompt", feedback.modeCompletions(), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1811 |
"editor", fileCompletions(Files::isExecutable), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1812 |
"start", FILE_COMPLETION_PROVIDER), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1813 |
STARTSWITH_MATCHER))); |
36990 | 1814 |
registerCommand(new Command("/?", |
1815 |
"help.quest", |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1816 |
this::cmdHelp, |
42265
b36ad5a64e75
8153402: jshell tool: completion provider for /help
rfield
parents:
42259
diff
changeset
|
1817 |
helpCompletion(), |
36990 | 1818 |
CommandKind.NORMAL)); |
1819 |
registerCommand(new Command("/!", |
|
1820 |
"help.bang", |
|
35812 | 1821 |
arg -> cmdUseHistoryEntry(-1), |
36990 | 1822 |
EMPTY_COMPLETION_PROVIDER, |
1823 |
CommandKind.NORMAL)); |
|
35812 | 1824 |
|
1825 |
// Documentation pseudo-commands |
|
36990 | 1826 |
registerCommand(new Command("/<id>", |
48349
3d4e8f5a2a69
8179858: jshell tool: sync nomenclature from reference to online /help
rfield
parents:
48292
diff
changeset
|
1827 |
"help.slashID", |
45602
7aeef976cb06
8180510: jshell tool: crash on entering pseudo-commands: /<id> or /-<n>
rfield
parents:
45215
diff
changeset
|
1828 |
arg -> cmdHelp("rerun"), |
7aeef976cb06
8180510: jshell tool: crash on entering pseudo-commands: /<id> or /-<n>
rfield
parents:
45215
diff
changeset
|
1829 |
EMPTY_COMPLETION_PROVIDER, |
35812 | 1830 |
CommandKind.HELP_ONLY)); |
36990 | 1831 |
registerCommand(new Command("/-<n>", |
1832 |
"help.previous", |
|
45602
7aeef976cb06
8180510: jshell tool: crash on entering pseudo-commands: /<id> or /-<n>
rfield
parents:
45215
diff
changeset
|
1833 |
arg -> cmdHelp("rerun"), |
7aeef976cb06
8180510: jshell tool: crash on entering pseudo-commands: /<id> or /-<n>
rfield
parents:
45215
diff
changeset
|
1834 |
EMPTY_COMPLETION_PROVIDER, |
35812 | 1835 |
CommandKind.HELP_ONLY)); |
36990 | 1836 |
registerCommand(new Command("intro", |
1837 |
"help.intro", |
|
35812 | 1838 |
CommandKind.HELP_SUBJECT)); |
48349
3d4e8f5a2a69
8179858: jshell tool: sync nomenclature from reference to online /help
rfield
parents:
48292
diff
changeset
|
1839 |
registerCommand(new Command("id", |
3d4e8f5a2a69
8179858: jshell tool: sync nomenclature from reference to online /help
rfield
parents:
48292
diff
changeset
|
1840 |
"help.id", |
3d4e8f5a2a69
8179858: jshell tool: sync nomenclature from reference to online /help
rfield
parents:
48292
diff
changeset
|
1841 |
CommandKind.HELP_SUBJECT)); |
36990 | 1842 |
registerCommand(new Command("shortcuts", |
1843 |
"help.shortcuts", |
|
35812 | 1844 |
CommandKind.HELP_SUBJECT)); |
43038 | 1845 |
registerCommand(new Command("context", |
1846 |
"help.context", |
|
1847 |
CommandKind.HELP_SUBJECT)); |
|
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1848 |
registerCommand(new Command("rerun", |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1849 |
"help.rerun", |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
1850 |
CommandKind.HELP_SUBJECT)); |
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1851 |
|
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1852 |
commandCompletions = new ContinuousCompletionProvider( |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1853 |
commands.values().stream() |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1854 |
.filter(c -> c.kind.shouldSuggestCompletions) |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1855 |
.collect(toMap(c -> c.command, c -> c.completions)), |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1856 |
STARTSWITH_MATCHER); |
33362 | 1857 |
} |
1858 |
||
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1859 |
private ContinuousCompletionProvider commandCompletions; |
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1860 |
|
33362 | 1861 |
public List<Suggestion> commandCompletionSuggestions(String code, int cursor, int[] anchor) { |
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
1862 |
return commandCompletions.completionSuggestions(code, cursor, anchor); |
33362 | 1863 |
} |
1864 |
||
44459 | 1865 |
public List<String> commandDocumentation(String code, int cursor, boolean shortDescription) { |
47931 | 1866 |
code = code.substring(0, cursor).replaceAll("\\h+", " "); |
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1867 |
String stripped = code.replaceFirst("/(he(lp?)?|\\?) ", ""); |
47931 | 1868 |
boolean inHelp = !code.equals(stripped); |
1869 |
int space = stripped.indexOf(' '); |
|
1870 |
String prefix = space != (-1) ? stripped.substring(0, space) : stripped; |
|
44459 | 1871 |
List<String> result = new ArrayList<>(); |
1872 |
||
47931 | 1873 |
List<Entry<String, String>> toShow; |
1874 |
||
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1875 |
if (SET_SUB.matcher(stripped).matches()) { |
47931 | 1876 |
String setSubcommand = stripped.replaceFirst("/?set ([^ ]*)($| .*)", "$1"); |
1877 |
toShow = |
|
1878 |
Arrays.stream(SET_SUBCOMMANDS) |
|
1879 |
.filter(s -> s.startsWith(setSubcommand)) |
|
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1880 |
.map(s -> new SimpleEntry<>("/set " + s, "help.set." + s)) |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1881 |
.collect(toList()); |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1882 |
} else if (RERUN_ID.matcher(stripped).matches()) { |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1883 |
toShow = |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1884 |
singletonList(new SimpleEntry<>("/<id>", "help.rerun")); |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1885 |
} else if (RERUN_PREVIOUS.matcher(stripped).matches()) { |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1886 |
toShow = |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1887 |
singletonList(new SimpleEntry<>("/-<n>", "help.rerun")); |
47931 | 1888 |
} else { |
1889 |
toShow = |
|
1890 |
commands.values() |
|
44459 | 1891 |
.stream() |
47931 | 1892 |
.filter(c -> c.command.startsWith(prefix) |
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1893 |
|| c.command.substring(1).startsWith(prefix)) |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1894 |
.filter(c -> c.kind.showInHelp |
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1895 |
|| (inHelp && c.kind == CommandKind.HELP_SUBJECT)) |
47931 | 1896 |
.sorted((c1, c2) -> c1.command.compareTo(c2.command)) |
1897 |
.map(c -> new SimpleEntry<>(c.command, c.helpKey)) |
|
48275
b2190c70a1ac
8192863: jshell tool: /<id><tab> gives "No such command"
rfield
parents:
48273
diff
changeset
|
1898 |
.collect(toList()); |
47931 | 1899 |
} |
1900 |
||
1901 |
if (toShow.size() == 1 && !inHelp) { |
|
1902 |
result.add(getResourceString(toShow.get(0).getValue() + (shortDescription ? ".summary" : ""))); |
|
44459 | 1903 |
} else { |
47931 | 1904 |
for (Entry<String, String> e : toShow) { |
1905 |
result.add(e.getKey() + "\n" + getResourceString(e.getValue() + (shortDescription ? ".summary" : ""))); |
|
33362 | 1906 |
} |
1907 |
} |
|
1908 |
||
44459 | 1909 |
return result; |
33362 | 1910 |
} |
1911 |
||
44188
3f2047e62102
8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents:
44065
diff
changeset
|
1912 |
// Attempt to stop currently running evaluation |
3f2047e62102
8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents:
44065
diff
changeset
|
1913 |
void stop() { |
3f2047e62102
8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents:
44065
diff
changeset
|
1914 |
state.stop(); |
3f2047e62102
8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents:
44065
diff
changeset
|
1915 |
} |
3f2047e62102
8176412: jshell tool: automatic imports are excluded on /reload causing it to fail
rfield
parents:
44065
diff
changeset
|
1916 |
|
33362 | 1917 |
// --- Command implementations --- |
1918 |
||
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
1919 |
private static final String[] SET_SUBCOMMANDS = new String[]{ |
38539 | 1920 |
"format", "truncation", "feedback", "mode", "prompt", "editor", "start"}; |
36494 | 1921 |
|
1922 |
final boolean cmdSet(String arg) { |
|
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1923 |
String cmd = "/set"; |
38539 | 1924 |
ArgTokenizer at = new ArgTokenizer(cmd, arg.trim()); |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1925 |
String which = subCommand(cmd, at, SET_SUBCOMMANDS); |
36494 | 1926 |
if (which == null) { |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
1927 |
return false; |
36494 | 1928 |
} |
1929 |
switch (which) { |
|
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1930 |
case "_retain": { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1931 |
errormsg("jshell.err.setting.to.retain.must.be.specified", at.whole()); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1932 |
return false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1933 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1934 |
case "_blank": { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1935 |
// show top-level settings |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1936 |
new SetEditor().set(); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1937 |
showSetStart(); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1938 |
setFeedback(this, at); // no args so shows feedback setting |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1939 |
hardmsg("jshell.msg.set.show.mode.settings"); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1940 |
return true; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1941 |
} |
36494 | 1942 |
case "format": |
1943 |
return feedback.setFormat(this, at); |
|
38513 | 1944 |
case "truncation": |
1945 |
return feedback.setTruncation(this, at); |
|
36494 | 1946 |
case "feedback": |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1947 |
return setFeedback(this, at); |
38539 | 1948 |
case "mode": |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1949 |
return feedback.setMode(this, at, |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1950 |
retained -> prefs.put(MODE_KEY, retained)); |
36494 | 1951 |
case "prompt": |
1952 |
return feedback.setPrompt(this, at); |
|
38539 | 1953 |
case "editor": |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1954 |
return new SetEditor(at).set(); |
38539 | 1955 |
case "start": |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1956 |
return setStart(at); |
36494 | 1957 |
default: |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1958 |
errormsg("jshell.err.arg", cmd, at.val()); |
36494 | 1959 |
return false; |
33362 | 1960 |
} |
1961 |
} |
|
1962 |
||
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1963 |
boolean setFeedback(MessageHandler messageHandler, ArgTokenizer at) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1964 |
return feedback.setFeedback(messageHandler, at, |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1965 |
fb -> prefs.put(FEEDBACK_KEY, fb)); |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1966 |
} |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1967 |
|
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1968 |
// Find which, if any, sub-command matches. |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1969 |
// Return null on error |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1970 |
String subCommand(String cmd, ArgTokenizer at, String[] subs) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1971 |
at.allowedOptions("-retain"); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1972 |
String sub = at.next(); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1973 |
if (sub == null) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1974 |
// No sub-command was given |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1975 |
return at.hasOption("-retain") |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1976 |
? "_retain" |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1977 |
: "_blank"; |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1978 |
} |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1979 |
String[] matches = Arrays.stream(subs) |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1980 |
.filter(s -> s.startsWith(sub)) |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
1981 |
.toArray(String[]::new); |
36990 | 1982 |
if (matches.length == 0) { |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1983 |
// There are no matching sub-commands |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1984 |
errormsg("jshell.err.arg", cmd, sub); |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1985 |
fluffmsg("jshell.msg.use.one.of", Arrays.stream(subs) |
36494 | 1986 |
.collect(Collectors.joining(", ")) |
1987 |
); |
|
1988 |
return null; |
|
36990 | 1989 |
} |
1990 |
if (matches.length > 1) { |
|
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
1991 |
// More than one sub-command matches the initial characters provided |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
1992 |
errormsg("jshell.err.sub.ambiguous", cmd, sub); |
36990 | 1993 |
fluffmsg("jshell.msg.use.one.of", Arrays.stream(matches) |
36494 | 1994 |
.collect(Collectors.joining(", ")) |
1995 |
); |
|
1996 |
return null; |
|
1997 |
} |
|
1998 |
return matches[0]; |
|
1999 |
} |
|
2000 |
||
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2001 |
static class EditorSetting { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2002 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2003 |
static String BUILT_IN_REP = "-default"; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2004 |
static char WAIT_PREFIX = '-'; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2005 |
static char NORMAL_PREFIX = '*'; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2006 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2007 |
final String[] cmd; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2008 |
final boolean wait; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2009 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2010 |
EditorSetting(String[] cmd, boolean wait) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2011 |
this.wait = wait; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2012 |
this.cmd = cmd; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2013 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2014 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2015 |
// returns null if not stored in preferences |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
2016 |
static EditorSetting fromPrefs(PersistentStorage prefs) { |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2017 |
// Read retained editor setting (if any) |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
2018 |
String editorString = prefs.get(EDITOR_KEY); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2019 |
if (editorString == null || editorString.isEmpty()) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2020 |
return null; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2021 |
} else if (editorString.equals(BUILT_IN_REP)) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2022 |
return BUILT_IN_EDITOR; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2023 |
} else { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2024 |
boolean wait = false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2025 |
char waitMarker = editorString.charAt(0); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2026 |
if (waitMarker == WAIT_PREFIX || waitMarker == NORMAL_PREFIX) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2027 |
wait = waitMarker == WAIT_PREFIX; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2028 |
editorString = editorString.substring(1); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2029 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2030 |
String[] cmd = editorString.split(RECORD_SEPARATOR); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2031 |
return new EditorSetting(cmd, wait); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2032 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2033 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2034 |
|
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
2035 |
static void removePrefs(PersistentStorage prefs) { |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2036 |
prefs.remove(EDITOR_KEY); |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2037 |
} |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2038 |
|
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
2039 |
void toPrefs(PersistentStorage prefs) { |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2040 |
prefs.put(EDITOR_KEY, (this == BUILT_IN_EDITOR) |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2041 |
? BUILT_IN_REP |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2042 |
: (wait ? WAIT_PREFIX : NORMAL_PREFIX) + String.join(RECORD_SEPARATOR, cmd)); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2043 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2044 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2045 |
@Override |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2046 |
public boolean equals(Object o) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2047 |
if (o instanceof EditorSetting) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2048 |
EditorSetting ed = (EditorSetting) o; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2049 |
return Arrays.equals(cmd, ed.cmd) && wait == ed.wait; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2050 |
} else { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2051 |
return false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2052 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2053 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2054 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2055 |
@Override |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2056 |
public int hashCode() { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2057 |
int hash = 7; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2058 |
hash = 71 * hash + Arrays.deepHashCode(this.cmd); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2059 |
hash = 71 * hash + (this.wait ? 1 : 0); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2060 |
return hash; |
38539 | 2061 |
} |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2062 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2063 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2064 |
class SetEditor { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2065 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2066 |
private final ArgTokenizer at; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2067 |
private final String[] command; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2068 |
private final boolean hasCommand; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2069 |
private final boolean defaultOption; |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2070 |
private final boolean deleteOption; |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2071 |
private final boolean waitOption; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2072 |
private final boolean retainOption; |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2073 |
private final int primaryOptionCount; |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2074 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2075 |
SetEditor(ArgTokenizer at) { |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2076 |
at.allowedOptions("-default", "-wait", "-retain", "-delete"); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2077 |
String prog = at.next(); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2078 |
List<String> ed = new ArrayList<>(); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2079 |
while (at.val() != null) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2080 |
ed.add(at.val()); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2081 |
at.nextToken(); // so that options are not interpreted as jshell options |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2082 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2083 |
this.at = at; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2084 |
this.command = ed.toArray(new String[ed.size()]); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2085 |
this.hasCommand = command.length > 0; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2086 |
this.defaultOption = at.hasOption("-default"); |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2087 |
this.deleteOption = at.hasOption("-delete"); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2088 |
this.waitOption = at.hasOption("-wait"); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2089 |
this.retainOption = at.hasOption("-retain"); |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2090 |
this.primaryOptionCount = (hasCommand? 1 : 0) + (defaultOption? 1 : 0) + (deleteOption? 1 : 0); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2091 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2092 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2093 |
SetEditor() { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2094 |
this(new ArgTokenizer("", "")); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2095 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2096 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2097 |
boolean set() { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2098 |
if (!check()) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2099 |
return false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2100 |
} |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2101 |
if (primaryOptionCount == 0 && !retainOption) { |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2102 |
// No settings or -retain, so this is a query |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2103 |
EditorSetting retained = EditorSetting.fromPrefs(prefs); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2104 |
if (retained != null) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2105 |
// retained editor is set |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2106 |
hard("/set editor -retain %s", format(retained)); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2107 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2108 |
if (retained == null || !retained.equals(editor)) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2109 |
// editor is not retained or retained is different from set |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2110 |
hard("/set editor %s", format(editor)); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2111 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2112 |
return true; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2113 |
} |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2114 |
if (retainOption && deleteOption) { |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2115 |
EditorSetting.removePrefs(prefs); |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2116 |
} |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2117 |
install(); |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2118 |
if (retainOption && !deleteOption) { |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2119 |
editor.toPrefs(prefs); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2120 |
fluffmsg("jshell.msg.set.editor.retain", format(editor)); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2121 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2122 |
return true; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2123 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2124 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2125 |
private boolean check() { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2126 |
if (!checkOptionsAndRemainingInput(at)) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2127 |
return false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2128 |
} |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2129 |
if (primaryOptionCount > 1) { |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2130 |
errormsg("jshell.err.default.option.or.program", at.whole()); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2131 |
return false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2132 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2133 |
if (waitOption && !hasCommand) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2134 |
errormsg("jshell.err.wait.applies.to.external.editor", at.whole()); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2135 |
return false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2136 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2137 |
return true; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2138 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2139 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2140 |
private void install() { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2141 |
if (hasCommand) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2142 |
editor = new EditorSetting(command, waitOption); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2143 |
} else if (defaultOption) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2144 |
editor = BUILT_IN_EDITOR; |
41641
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2145 |
} else if (deleteOption) { |
a628785b9dd9
8167637: jshell tool: /edit should use EDITOR setting
rfield
parents:
41635
diff
changeset
|
2146 |
configEditor(); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2147 |
} else { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2148 |
return; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2149 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2150 |
fluffmsg("jshell.msg.set.editor.set", format(editor)); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2151 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2152 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2153 |
private String format(EditorSetting ed) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2154 |
if (ed == BUILT_IN_EDITOR) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2155 |
return "-default"; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2156 |
} else { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2157 |
Stream<String> elems = Arrays.stream(ed.cmd); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2158 |
if (ed.wait) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2159 |
elems = Stream.concat(Stream.of("-wait"), elems); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2160 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2161 |
return elems.collect(joining(" ")); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2162 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2163 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2164 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2165 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2166 |
// The sub-command: /set start <start-file> |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2167 |
boolean setStart(ArgTokenizer at) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2168 |
at.allowedOptions("-default", "-none", "-retain"); |
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
2169 |
List<String> fns = new ArrayList<>(); |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
2170 |
while (at.next() != null) { |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
2171 |
fns.add(at.val()); |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
2172 |
} |
38539 | 2173 |
if (!checkOptionsAndRemainingInput(at)) { |
2174 |
return false; |
|
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
2175 |
} |
38539 | 2176 |
boolean defaultOption = at.hasOption("-default"); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2177 |
boolean noneOption = at.hasOption("-none"); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2178 |
boolean retainOption = at.hasOption("-retain"); |
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
2179 |
boolean hasFile = !fns.isEmpty(); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2180 |
|
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2181 |
int argCount = (defaultOption ? 1 : 0) + (noneOption ? 1 : 0) + (hasFile ? 1 : 0); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2182 |
if (argCount > 1) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2183 |
errormsg("jshell.err.option.or.filename", at.whole()); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2184 |
return false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2185 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2186 |
if (argCount == 0 && !retainOption) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2187 |
// no options or filename, show current setting |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2188 |
showSetStart(); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2189 |
return true; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2190 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2191 |
if (hasFile) { |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2192 |
startup = Startup.fromFileList(fns, "/set start", this); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2193 |
if (startup == null) { |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2194 |
return false; |
38539 | 2195 |
} |
2196 |
} else if (defaultOption) { |
|
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2197 |
startup = Startup.defaultStartup(this); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2198 |
} else if (noneOption) { |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2199 |
startup = Startup.noStartup(); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2200 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2201 |
if (retainOption) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2202 |
// retain startup setting |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2203 |
prefs.put(STARTUP_KEY, startup.storedForm()); |
38539 | 2204 |
} |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
2205 |
return true; |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
2206 |
} |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
2207 |
|
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2208 |
// show the "/set start" settings (retained and, if different, current) |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2209 |
// as commands (and file contents). All commands first, then contents. |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2210 |
void showSetStart() { |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2211 |
StringBuilder sb = new StringBuilder(); |
42843
a8d83044a192
8170162: jshell tool: no mechanism to programmatically launch
rfield
parents:
42827
diff
changeset
|
2212 |
String retained = prefs.get(STARTUP_KEY); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2213 |
if (retained != null) { |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2214 |
Startup retainedStart = Startup.unpack(retained, this); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2215 |
boolean currentDifferent = !startup.equals(retainedStart); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2216 |
sb.append(retainedStart.show(true)); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2217 |
if (currentDifferent) { |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2218 |
sb.append(startup.show(false)); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2219 |
} |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2220 |
sb.append(retainedStart.showDetail()); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2221 |
if (currentDifferent) { |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2222 |
sb.append(startup.showDetail()); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2223 |
} |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2224 |
} else { |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2225 |
sb.append(startup.show(false)); |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2226 |
sb.append(startup.showDetail()); |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
2227 |
} |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
2228 |
hard(sb.toString()); |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
2229 |
} |
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
2230 |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2231 |
boolean cmdDebug(String arg) { |
33362 | 2232 |
if (arg.isEmpty()) { |
2233 |
debug = !debug; |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
2234 |
InternalDebugControl.setDebugFlags(state, debug ? DBG_GEN : 0); |
33362 | 2235 |
fluff("Debugging %s", debug ? "on" : "off"); |
2236 |
} else { |
|
2237 |
int flags = 0; |
|
2238 |
for (char ch : arg.toCharArray()) { |
|
2239 |
switch (ch) { |
|
2240 |
case '0': |
|
2241 |
flags = 0; |
|
2242 |
debug = false; |
|
2243 |
fluff("Debugging off"); |
|
2244 |
break; |
|
2245 |
case 'r': |
|
2246 |
debug = true; |
|
2247 |
fluff("REPL tool debugging on"); |
|
2248 |
break; |
|
2249 |
case 'g': |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
2250 |
flags |= DBG_GEN; |
33362 | 2251 |
fluff("General debugging on"); |
2252 |
break; |
|
2253 |
case 'f': |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
2254 |
flags |= DBG_FMGR; |
33362 | 2255 |
fluff("File manager debugging on"); |
2256 |
break; |
|
2257 |
case 'c': |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
2258 |
flags |= DBG_COMPA; |
33362 | 2259 |
fluff("Completion analysis debugging on"); |
2260 |
break; |
|
2261 |
case 'd': |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
2262 |
flags |= DBG_DEP; |
33362 | 2263 |
fluff("Dependency debugging on"); |
2264 |
break; |
|
2265 |
case 'e': |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38531
diff
changeset
|
2266 |
flags |= DBG_EVNT; |
33362 | 2267 |
fluff("Event debugging on"); |
2268 |
break; |
|
43134
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
43038
diff
changeset
|
2269 |
case 'w': |
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
43038
diff
changeset
|
2270 |
flags |= DBG_WRAP; |
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
43038
diff
changeset
|
2271 |
fluff("Wrap debugging on"); |
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
43038
diff
changeset
|
2272 |
break; |
33362 | 2273 |
default: |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2274 |
error("Unknown debugging option: %c", ch); |
43134
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
43038
diff
changeset
|
2275 |
fluff("Use: 0 r g f c d e w"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2276 |
return false; |
33362 | 2277 |
} |
2278 |
} |
|
2279 |
InternalDebugControl.setDebugFlags(state, flags); |
|
2280 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2281 |
return true; |
33362 | 2282 |
} |
2283 |
||
47840 | 2284 |
private boolean cmdExit(String arg) { |
2285 |
if (!arg.trim().isEmpty()) { |
|
2286 |
debug("Compiling exit: %s", arg); |
|
2287 |
List<SnippetEvent> events = state.eval(arg); |
|
2288 |
for (SnippetEvent e : events) { |
|
2289 |
// Only care about main snippet |
|
2290 |
if (e.causeSnippet() == null) { |
|
2291 |
Snippet sn = e.snippet(); |
|
2292 |
||
2293 |
// Show any diagnostics |
|
2294 |
List<Diag> diagnostics = state.diagnostics(sn).collect(toList()); |
|
2295 |
String source = sn.source(); |
|
2296 |
displayDiagnostics(source, diagnostics); |
|
2297 |
||
2298 |
// Show any exceptions |
|
2299 |
if (e.exception() != null && e.status() != Status.REJECTED) { |
|
2300 |
if (displayException(e.exception())) { |
|
2301 |
// Abort: an exception occurred (reported) |
|
2302 |
return false; |
|
2303 |
} |
|
2304 |
} |
|
2305 |
||
2306 |
if (e.status() != Status.VALID) { |
|
2307 |
// Abort: can only use valid snippets, diagnostics have been reported (above) |
|
2308 |
return false; |
|
2309 |
} |
|
2310 |
String typeName; |
|
2311 |
if (sn.kind() == Kind.EXPRESSION) { |
|
2312 |
typeName = ((ExpressionSnippet) sn).typeName(); |
|
2313 |
} else if (sn.subKind() == TEMP_VAR_EXPRESSION_SUBKIND) { |
|
2314 |
typeName = ((VarSnippet) sn).typeName(); |
|
2315 |
} else { |
|
2316 |
// Abort: not an expression |
|
2317 |
errormsg("jshell.err.exit.not.expression", arg); |
|
2318 |
return false; |
|
2319 |
} |
|
2320 |
switch (typeName) { |
|
2321 |
case "int": |
|
2322 |
case "Integer": |
|
2323 |
case "byte": |
|
2324 |
case "Byte": |
|
2325 |
case "short": |
|
2326 |
case "Short": |
|
2327 |
try { |
|
2328 |
int i = Integer.parseInt(e.value()); |
|
2329 |
/** |
|
2330 |
addToReplayHistory("/exit " + arg); |
|
2331 |
replayableHistory.storeHistory(prefs); |
|
2332 |
closeState(); |
|
2333 |
try { |
|
2334 |
input.close(); |
|
2335 |
} catch (Exception exc) { |
|
2336 |
// ignore |
|
2337 |
} |
|
2338 |
* **/ |
|
2339 |
exitCode = i; |
|
2340 |
break; |
|
2341 |
} catch (NumberFormatException exc) { |
|
2342 |
// Abort: bad value |
|
2343 |
errormsg("jshell.err.exit.bad.value", arg, e.value()); |
|
2344 |
return false; |
|
2345 |
} |
|
2346 |
default: |
|
2347 |
// Abort: bad type |
|
2348 |
errormsg("jshell.err.exit.bad.type", arg, typeName); |
|
2349 |
return false; |
|
2350 |
} |
|
2351 |
} |
|
2352 |
} |
|
2353 |
} |
|
33362 | 2354 |
regenerateOnDeath = false; |
2355 |
live = false; |
|
47840 | 2356 |
if (exitCode == 0) { |
2357 |
fluffmsg("jshell.msg.goodbye"); |
|
2358 |
} else { |
|
2359 |
fluffmsg("jshell.msg.goodbye.value", exitCode); |
|
2360 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2361 |
return true; |
33362 | 2362 |
} |
2363 |
||
36494 | 2364 |
boolean cmdHelp(String arg) { |
38539 | 2365 |
ArgTokenizer at = new ArgTokenizer("/help", arg); |
36494 | 2366 |
String subject = at.next(); |
2367 |
if (subject != null) { |
|
44454
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2368 |
// check if the requested subject is a help subject or |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2369 |
// a command, with or without slash |
36494 | 2370 |
Command[] matches = commands.values().stream() |
44454
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2371 |
.filter(c -> c.command.startsWith(subject) |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2372 |
|| c.command.substring(1).startsWith(subject)) |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
2373 |
.toArray(Command[]::new); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2374 |
if (matches.length == 1) { |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
2375 |
String cmd = matches[0].command; |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2376 |
if (cmd.equals("/set")) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2377 |
// Print the help doc for the specified sub-command |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2378 |
String which = subCommand(cmd, at, SET_SUBCOMMANDS); |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2379 |
if (which == null) { |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2380 |
return false; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2381 |
} |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2382 |
if (!which.equals("_blank")) { |
48273
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2383 |
printHelp("/set " + which, "help.set." + which); |
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2384 |
return true; |
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2385 |
} |
38531
c449daa25b45
8157200: jshell tool: Add /retain command to persist settings
rfield
parents:
38520
diff
changeset
|
2386 |
} |
36494 | 2387 |
} |
2388 |
if (matches.length > 0) { |
|
2389 |
for (Command c : matches) { |
|
48273
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2390 |
printHelp(c.command, c.helpKey); |
36494 | 2391 |
} |
2392 |
return true; |
|
2393 |
} else { |
|
44454
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2394 |
// failing everything else, check if this is the start of |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2395 |
// a /set sub-command name |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2396 |
String[] subs = Arrays.stream(SET_SUBCOMMANDS) |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2397 |
.filter(s -> s.startsWith(subject)) |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2398 |
.toArray(String[]::new); |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2399 |
if (subs.length > 0) { |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2400 |
for (String sub : subs) { |
48273
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2401 |
printHelp("/set " + sub, "help.set." + sub); |
44454
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2402 |
} |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2403 |
return true; |
74af976d6798
8177079: jshell tool: usability of /help for commands and sub-commands
rfield
parents:
44188
diff
changeset
|
2404 |
} |
36990 | 2405 |
errormsg("jshell.err.help.arg", arg); |
36494 | 2406 |
} |
33362 | 2407 |
} |
36990 | 2408 |
hardmsg("jshell.msg.help.begin"); |
36494 | 2409 |
hardPairs(commands.values().stream() |
2410 |
.filter(cmd -> cmd.kind.showInHelp), |
|
36990 | 2411 |
cmd -> cmd.command + " " + getResourceString(cmd.helpKey + ".args"), |
2412 |
cmd -> getResourceString(cmd.helpKey + ".summary") |
|
36494 | 2413 |
); |
36990 | 2414 |
hardmsg("jshell.msg.help.subject"); |
36494 | 2415 |
hardPairs(commands.values().stream() |
2416 |
.filter(cmd -> cmd.kind == CommandKind.HELP_SUBJECT), |
|
2417 |
cmd -> cmd.command, |
|
36990 | 2418 |
cmd -> getResourceString(cmd.helpKey + ".summary") |
36494 | 2419 |
); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2420 |
return true; |
33362 | 2421 |
} |
2422 |
||
48273
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2423 |
private void printHelp(String name, String key) { |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2424 |
int len = name.length(); |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2425 |
String centered = "%" + ((OUTPUT_WIDTH + len) / 2) + "s"; |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2426 |
hard(""); |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2427 |
hard(centered, name); |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2428 |
hard(centered, Stream.generate(() -> "=").limit(len).collect(Collectors.joining())); |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2429 |
hard(""); |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2430 |
hardrb(key); |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2431 |
} |
e2065f7505eb
8192979: jshell tool: Online help text for commands is confusing
rfield
parents:
47931
diff
changeset
|
2432 |
|
48939
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
2433 |
private boolean cmdHistory(String rawArgs) { |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
2434 |
ArgTokenizer at = new ArgTokenizer("/history", rawArgs.trim()); |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
2435 |
at.allowedOptions("-all"); |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
2436 |
if (!checkOptionsAndRemainingInput(at)) { |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
2437 |
return false; |
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
2438 |
} |
33362 | 2439 |
cmdout.println(); |
48939
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
2440 |
for (String s : input.history(!at.hasOption("-all"))) { |
33362 | 2441 |
// No number prefix, confusing with snippet ids |
2442 |
cmdout.printf("%s\n", s); |
|
2443 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2444 |
return true; |
33362 | 2445 |
} |
2446 |
||
2447 |
/** |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2448 |
* Avoid parameterized varargs possible heap pollution warning. |
33362 | 2449 |
*/ |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2450 |
private interface SnippetPredicate<T extends Snippet> extends Predicate<T> { } |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2451 |
|
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2452 |
/** |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2453 |
* 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
|
2454 |
* Adapted from Stuart Marks |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2455 |
* |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2456 |
* @param supplier Supply the Snippet stream to filter |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2457 |
* @param filters Filters to attempt |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2458 |
* @return The non-empty filtered Stream, or null |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2459 |
*/ |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2460 |
@SafeVarargs |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2461 |
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
|
2462 |
SnippetPredicate<T>... filters) { |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2463 |
for (SnippetPredicate<T> filt : filters) { |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2464 |
Iterator<T> iterator = supplier.get().filter(filt).iterator(); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2465 |
if (iterator.hasNext()) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2466 |
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false); |
33362 | 2467 |
} |
2468 |
} |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2469 |
return null; |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2470 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2471 |
|
35812 | 2472 |
private boolean inStartUp(Snippet sn) { |
2473 |
return mapSnippet.get(sn).space == startNamespace; |
|
2474 |
} |
|
2475 |
||
2476 |
private boolean isActive(Snippet sn) { |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38613
diff
changeset
|
2477 |
return state.status(sn).isActive(); |
35812 | 2478 |
} |
2479 |
||
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2480 |
private boolean mainActive(Snippet sn) { |
35812 | 2481 |
return !inStartUp(sn) && isActive(sn); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2482 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2483 |
|
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2484 |
private boolean matchingDeclaration(Snippet sn, String name) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2485 |
return sn instanceof DeclarationSnippet |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2486 |
&& ((DeclarationSnippet) sn).name().equals(name); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2487 |
} |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2488 |
|
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2489 |
/** |
38539 | 2490 |
* Convert user arguments to a Stream of snippets referenced by those |
2491 |
* arguments (or lack of arguments). |
|
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2492 |
* |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2493 |
* @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
|
2494 |
* @param defFilter the filter to apply to the arguments if no argument |
38539 | 2495 |
* @param rawargs the user's argument to the command, maybe be the empty |
2496 |
* string |
|
2497 |
* @return a Stream of referenced snippets or null if no matches are found |
|
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2498 |
*/ |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
2499 |
private <T extends Snippet> Stream<T> argsOptionsToSnippets(Supplier<Stream<T>> snippetSupplier, |
38539 | 2500 |
Predicate<Snippet> defFilter, String rawargs, String cmd) { |
2501 |
ArgTokenizer at = new ArgTokenizer(cmd, rawargs.trim()); |
|
2502 |
at.allowedOptions("-all", "-start"); |
|
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2503 |
return argsOptionsToSnippets(snippetSupplier, defFilter, at); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2504 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2505 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2506 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2507 |
* Convert user arguments to a Stream of snippets referenced by those |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2508 |
* arguments (or lack of arguments). |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2509 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2510 |
* @param snippets the base list of possible snippets |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2511 |
* @param defFilter the filter to apply to the arguments if no argument |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2512 |
* @param at the ArgTokenizer, with allowed options set |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2513 |
* @return |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2514 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2515 |
private <T extends Snippet> Stream<T> argsOptionsToSnippets(Supplier<Stream<T>> snippetSupplier, |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2516 |
Predicate<Snippet> defFilter, ArgTokenizer at) { |
38539 | 2517 |
List<String> args = new ArrayList<>(); |
2518 |
String s; |
|
2519 |
while ((s = at.next()) != null) { |
|
2520 |
args.add(s); |
|
2521 |
} |
|
2522 |
if (!checkOptionsAndRemainingInput(at)) { |
|
2523 |
return null; |
|
2524 |
} |
|
2525 |
if (at.optionCount() > 0 && args.size() > 0) { |
|
2526 |
errormsg("jshell.err.may.not.specify.options.and.snippets", at.whole()); |
|
2527 |
return null; |
|
2528 |
} |
|
2529 |
if (at.optionCount() > 1) { |
|
2530 |
errormsg("jshell.err.conflicting.options", at.whole()); |
|
2531 |
return null; |
|
2532 |
} |
|
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2533 |
if (at.isAllowedOption("-all") && at.hasOption("-all")) { |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2534 |
// all snippets including start-up, failed, and overwritten |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
2535 |
return snippetSupplier.get(); |
38539 | 2536 |
} |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2537 |
if (at.isAllowedOption("-start") && at.hasOption("-start")) { |
35812 | 2538 |
// start-up snippets |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
2539 |
return snippetSupplier.get() |
35812 | 2540 |
.filter(this::inStartUp); |
38539 | 2541 |
} |
2542 |
if (args.isEmpty()) { |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2543 |
// Default is all active user snippets |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
2544 |
return snippetSupplier.get() |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2545 |
.filter(defFilter); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2546 |
} |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2547 |
return new ArgToSnippets<>(snippetSupplier).argsToSnippets(args); |
33362 | 2548 |
} |
2549 |
||
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2550 |
/** |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2551 |
* Support for converting arguments that are definition names, snippet ids, |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2552 |
* or snippet id ranges into a stream of snippets, |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2553 |
* |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2554 |
* @param <T> the snipper subtype |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2555 |
*/ |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2556 |
private class ArgToSnippets<T extends Snippet> { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2557 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2558 |
// the supplier of snippet streams |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2559 |
final Supplier<Stream<T>> snippetSupplier; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2560 |
// these two are parallel, and lazily filled if a range is encountered |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2561 |
List<T> allSnippets; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2562 |
String[] allIds = null; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2563 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2564 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2565 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2566 |
* @param snippetSupplier the base list of possible snippets |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2567 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2568 |
ArgToSnippets(Supplier<Stream<T>> snippetSupplier) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2569 |
this.snippetSupplier = snippetSupplier; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2570 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2571 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2572 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2573 |
* Convert user arguments to a Stream of snippets referenced by those |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2574 |
* arguments. |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2575 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2576 |
* @param args the user's argument to the command, maybe be the empty |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2577 |
* list |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2578 |
* @return a Stream of referenced snippets or null if no matches to |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2579 |
* specific arg |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2580 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2581 |
Stream<T> argsToSnippets(List<String> args) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2582 |
Stream<T> result = null; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2583 |
for (String arg : args) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2584 |
// Find the best match |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2585 |
Stream<T> st = argToSnippets(arg); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2586 |
if (st == null) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2587 |
return null; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2588 |
} else { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2589 |
result = (result == null) |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2590 |
? st |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2591 |
: Stream.concat(result, st); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2592 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2593 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2594 |
return result; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2595 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2596 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2597 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2598 |
* Convert a user argument to a Stream of snippets referenced by the |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2599 |
* argument. |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2600 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2601 |
* @param snippetSupplier the base list of possible snippets |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2602 |
* @param arg the user's argument to the command |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2603 |
* @return a Stream of referenced snippets or null if no matches to |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2604 |
* specific arg |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2605 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2606 |
Stream<T> argToSnippets(String arg) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2607 |
if (arg.contains("-")) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2608 |
return range(arg); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2609 |
} |
38539 | 2610 |
// Find the best match |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
2611 |
Stream<T> st = layeredSnippetSearch(snippetSupplier, arg); |
38539 | 2612 |
if (st == null) { |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2613 |
badSnippetErrormsg(arg); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2614 |
return null; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2615 |
} else { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2616 |
return st; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2617 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2618 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2619 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2620 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2621 |
* Look for inappropriate snippets to give best error message |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2622 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2623 |
* @param arg the bad snippet arg |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2624 |
* @param errKey the not found error key |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2625 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2626 |
void badSnippetErrormsg(String arg) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2627 |
Stream<Snippet> est = layeredSnippetSearch(state::snippets, arg); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2628 |
if (est == null) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2629 |
if (ID.matcher(arg).matches()) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2630 |
errormsg("jshell.err.no.snippet.with.id", arg); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2631 |
} else { |
38539 | 2632 |
errormsg("jshell.err.no.such.snippets", arg); |
2633 |
} |
|
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2634 |
} else { |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2635 |
errormsg("jshell.err.the.snippet.cannot.be.used.with.this.command", |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2636 |
arg, est.findFirst().get().source()); |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2637 |
} |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2638 |
} |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2639 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2640 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2641 |
* Search through the snippets for the best match to the id/name. |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2642 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2643 |
* @param <R> the snippet type |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2644 |
* @param aSnippetSupplier the supplier of snippet streams |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2645 |
* @param arg the arg to match |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2646 |
* @return a Stream of referenced snippets or null if no matches to |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2647 |
* specific arg |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2648 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2649 |
<R extends Snippet> Stream<R> layeredSnippetSearch(Supplier<Stream<R>> aSnippetSupplier, String arg) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2650 |
return nonEmptyStream( |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2651 |
// the stream supplier |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2652 |
aSnippetSupplier, |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2653 |
// look for active user declarations matching the name |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2654 |
sn -> isActive(sn) && matchingDeclaration(sn, arg), |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2655 |
// else, look for any declarations matching the name |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2656 |
sn -> matchingDeclaration(sn, arg), |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2657 |
// else, look for an id of this name |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2658 |
sn -> sn.id().equals(arg) |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2659 |
); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2660 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2661 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2662 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2663 |
* Given an id1-id2 range specifier, return a stream of snippets within |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2664 |
* our context |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2665 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2666 |
* @param arg the range arg |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2667 |
* @return a Stream of referenced snippets or null if no matches to |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2668 |
* specific arg |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2669 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2670 |
Stream<T> range(String arg) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2671 |
int dash = arg.indexOf('-'); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2672 |
String iid = arg.substring(0, dash); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2673 |
String tid = arg.substring(dash + 1); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2674 |
int iidx = snippetIndex(iid); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2675 |
if (iidx < 0) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2676 |
return null; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2677 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2678 |
int tidx = snippetIndex(tid); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2679 |
if (tidx < 0) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2680 |
return null; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2681 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2682 |
if (tidx < iidx) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2683 |
errormsg("jshell.err.end.snippet.range.less.than.start", iid, tid); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2684 |
return null; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2685 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2686 |
return allSnippets.subList(iidx, tidx+1).stream(); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2687 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2688 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2689 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2690 |
* Lazily initialize the id mapping -- needed only for id ranges. |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2691 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2692 |
void initIdMapping() { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2693 |
if (allIds == null) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2694 |
allSnippets = snippetSupplier.get() |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2695 |
.sorted((a, b) -> order(a) - order(b)) |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2696 |
.collect(toList()); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2697 |
allIds = allSnippets.stream() |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2698 |
.map(sn -> sn.id()) |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2699 |
.toArray(n -> new String[n]); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2700 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2701 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2702 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2703 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2704 |
* Return all the snippet ids -- within the context, and in order. |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2705 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2706 |
* @return the snippet ids |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2707 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2708 |
String[] allIds() { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2709 |
initIdMapping(); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2710 |
return allIds; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2711 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2712 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2713 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2714 |
* Establish an order on snippet ids. All startup snippets are first, |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2715 |
* all error snippets are last -- within that is by snippet number. |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2716 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2717 |
* @param id the id string |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2718 |
* @return an ordering int |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2719 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2720 |
int order(String id) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2721 |
try { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2722 |
switch (id.charAt(0)) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2723 |
case 's': |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2724 |
return Integer.parseInt(id.substring(1)); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2725 |
case 'e': |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2726 |
return 0x40000000 + Integer.parseInt(id.substring(1)); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2727 |
default: |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2728 |
return 0x20000000 + Integer.parseInt(id); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2729 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2730 |
} catch (Exception ex) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2731 |
return 0x60000000; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2732 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2733 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2734 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2735 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2736 |
* Establish an order on snippets, based on its snippet id. All startup |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2737 |
* snippets are first, all error snippets are last -- within that is by |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2738 |
* snippet number. |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2739 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2740 |
* @param sn the id string |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2741 |
* @return an ordering int |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2742 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2743 |
int order(Snippet sn) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2744 |
return order(sn.id()); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2745 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2746 |
|
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2747 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2748 |
* Find the index into the parallel allSnippets and allIds structures. |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2749 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2750 |
* @param s the snippet id name |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2751 |
* @return the index, or, if not found, report the error and return a |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2752 |
* negative number |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2753 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2754 |
int snippetIndex(String s) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2755 |
int idx = Arrays.binarySearch(allIds(), 0, allIds().length, s, |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2756 |
(a, b) -> order(a) - order(b)); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2757 |
if (idx < 0) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2758 |
// the id is not in the snippet domain, find the right error to report |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2759 |
if (!ID.matcher(s).matches()) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2760 |
errormsg("jshell.err.range.requires.id", s); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2761 |
} else { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2762 |
badSnippetErrormsg(s); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2763 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2764 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2765 |
return idx; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2766 |
} |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2767 |
|
38539 | 2768 |
} |
2769 |
||
2770 |
private boolean cmdDrop(String rawargs) { |
|
2771 |
ArgTokenizer at = new ArgTokenizer("/drop", rawargs.trim()); |
|
2772 |
at.allowedOptions(); |
|
2773 |
List<String> args = new ArrayList<>(); |
|
2774 |
String s; |
|
2775 |
while ((s = at.next()) != null) { |
|
2776 |
args.add(s); |
|
2777 |
} |
|
2778 |
if (!checkOptionsAndRemainingInput(at)) { |
|
2779 |
return false; |
|
2780 |
} |
|
2781 |
if (args.isEmpty()) { |
|
36990 | 2782 |
errormsg("jshell.err.drop.arg"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2783 |
return false; |
33362 | 2784 |
} |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2785 |
Stream<Snippet> stream = new ArgToSnippets<>(this::dropableSnippets).argsToSnippets(args); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2786 |
if (stream == null) { |
38539 | 2787 |
// Snippet not found. Error already printed |
36990 | 2788 |
fluffmsg("jshell.msg.see.classes.etc"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2789 |
return false; |
33362 | 2790 |
} |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
2791 |
stream.forEach(sn -> state.drop(sn).forEach(this::handleEvent)); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2792 |
return true; |
33362 | 2793 |
} |
2794 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2795 |
private boolean cmdEdit(String arg) { |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
2796 |
Stream<Snippet> stream = argsOptionsToSnippets(state::snippets, |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2797 |
this::mainActive, arg, "/edit"); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2798 |
if (stream == null) { |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2799 |
return false; |
33362 | 2800 |
} |
2801 |
Set<String> srcSet = new LinkedHashSet<>(); |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2802 |
stream.forEachOrdered(sn -> { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2803 |
String src = sn.source(); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2804 |
switch (sn.subKind()) { |
33362 | 2805 |
case VAR_VALUE_SUBKIND: |
2806 |
break; |
|
2807 |
case ASSIGNMENT_SUBKIND: |
|
2808 |
case OTHER_EXPRESSION_SUBKIND: |
|
2809 |
case TEMP_VAR_EXPRESSION_SUBKIND: |
|
42259
e4d04fcd0826
8143006: jshell tool: /edit doesn't process each line as same as inputs for jshell
rfield
parents:
41937
diff
changeset
|
2810 |
case UNKNOWN_SUBKIND: |
33362 | 2811 |
if (!src.endsWith(";")) { |
2812 |
src = src + ";"; |
|
2813 |
} |
|
2814 |
srcSet.add(src); |
|
2815 |
break; |
|
43264
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2816 |
case STATEMENT_SUBKIND: |
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2817 |
if (src.endsWith("}")) { |
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2818 |
// Could end with block or, for example, new Foo() {...} |
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2819 |
// so, we need deeper analysis to know if it needs a semicolon |
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2820 |
src = analysis.analyzeCompletion(src).source(); |
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2821 |
} else if (!src.endsWith(";")) { |
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2822 |
src = src + ";"; |
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2823 |
} |
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2824 |
srcSet.add(src); |
7b06e19184de
8171130: jshell tool: /edit adds empty statement to brace terminated snippet
rfield
parents:
43263
diff
changeset
|
2825 |
break; |
33362 | 2826 |
default: |
2827 |
srcSet.add(src); |
|
2828 |
break; |
|
2829 |
} |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2830 |
}); |
33362 | 2831 |
StringBuilder sb = new StringBuilder(); |
2832 |
for (String s : srcSet) { |
|
2833 |
sb.append(s); |
|
2834 |
sb.append('\n'); |
|
2835 |
} |
|
2836 |
String src = sb.toString(); |
|
2837 |
Consumer<String> saveHandler = new SaveHandler(src, srcSet); |
|
2838 |
Consumer<String> errorHandler = s -> hard("Edit Error: %s", s); |
|
41635
cb3d04878117
8163840: jshell tool: provide way to display configuration settings
rfield
parents:
41514
diff
changeset
|
2839 |
if (editor == BUILT_IN_EDITOR) { |
41934
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2840 |
return builtInEdit(src, saveHandler, errorHandler); |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2841 |
} else { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2842 |
// Changes have occurred in temp edit directory, |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2843 |
// transfer the new sources to JShell (unless the editor is |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2844 |
// running directly in JShell's window -- don't make a mess) |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2845 |
String[] buffer = new String[1]; |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2846 |
Consumer<String> extSaveHandler = s -> { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2847 |
if (input.terminalEditorRunning()) { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2848 |
buffer[0] = s; |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2849 |
} else { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2850 |
saveHandler.accept(s); |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2851 |
} |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2852 |
}; |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2853 |
ExternalEditor.edit(editor.cmd, src, |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2854 |
errorHandler, extSaveHandler, |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2855 |
() -> input.suspend(), |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2856 |
() -> input.resume(), |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2857 |
editor.wait, |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2858 |
() -> hardrb("jshell.msg.press.return.to.leave.edit.mode")); |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2859 |
if (buffer[0] != null) { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2860 |
saveHandler.accept(buffer[0]); |
37007
6023a9a9d58a
8153716: JShell tool: should warn when failed to launch editor
rfield
parents:
36992
diff
changeset
|
2861 |
} |
33362 | 2862 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2863 |
return true; |
33362 | 2864 |
} |
2865 |
//where |
|
41934
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2866 |
// start the built-in editor |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2867 |
private boolean builtInEdit(String initialText, |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2868 |
Consumer<String> saveHandler, Consumer<String> errorHandler) { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2869 |
try { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2870 |
ServiceLoader<BuildInEditorProvider> sl |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2871 |
= ServiceLoader.load(BuildInEditorProvider.class); |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2872 |
// Find the highest ranking provider |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2873 |
BuildInEditorProvider provider = null; |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2874 |
for (BuildInEditorProvider p : sl) { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2875 |
if (provider == null || p.rank() > provider.rank()) { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2876 |
provider = p; |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2877 |
} |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2878 |
} |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2879 |
if (provider != null) { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2880 |
provider.edit(getResourceString("jshell.label.editpad"), |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2881 |
initialText, saveHandler, errorHandler); |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2882 |
return true; |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2883 |
} else { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2884 |
errormsg("jshell.err.no.builtin.editor"); |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2885 |
} |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2886 |
} catch (RuntimeException ex) { |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2887 |
errormsg("jshell.err.cant.launch.editor", ex); |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2888 |
} |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2889 |
fluffmsg("jshell.msg.try.set.editor"); |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2890 |
return false; |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2891 |
} |
a4da50688dc7
8167636: jshell tool: Edit Pad should be in its own module
rfield
parents:
41865
diff
changeset
|
2892 |
//where |
33362 | 2893 |
// receives editor requests to save |
2894 |
private class SaveHandler implements Consumer<String> { |
|
2895 |
||
2896 |
String src; |
|
2897 |
Set<String> currSrcs; |
|
2898 |
||
2899 |
SaveHandler(String src, Set<String> ss) { |
|
2900 |
this.src = src; |
|
2901 |
this.currSrcs = ss; |
|
2902 |
} |
|
2903 |
||
2904 |
@Override |
|
2905 |
public void accept(String s) { |
|
2906 |
if (!s.equals(src)) { // quick check first |
|
2907 |
src = s; |
|
2908 |
try { |
|
2909 |
Set<String> nextSrcs = new LinkedHashSet<>(); |
|
2910 |
boolean failed = false; |
|
2911 |
while (true) { |
|
2912 |
CompletionInfo an = analysis.analyzeCompletion(s); |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38613
diff
changeset
|
2913 |
if (!an.completeness().isComplete()) { |
33362 | 2914 |
break; |
2915 |
} |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38613
diff
changeset
|
2916 |
String tsrc = trimNewlines(an.source()); |
33362 | 2917 |
if (!failed && !currSrcs.contains(tsrc)) { |
47840 | 2918 |
failed = processSource(tsrc); |
33362 | 2919 |
} |
2920 |
nextSrcs.add(tsrc); |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38613
diff
changeset
|
2921 |
if (an.remaining().isEmpty()) { |
33362 | 2922 |
break; |
2923 |
} |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38613
diff
changeset
|
2924 |
s = an.remaining(); |
33362 | 2925 |
} |
2926 |
currSrcs = nextSrcs; |
|
2927 |
} catch (IllegalStateException ex) { |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2928 |
errormsg("jshell.msg.resetting"); |
33362 | 2929 |
resetState(); |
2930 |
currSrcs = new LinkedHashSet<>(); // re-process everything |
|
2931 |
} |
|
2932 |
} |
|
2933 |
} |
|
2934 |
||
2935 |
private String trimNewlines(String s) { |
|
2936 |
int b = 0; |
|
2937 |
while (b < s.length() && s.charAt(b) == '\n') { |
|
2938 |
++b; |
|
2939 |
} |
|
2940 |
int e = s.length() -1; |
|
2941 |
while (e >= 0 && s.charAt(e) == '\n') { |
|
2942 |
--e; |
|
2943 |
} |
|
2944 |
return s.substring(b, e + 1); |
|
2945 |
} |
|
2946 |
} |
|
2947 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2948 |
private boolean cmdList(String arg) { |
38539 | 2949 |
if (arg.length() >= 2 && "-history".startsWith(arg)) { |
48939
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
2950 |
return cmdHistory(""); |
33362 | 2951 |
} |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
2952 |
Stream<Snippet> stream = argsOptionsToSnippets(state::snippets, |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
2953 |
this::mainActive, arg, "/list"); |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2954 |
if (stream == null) { |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2955 |
return false; |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2956 |
} |
33362 | 2957 |
|
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2958 |
// prevent double newline on empty list |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2959 |
boolean[] hasOutput = new boolean[1]; |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2960 |
stream.forEachOrdered(sn -> { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2961 |
if (!hasOutput[0]) { |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2962 |
cmdout.println(); |
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2963 |
hasOutput[0] = true; |
33362 | 2964 |
} |
34570
8a8f52a733dd
8144095: jshell tool: normalize command parameter handling
rfield
parents:
34477
diff
changeset
|
2965 |
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
|
2966 |
}); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2967 |
return true; |
33362 | 2968 |
} |
2969 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
2970 |
private boolean cmdOpen(String filename) { |
37389 | 2971 |
return runFile(filename, "/open"); |
36990 | 2972 |
} |
2973 |
||
2974 |
private boolean runFile(String filename, String context) { |
|
2975 |
if (!filename.isEmpty()) { |
|
33362 | 2976 |
try { |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2977 |
Scanner scanner; |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2978 |
if (!interactiveModeBegun && filename.equals("-")) { |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2979 |
// - on command line: no interactive later, read from input |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2980 |
regenerateOnDeath = false; |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2981 |
scanner = new Scanner(cmdin); |
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
2982 |
} else { |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2983 |
Path path = toPathResolvingUserHome(filename); |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2984 |
String resource; |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2985 |
scanner = new Scanner( |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2986 |
(!Files.exists(path) && (resource = getResource(filename)) != null) |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2987 |
? new StringReader(resource) // Not found as file, but found as resource |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2988 |
: new FileReader(path.toString()) |
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2989 |
); |
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
2990 |
} |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
2991 |
run(new ScannerIOContext(scanner)); |
36990 | 2992 |
return true; |
33362 | 2993 |
} catch (FileNotFoundException e) { |
36990 | 2994 |
errormsg("jshell.err.file.not.found", context, filename, e.getMessage()); |
33362 | 2995 |
} catch (Exception e) { |
36990 | 2996 |
errormsg("jshell.err.file.exception", context, filename, e); |
33362 | 2997 |
} |
36990 | 2998 |
} else { |
2999 |
errormsg("jshell.err.file.filename", context); |
|
33362 | 3000 |
} |
36990 | 3001 |
return false; |
3002 |
} |
|
3003 |
||
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
3004 |
static String getResource(String name) { |
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3005 |
if (BUILTIN_FILE_PATTERN.matcher(name).matches()) { |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3006 |
try { |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3007 |
return readResource(name); |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3008 |
} catch (Throwable t) { |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3009 |
// Fall-through to null |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3010 |
} |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3011 |
} |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3012 |
return null; |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3013 |
} |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3014 |
|
47837
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3015 |
// Read a built-in file from resources or compute it |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3016 |
static String readResource(String name) throws Exception { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3017 |
// Class to compute imports by following requires for a module |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3018 |
class ComputeImports { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3019 |
final String base; |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3020 |
ModuleFinder finder = ModuleFinder.ofSystem(); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3021 |
|
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3022 |
ComputeImports(String base) { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3023 |
this.base = base; |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3024 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3025 |
|
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3026 |
Set<ModuleDescriptor> modules() { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3027 |
Set<ModuleDescriptor> closure = new HashSet<>(); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3028 |
moduleClosure(finder.find(base), closure); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3029 |
return closure; |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3030 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3031 |
|
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3032 |
void moduleClosure(Optional<ModuleReference> omr, Set<ModuleDescriptor> closure) { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3033 |
if (omr.isPresent()) { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3034 |
ModuleDescriptor mdesc = omr.get().descriptor(); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3035 |
if (closure.add(mdesc)) { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3036 |
for (ModuleDescriptor.Requires req : mdesc.requires()) { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3037 |
if (!req.modifiers().contains(ModuleDescriptor.Requires.Modifier.STATIC)) { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3038 |
moduleClosure(finder.find(req.name()), closure); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3039 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3040 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3041 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3042 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3043 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3044 |
|
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3045 |
Set<String> packages() { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3046 |
return modules().stream().flatMap(md -> md.exports().stream()) |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3047 |
.filter(e -> !e.isQualified()).map(Object::toString).collect(Collectors.toSet()); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3048 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3049 |
|
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3050 |
String imports() { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3051 |
Set<String> si = packages(); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3052 |
String[] ai = si.toArray(new String[si.size()]); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3053 |
Arrays.sort(ai); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3054 |
return Arrays.stream(ai) |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3055 |
.map(p -> String.format("import %s.*;\n", p)) |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3056 |
.collect(Collectors.joining()); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3057 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3058 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3059 |
|
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3060 |
if (name.equals("JAVASE")) { |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3061 |
// The built-in JAVASE is computed as the imports of all the packages in Java SE |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3062 |
return new ComputeImports("java.se").imports(); |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3063 |
} |
ef7aad81c3d6
8172154: jshell tool: make all IMPORTS.jsh generated at build time
rfield
parents:
47504
diff
changeset
|
3064 |
|
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3065 |
// Attempt to find the file as a resource |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3066 |
String spec = String.format(BUILTIN_FILE_PATH_FORMAT, name); |
43136
24d62ba7ad5e
8172414: jshell not working in exploded JDK build
jlahoda
parents:
43134
diff
changeset
|
3067 |
|
24d62ba7ad5e
8172414: jshell not working in exploded JDK build
jlahoda
parents:
43134
diff
changeset
|
3068 |
try (InputStream in = JShellTool.class.getResourceAsStream(spec); |
43263
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
3069 |
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { |
ca999fb7b46d
8172179: jshell tool: builtin startup settings should be by reference not content
rfield
parents:
43136
diff
changeset
|
3070 |
return reader.lines().collect(Collectors.joining("\n", "", "\n")); |
43136
24d62ba7ad5e
8172414: jshell not working in exploded JDK build
jlahoda
parents:
43134
diff
changeset
|
3071 |
} |
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3072 |
} |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3073 |
|
43038 | 3074 |
private boolean cmdReset(String rawargs) { |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3075 |
Options oldOptions = rawargs.trim().isEmpty()? null : options; |
43038 | 3076 |
if (!parseCommandLineLikeFlags(rawargs, new OptionParserBase())) { |
3077 |
return false; |
|
3078 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3079 |
live = false; |
36990 | 3080 |
fluffmsg("jshell.msg.resetting.state"); |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3081 |
return doReload(null, false, oldOptions); |
33362 | 3082 |
} |
3083 |
||
38539 | 3084 |
private boolean cmdReload(String rawargs) { |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3085 |
Options oldOptions = rawargs.trim().isEmpty()? null : options; |
43038 | 3086 |
OptionParserReload ap = new OptionParserReload(); |
3087 |
if (!parseCommandLineLikeFlags(rawargs, ap)) { |
|
38539 | 3088 |
return false; |
3089 |
} |
|
43564 | 3090 |
ReplayableHistory history; |
43038 | 3091 |
if (ap.restore()) { |
38539 | 3092 |
if (replayableHistoryPrevious == null) { |
3093 |
errormsg("jshell.err.reload.no.previous"); |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3094 |
return false; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3095 |
} |
38539 | 3096 |
history = replayableHistoryPrevious; |
3097 |
fluffmsg("jshell.err.reload.restarting.previous.state"); |
|
3098 |
} else { |
|
3099 |
history = replayableHistory; |
|
3100 |
fluffmsg("jshell.err.reload.restarting.state"); |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3101 |
} |
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3102 |
boolean success = doReload(history, !ap.quiet(), oldOptions); |
43564 | 3103 |
if (success && ap.restore()) { |
3104 |
// if we are restoring from previous, then if nothing was added |
|
3105 |
// before time of exit, there is nothing to save |
|
3106 |
replayableHistory.markSaved(); |
|
3107 |
} |
|
3108 |
return success; |
|
43038 | 3109 |
} |
3110 |
||
3111 |
private boolean cmdEnv(String rawargs) { |
|
3112 |
if (rawargs.trim().isEmpty()) { |
|
3113 |
// No arguments, display current settings (as option flags) |
|
3114 |
StringBuilder sb = new StringBuilder(); |
|
3115 |
for (String a : options.commonOptions()) { |
|
3116 |
sb.append( |
|
3117 |
a.startsWith("-") |
|
3118 |
? sb.length() > 0 |
|
3119 |
? "\n " |
|
3120 |
: " " |
|
3121 |
: " "); |
|
3122 |
sb.append(a); |
|
3123 |
} |
|
3124 |
if (sb.length() > 0) { |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
3125 |
hard(sb.toString()); |
43038 | 3126 |
} |
3127 |
return false; |
|
3128 |
} |
|
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3129 |
Options oldOptions = options; |
43038 | 3130 |
if (!parseCommandLineLikeFlags(rawargs, new OptionParserBase())) { |
3131 |
return false; |
|
3132 |
} |
|
3133 |
fluffmsg("jshell.msg.set.restore"); |
|
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3134 |
return doReload(replayableHistory, false, oldOptions); |
43038 | 3135 |
} |
3136 |
||
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3137 |
private boolean doReload(ReplayableHistory history, boolean echo, Options oldOptions) { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3138 |
if (oldOptions != null) { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3139 |
try { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3140 |
resetState(); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3141 |
} catch (IllegalStateException ex) { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3142 |
currentNameSpace = mainNamespace; // back out of start-up (messages) |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3143 |
errormsg("jshell.err.restart.failed", ex.getMessage()); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3144 |
// attempt recovery to previous option settings |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3145 |
options = oldOptions; |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3146 |
resetState(); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3147 |
} |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3148 |
} else { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3149 |
resetState(); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3150 |
} |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3151 |
if (history != null) { |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3152 |
run(new ReloadIOContext(history.iterable(), |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3153 |
echo ? cmdout : null)); |
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3154 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3155 |
return true; |
33362 | 3156 |
} |
3157 |
||
43038 | 3158 |
private boolean parseCommandLineLikeFlags(String rawargs, OptionParserBase ap) { |
3159 |
String[] args = Arrays.stream(rawargs.split("\\s+")) |
|
3160 |
.filter(s -> !s.isEmpty()) |
|
3161 |
.toArray(String[]::new); |
|
3162 |
Options opts = ap.parse(args); |
|
3163 |
if (opts == null) { |
|
3164 |
return false; |
|
3165 |
} |
|
3166 |
if (!ap.nonOptions().isEmpty()) { |
|
3167 |
errormsg("jshell.err.unexpected.at.end", ap.nonOptions(), rawargs); |
|
3168 |
return false; |
|
3169 |
} |
|
44683
610dc2b48954
8178023: jshell tool: crash with ugly message on attempt to add non-existant module path
rfield
parents:
44569
diff
changeset
|
3170 |
options = options.override(opts); |
43038 | 3171 |
return true; |
3172 |
} |
|
3173 |
||
38539 | 3174 |
private boolean cmdSave(String rawargs) { |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3175 |
// The filename to save to is the last argument, extract it |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3176 |
String[] args = rawargs.split("\\s"); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3177 |
String filename = args[args.length - 1]; |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3178 |
if (filename.isEmpty()) { |
38539 | 3179 |
errormsg("jshell.err.file.filename", "/save"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3180 |
return false; |
33362 | 3181 |
} |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3182 |
// All the non-filename arguments are the specifier of what to save |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3183 |
String srcSpec = Arrays.stream(args, 0, args.length - 1) |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3184 |
.collect(Collectors.joining("\n")); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3185 |
// From the what to save specifier, compute the snippets (as a stream) |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3186 |
ArgTokenizer at = new ArgTokenizer("/save", srcSpec); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3187 |
at.allowedOptions("-all", "-start", "-history"); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3188 |
Stream<Snippet> snippetStream = argsOptionsToSnippets(state::snippets, this::mainActive, at); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3189 |
if (snippetStream == null) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3190 |
// error occurred, already reported |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3191 |
return false; |
33362 | 3192 |
} |
3193 |
try (BufferedWriter writer = Files.newBufferedWriter(toPathResolvingUserHome(filename), |
|
3194 |
Charset.defaultCharset(), |
|
3195 |
CREATE, TRUNCATE_EXISTING, WRITE)) { |
|
38539 | 3196 |
if (at.hasOption("-history")) { |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3197 |
// they want history (commands and snippets), ignore the snippet stream |
48939
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
3198 |
for (String s : input.history(true)) { |
33362 | 3199 |
writer.write(s); |
3200 |
writer.write("\n"); |
|
3201 |
} |
|
3202 |
} else { |
|
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3203 |
// write the snippet stream to the file |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3204 |
writer.write(snippetStream |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
3205 |
.map(Snippet::source) |
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3206 |
.collect(Collectors.joining("\n"))); |
33362 | 3207 |
} |
3208 |
} catch (FileNotFoundException e) { |
|
36990 | 3209 |
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
|
3210 |
return false; |
33362 | 3211 |
} catch (Exception e) { |
36990 | 3212 |
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
|
3213 |
return false; |
33362 | 3214 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3215 |
return true; |
33362 | 3216 |
} |
3217 |
||
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3218 |
private boolean cmdVars(String arg) { |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
3219 |
Stream<VarSnippet> stream = argsOptionsToSnippets(this::allVarSnippets, |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3220 |
this::isActive, arg, "/vars"); |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3221 |
if (stream == null) { |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3222 |
return false; |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3223 |
} |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3224 |
stream.forEachOrdered(vk -> |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3225 |
{ |
33362 | 3226 |
String val = state.status(vk) == Status.VALID |
41937
1313399705e9
8161969: jshell tool: /var value is not truncated per feedback setting
rfield
parents:
41934
diff
changeset
|
3227 |
? feedback.truncateVarValue(state.varValue(vk)) |
40516
9e0e107c39dd
8154374: JShell: setContextClassLoader() for remote Snippet class loader
rfield
parents:
40498
diff
changeset
|
3228 |
: getResourceString("jshell.msg.vars.not.active"); |
33362 | 3229 |
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
|
3230 |
}); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3231 |
return true; |
33362 | 3232 |
} |
3233 |
||
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3234 |
private boolean cmdMethods(String arg) { |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
3235 |
Stream<MethodSnippet> stream = argsOptionsToSnippets(this::allMethodSnippets, |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3236 |
this::isActive, arg, "/methods"); |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3237 |
if (stream == null) { |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3238 |
return false; |
33362 | 3239 |
} |
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3240 |
stream.forEachOrdered(meth -> { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3241 |
String sig = meth.signature(); |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3242 |
int i = sig.lastIndexOf(")") + 1; |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3243 |
if (i <= 0) { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3244 |
hard(" %s", meth.name()); |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3245 |
} else { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3246 |
hard(" %s %s%s", sig.substring(i), meth.name(), sig.substring(0, i)); |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3247 |
} |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3248 |
printSnippetStatus(meth, true); |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3249 |
}); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3250 |
return true; |
33362 | 3251 |
} |
3252 |
||
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3253 |
private boolean cmdTypes(String arg) { |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
3254 |
Stream<TypeDeclSnippet> stream = argsOptionsToSnippets(this::allTypeSnippets, |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3255 |
this::isActive, arg, "/types"); |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3256 |
if (stream == null) { |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3257 |
return false; |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3258 |
} |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3259 |
stream.forEachOrdered(ck |
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3260 |
-> { |
33362 | 3261 |
String kind; |
3262 |
switch (ck.subKind()) { |
|
3263 |
case INTERFACE_SUBKIND: |
|
3264 |
kind = "interface"; |
|
3265 |
break; |
|
3266 |
case CLASS_SUBKIND: |
|
3267 |
kind = "class"; |
|
3268 |
break; |
|
3269 |
case ENUM_SUBKIND: |
|
3270 |
kind = "enum"; |
|
3271 |
break; |
|
3272 |
case ANNOTATION_TYPE_SUBKIND: |
|
3273 |
kind = "@interface"; |
|
3274 |
break; |
|
3275 |
default: |
|
3276 |
assert false : "Wrong kind" + ck.subKind(); |
|
3277 |
kind = "class"; |
|
3278 |
break; |
|
3279 |
} |
|
3280 |
hard(" %s %s", kind, ck.name()); |
|
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3281 |
printSnippetStatus(ck, true); |
38514
f7df9ab653b0
8153920: jshell tool: allow a parameter on the /vars /methods /classes commands
rfield
parents:
38513
diff
changeset
|
3282 |
}); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3283 |
return true; |
33362 | 3284 |
} |
3285 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3286 |
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
|
3287 |
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
|
3288 |
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
|
3289 |
}); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3290 |
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
|
3291 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
3292 |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3293 |
private boolean cmdUseHistoryEntry(int index) { |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
3294 |
List<Snippet> keys = state.snippets().collect(toList()); |
33362 | 3295 |
if (index < 0) |
3296 |
index += keys.size(); |
|
3297 |
else |
|
3298 |
index--; |
|
3299 |
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
|
3300 |
rerunSnippet(keys.get(index)); |
33362 | 3301 |
} else { |
36990 | 3302 |
errormsg("jshell.err.out.of.range"); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3303 |
return false; |
33362 | 3304 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3305 |
return true; |
33362 | 3306 |
} |
3307 |
||
38539 | 3308 |
boolean checkOptionsAndRemainingInput(ArgTokenizer at) { |
3309 |
String junk = at.remainder(); |
|
3310 |
if (!junk.isEmpty()) { |
|
3311 |
errormsg("jshell.err.unexpected.at.end", junk, at.whole()); |
|
3312 |
return false; |
|
3313 |
} else { |
|
3314 |
String bad = at.badOptions(); |
|
3315 |
if (!bad.isEmpty()) { |
|
3316 |
errormsg("jshell.err.unknown.option", bad, at.whole()); |
|
3317 |
return false; |
|
3318 |
} |
|
3319 |
} |
|
3320 |
return true; |
|
3321 |
} |
|
3322 |
||
45215
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3323 |
/** |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3324 |
* Handle snippet reevaluation commands: {@code /<id>}. These commands are a |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3325 |
* sequence of ids and id ranges (names are permitted, though not in the |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3326 |
* first position. Support for names is purposely not documented). |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3327 |
* |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3328 |
* @param rawargs the whole command including arguments |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3329 |
*/ |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3330 |
private void rerunHistoryEntriesById(String rawargs) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3331 |
ArgTokenizer at = new ArgTokenizer("/<id>", rawargs.trim().substring(1)); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3332 |
at.allowedOptions(); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3333 |
Stream<Snippet> stream = argsOptionsToSnippets(state::snippets, sn -> true, at); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3334 |
if (stream != null) { |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3335 |
// successfully parsed, rerun snippets |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3336 |
stream.forEach(sn -> rerunSnippet(sn)); |
c9477e22877f
8167554: jshell tool: re-execute a range and/or sequence of snippets
rfield
parents:
44683
diff
changeset
|
3337 |
} |
34477
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
3338 |
} |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
3339 |
|
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
3340 |
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
|
3341 |
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
|
3342 |
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
|
3343 |
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
|
3344 |
processSourceCatchingReset(source); |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
3345 |
} |
64001b0533a2
8142447: JShell tool: Command change: re-run n-th command should be re-run by id
rfield
parents:
34475
diff
changeset
|
3346 |
|
33362 | 3347 |
/** |
3348 |
* Filter diagnostics for only errors (no warnings, ...) |
|
3349 |
* @param diagnostics input list |
|
3350 |
* @return filtered list |
|
3351 |
*/ |
|
3352 |
List<Diag> errorsOnly(List<Diag> diagnostics) { |
|
3353 |
return diagnostics.stream() |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42415
diff
changeset
|
3354 |
.filter(Diag::isError) |
33362 | 3355 |
.collect(toList()); |
3356 |
} |
|
3357 |
||
47840 | 3358 |
/** |
3359 |
* Print out a snippet exception. |
|
3360 |
* |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3361 |
* @param exception the throwable to print |
47840 | 3362 |
* @return true on fatal exception |
3363 |
*/ |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3364 |
private boolean displayException(Throwable exception) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3365 |
Throwable rootCause = exception; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3366 |
while (rootCause instanceof EvalException) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3367 |
rootCause = rootCause.getCause(); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3368 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3369 |
if (rootCause != exception && rootCause instanceof UnresolvedReferenceException) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3370 |
// An unresolved reference caused a chained exception, just show the unresolved |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3371 |
return displayException(rootCause, null); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3372 |
} else { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3373 |
return displayException(exception, null); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3374 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3375 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3376 |
//where |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3377 |
private boolean displayException(Throwable exception, StackTraceElement[] caused) { |
47840 | 3378 |
if (exception instanceof EvalException) { |
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3379 |
// User exception |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3380 |
return displayEvalException((EvalException) exception, caused); |
47840 | 3381 |
} else if (exception instanceof UnresolvedReferenceException) { |
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3382 |
// Reference to an undefined snippet |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3383 |
return displayUnresolvedException((UnresolvedReferenceException) exception); |
47840 | 3384 |
} else { |
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3385 |
// Should never occur |
47840 | 3386 |
error("Unexpected execution exception: %s", exception); |
3387 |
return true; |
|
3388 |
} |
|
3389 |
} |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3390 |
//where |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3391 |
private boolean displayUnresolvedException(UnresolvedReferenceException ex) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3392 |
// Display the resolution issue |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3393 |
printSnippetStatus(ex.getSnippet(), false); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3394 |
return false; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3395 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3396 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3397 |
//where |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3398 |
private boolean displayEvalException(EvalException ex, StackTraceElement[] caused) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3399 |
// The message for the user exception is configured based on the |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3400 |
// existance of an exception message and if this is a recursive |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3401 |
// invocation for a chained exception. |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3402 |
String msg = ex.getMessage(); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3403 |
String key = "jshell.err.exception" + |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3404 |
(caused == null? ".thrown" : ".cause") + |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3405 |
(msg == null? "" : ".message"); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3406 |
errormsg(key, ex.getExceptionClassName(), msg); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3407 |
// The caused trace is sent to truncate duplicate elements in the cause trace |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3408 |
printStackTrace(ex.getStackTrace(), caused); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3409 |
JShellException cause = ex.getCause(); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3410 |
if (cause != null) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3411 |
// Display the cause (recursively) |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3412 |
displayException(cause, ex.getStackTrace()); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3413 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3414 |
return true; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3415 |
} |
47840 | 3416 |
|
3417 |
/** |
|
3418 |
* Display a list of diagnostics. |
|
3419 |
* |
|
3420 |
* @param source the source line with the error/warning |
|
3421 |
* @param diagnostics the diagnostics to display |
|
3422 |
*/ |
|
3423 |
private void displayDiagnostics(String source, List<Diag> diagnostics) { |
|
3424 |
for (Diag d : diagnostics) { |
|
3425 |
errormsg(d.isError() ? "jshell.msg.error" : "jshell.msg.warning"); |
|
3426 |
List<String> disp = new ArrayList<>(); |
|
3427 |
displayableDiagnostic(source, d, disp); |
|
3428 |
disp.stream() |
|
3429 |
.forEach(l -> error("%s", l)); |
|
3430 |
} |
|
3431 |
} |
|
3432 |
||
3433 |
/** |
|
3434 |
* Convert a diagnostic into a list of pretty displayable strings with |
|
3435 |
* source context. |
|
3436 |
* |
|
3437 |
* @param source the source line for the error/warning |
|
3438 |
* @param diag the diagnostic to convert |
|
3439 |
* @param toDisplay a list that the displayable strings are added to |
|
3440 |
*/ |
|
3441 |
private void displayableDiagnostic(String source, Diag diag, List<String> toDisplay) { |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3442 |
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
|
3443 |
if (!line.trim().startsWith("location:")) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3444 |
toDisplay.add(line); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3445 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3446 |
} |
33362 | 3447 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3448 |
int pstart = (int) diag.getStartPosition(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3449 |
int pend = (int) diag.getEndPosition(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3450 |
Matcher m = LINEBREAK.matcher(source); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3451 |
int pstartl = 0; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3452 |
int pendl = -2; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3453 |
while (m.find(pstartl)) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3454 |
pendl = m.start(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3455 |
if (pendl >= pstart) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3456 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3457 |
} else { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3458 |
pstartl = m.end(); |
33362 | 3459 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3460 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3461 |
if (pendl < pstart) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3462 |
pendl = source.length(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3463 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3464 |
toDisplay.add(source.substring(pstartl, pendl)); |
33362 | 3465 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3466 |
StringBuilder sb = new StringBuilder(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3467 |
int start = pstart - pstartl; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3468 |
for (int i = 0; i < start; ++i) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3469 |
sb.append(' '); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3470 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3471 |
sb.append('^'); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3472 |
boolean multiline = pend > pendl; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3473 |
int end = (multiline ? pendl : pend) - pstartl - 1; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3474 |
if (end > start) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3475 |
for (int i = start + 1; i < end; ++i) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3476 |
sb.append('-'); |
33362 | 3477 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3478 |
if (multiline) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3479 |
sb.append("-..."); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3480 |
} else { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3481 |
sb.append('^'); |
33362 | 3482 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3483 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3484 |
toDisplay.add(sb.toString()); |
33362 | 3485 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3486 |
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
|
3487 |
debug("Code: %s", diag.getCode()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3488 |
debug("Pos: %d (%d - %d)", diag.getPosition(), |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3489 |
diag.getStartPosition(), diag.getEndPosition()); |
33362 | 3490 |
} |
3491 |
||
47840 | 3492 |
/** |
3493 |
* Process a source snippet. |
|
3494 |
* |
|
3495 |
* @param source the input source |
|
3496 |
* @return true if the snippet succeeded |
|
3497 |
*/ |
|
3498 |
boolean processSource(String source) { |
|
33362 | 3499 |
debug("Compiling: %s", source); |
3500 |
boolean failed = false; |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3501 |
boolean isActive = false; |
33362 | 3502 |
List<SnippetEvent> events = state.eval(source); |
3503 |
for (SnippetEvent e : events) { |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3504 |
// Report the event, recording failure |
33362 | 3505 |
failed |= handleEvent(e); |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3506 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3507 |
// 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
|
3508 |
// also ignore var value queries |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3509 |
isActive |= e.causeSnippet() == null && |
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38613
diff
changeset
|
3510 |
e.status().isActive() && |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3511 |
e.snippet().subKind() != VAR_VALUE_SUBKIND; |
33362 | 3512 |
} |
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3513 |
// 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
|
3514 |
// add it to the replayable history |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3515 |
if (isActive && live) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3516 |
addToReplayHistory(source); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3517 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3518 |
|
47840 | 3519 |
return !failed; |
33362 | 3520 |
} |
3521 |
||
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3522 |
// Handle incoming snippet events -- return true on failure |
33362 | 3523 |
private boolean handleEvent(SnippetEvent ste) { |
3524 |
Snippet sn = ste.snippet(); |
|
3525 |
if (sn == null) { |
|
3526 |
debug("Event with null key: %s", ste); |
|
3527 |
return false; |
|
3528 |
} |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
3529 |
List<Diag> diagnostics = state.diagnostics(sn).collect(toList()); |
33362 | 3530 |
String source = sn.source(); |
3531 |
if (ste.causeSnippet() == null) { |
|
3532 |
// main event |
|
47840 | 3533 |
displayDiagnostics(source, diagnostics); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3534 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3535 |
if (ste.status() != Status.REJECTED) { |
33362 | 3536 |
if (ste.exception() != null) { |
47840 | 3537 |
if (displayException(ste.exception())) { |
33362 | 3538 |
return true; |
3539 |
} |
|
3540 |
} else { |
|
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3541 |
new DisplayEvent(ste, FormatWhen.PRIMARY, ste.value(), diagnostics) |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3542 |
.displayDeclarationAndValue(); |
33362 | 3543 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3544 |
} else { |
33362 | 3545 |
if (diagnostics.isEmpty()) { |
36990 | 3546 |
errormsg("jshell.err.failed"); |
33362 | 3547 |
} |
3548 |
return true; |
|
3549 |
} |
|
3550 |
} else { |
|
3551 |
// Update |
|
36494 | 3552 |
if (sn instanceof DeclarationSnippet) { |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3553 |
List<Diag> other = errorsOnly(diagnostics); |
36494 | 3554 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3555 |
// display update information |
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3556 |
new DisplayEvent(ste, FormatWhen.UPDATE, ste.value(), other) |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3557 |
.displayDeclarationAndValue(); |
33362 | 3558 |
} |
3559 |
} |
|
3560 |
return false; |
|
3561 |
} |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3562 |
|
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3563 |
// Print a stack trace, elide frames displayed for the caused exception |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3564 |
void printStackTrace(StackTraceElement[] stes, StackTraceElement[] caused) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3565 |
int overlap = 0; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3566 |
if (caused != null) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3567 |
int maxOverlap = Math.min(stes.length, caused.length); |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3568 |
while (overlap < maxOverlap |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3569 |
&& stes[stes.length - (overlap + 1)].equals(caused[caused.length - (overlap + 1)])) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3570 |
++overlap; |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3571 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3572 |
} |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3573 |
for (int i = 0; i < stes.length - overlap; ++i) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3574 |
StackTraceElement ste = stes[i]; |
33362 | 3575 |
StringBuilder sb = new StringBuilder(); |
3576 |
String cn = ste.getClassName(); |
|
3577 |
if (!cn.isEmpty()) { |
|
3578 |
int dot = cn.lastIndexOf('.'); |
|
3579 |
if (dot > 0) { |
|
3580 |
sb.append(cn.substring(dot + 1)); |
|
3581 |
} else { |
|
3582 |
sb.append(cn); |
|
3583 |
} |
|
3584 |
sb.append("."); |
|
3585 |
} |
|
3586 |
if (!ste.getMethodName().isEmpty()) { |
|
3587 |
sb.append(ste.getMethodName()); |
|
3588 |
sb.append(" "); |
|
3589 |
} |
|
3590 |
String fileName = ste.getFileName(); |
|
3591 |
int lineNumber = ste.getLineNumber(); |
|
3592 |
String loc = ste.isNativeMethod() |
|
36990 | 3593 |
? getResourceString("jshell.msg.native.method") |
33362 | 3594 |
: fileName == null |
36990 | 3595 |
? getResourceString("jshell.msg.unknown.source") |
33362 | 3596 |
: lineNumber >= 0 |
3597 |
? fileName + ":" + lineNumber |
|
3598 |
: fileName; |
|
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
3599 |
error(" at %s(%s)", sb, loc); |
33362 | 3600 |
|
3601 |
} |
|
49515
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3602 |
if (overlap != 0) { |
083318155ad1
8198801: JShell: user exception chained cause not retained
rfield
parents:
48939
diff
changeset
|
3603 |
error(" ..."); |
33362 | 3604 |
} |
3605 |
} |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3606 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3607 |
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
|
3608 |
FormatAction act; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3609 |
switch (status) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3610 |
case VALID: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3611 |
case RECOVERABLE_DEFINED: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3612 |
case RECOVERABLE_NOT_DEFINED: |
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38613
diff
changeset
|
3613 |
if (previousStatus.isActive()) { |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3614 |
act = isSignatureChange |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3615 |
? FormatAction.REPLACED |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3616 |
: FormatAction.MODIFIED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3617 |
} else { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3618 |
act = FormatAction.ADDED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3619 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3620 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3621 |
case OVERWRITTEN: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3622 |
act = FormatAction.OVERWROTE; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3623 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3624 |
case DROPPED: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3625 |
act = FormatAction.DROPPED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3626 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3627 |
case REJECTED: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3628 |
case NONEXISTENT: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3629 |
default: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3630 |
// Should not occur |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3631 |
error("Unexpected status: " + previousStatus.toString() + "=>" + status.toString()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3632 |
act = FormatAction.DROPPED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3633 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3634 |
return act; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3635 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3636 |
|
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3637 |
void printSnippetStatus(DeclarationSnippet sn, boolean resolve) { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3638 |
List<Diag> otherErrors = errorsOnly(state.diagnostics(sn).collect(toList())); |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3639 |
new DisplayEvent(sn, state.status(sn), resolve, otherErrors) |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3640 |
.displayDeclarationAndValue(); |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3641 |
} |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3642 |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3643 |
class DisplayEvent { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3644 |
private final Snippet sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3645 |
private final FormatAction action; |
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3646 |
private final FormatWhen update; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3647 |
private final String value; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3648 |
private final List<String> errorLines; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3649 |
private final FormatResolve resolution; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3650 |
private final String unresolved; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3651 |
private final FormatUnresolved unrcnt; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3652 |
private final FormatErrors errcnt; |
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3653 |
private final boolean resolve; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3654 |
|
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3655 |
DisplayEvent(SnippetEvent ste, FormatWhen update, String value, List<Diag> errors) { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3656 |
this(ste.snippet(), ste.status(), false, |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3657 |
toAction(ste.status(), ste.previousStatus(), ste.isSignatureChange()), |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3658 |
update, value, errors); |
33362 | 3659 |
} |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3660 |
|
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3661 |
DisplayEvent(Snippet sn, Status status, boolean resolve, List<Diag> errors) { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3662 |
this(sn, status, resolve, FormatAction.USED, FormatWhen.UPDATE, null, errors); |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3663 |
} |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3664 |
|
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3665 |
private DisplayEvent(Snippet sn, Status status, boolean resolve, |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3666 |
FormatAction action, FormatWhen update, String value, List<Diag> errors) { |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3667 |
this.sn = sn; |
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3668 |
this.resolve =resolve; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3669 |
this.action = action; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3670 |
this.update = update; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3671 |
this.value = value; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3672 |
this.errorLines = new ArrayList<>(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3673 |
for (Diag d : errors) { |
47840 | 3674 |
displayableDiagnostic(sn.source(), d, errorLines); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3675 |
} |
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3676 |
if (resolve) { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3677 |
// resolve needs error lines indented |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3678 |
for (int i = 0; i < errorLines.size(); ++i) { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3679 |
errorLines.set(i, " " + errorLines.get(i)); |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3680 |
} |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3681 |
} |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
3682 |
long unresolvedCount; |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3683 |
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
|
3684 |
resolution = (status == Status.RECOVERABLE_NOT_DEFINED) |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3685 |
? FormatResolve.NOTDEFINED |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3686 |
: FormatResolve.DEFINED; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3687 |
unresolved = unresolved((DeclarationSnippet) sn); |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
3688 |
unresolvedCount = state.unresolvedDependencies((DeclarationSnippet) sn).count(); |
33362 | 3689 |
} else { |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3690 |
resolution = FormatResolve.OK; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3691 |
unresolved = ""; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3692 |
unresolvedCount = 0; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3693 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3694 |
unrcnt = unresolvedCount == 0 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3695 |
? FormatUnresolved.UNRESOLVED0 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3696 |
: unresolvedCount == 1 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3697 |
? FormatUnresolved.UNRESOLVED1 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3698 |
: FormatUnresolved.UNRESOLVED2; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3699 |
errcnt = errors.isEmpty() |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3700 |
? FormatErrors.ERROR0 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3701 |
: errors.size() == 1 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3702 |
? FormatErrors.ERROR1 |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3703 |
: FormatErrors.ERROR2; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3704 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3705 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3706 |
private String unresolved(DeclarationSnippet key) { |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
38912
diff
changeset
|
3707 |
List<String> unr = state.unresolvedDependencies(key).collect(toList()); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3708 |
StringBuilder sb = new StringBuilder(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3709 |
int fromLast = unr.size(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3710 |
if (fromLast > 0) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3711 |
sb.append(" "); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3712 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3713 |
for (String u : unr) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3714 |
--fromLast; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3715 |
sb.append(u); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3716 |
switch (fromLast) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3717 |
// No suffix |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3718 |
case 0: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3719 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3720 |
case 1: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3721 |
sb.append(", and "); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3722 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3723 |
default: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3724 |
sb.append(", "); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3725 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3726 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3727 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3728 |
return sb.toString(); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3729 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3730 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3731 |
private void custom(FormatCase fcase, String name) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3732 |
custom(fcase, name, null); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3733 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3734 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3735 |
private void custom(FormatCase fcase, String name, String type) { |
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3736 |
if (resolve) { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3737 |
String resolutionErrors = feedback.format("resolve", fcase, action, update, |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3738 |
resolution, unrcnt, errcnt, |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3739 |
name, type, value, unresolved, errorLines); |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3740 |
if (!resolutionErrors.trim().isEmpty()) { |
47504
58ce36f43f1a
8179856: jshell tool: not suitable for pipeline use
rfield
parents:
47216
diff
changeset
|
3741 |
error(" %s", resolutionErrors); |
43759
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3742 |
} |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3743 |
} else if (interactive()) { |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3744 |
String display = feedback.format(fcase, action, update, |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3745 |
resolution, unrcnt, errcnt, |
61535ac55add
8173916: jshell tool: /methods signature confusing/non-standard format
rfield
parents:
43757
diff
changeset
|
3746 |
name, type, value, unresolved, errorLines); |
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3747 |
cmdout.print(display); |
33362 | 3748 |
} |
3749 |
} |
|
36718
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3750 |
|
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3751 |
@SuppressWarnings("fallthrough") |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3752 |
private void displayDeclarationAndValue() { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3753 |
switch (sn.subKind()) { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3754 |
case CLASS_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3755 |
custom(FormatCase.CLASS, ((TypeDeclSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3756 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3757 |
case INTERFACE_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3758 |
custom(FormatCase.INTERFACE, ((TypeDeclSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3759 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3760 |
case ENUM_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3761 |
custom(FormatCase.ENUM, ((TypeDeclSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3762 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3763 |
case ANNOTATION_TYPE_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3764 |
custom(FormatCase.ANNOTATION, ((TypeDeclSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3765 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3766 |
case METHOD_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3767 |
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
|
3768 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3769 |
case VAR_DECLARATION_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3770 |
VarSnippet vk = (VarSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3771 |
custom(FormatCase.VARDECL, vk.name(), vk.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3772 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3773 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3774 |
case VAR_DECLARATION_WITH_INITIALIZER_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3775 |
VarSnippet vk = (VarSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3776 |
custom(FormatCase.VARINIT, vk.name(), vk.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3777 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3778 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3779 |
case TEMP_VAR_EXPRESSION_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3780 |
VarSnippet vk = (VarSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3781 |
custom(FormatCase.EXPRESSION, vk.name(), vk.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3782 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3783 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3784 |
case OTHER_EXPRESSION_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3785 |
error("Unexpected expression form -- value is: %s", (value)); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3786 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3787 |
case VAR_VALUE_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3788 |
ExpressionSnippet ek = (ExpressionSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3789 |
custom(FormatCase.VARVALUE, ek.name(), ek.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3790 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3791 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3792 |
case ASSIGNMENT_SUBKIND: { |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3793 |
ExpressionSnippet ek = (ExpressionSnippet) sn; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3794 |
custom(FormatCase.ASSIGNMENT, ek.name(), ek.typeName()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3795 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3796 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3797 |
case SINGLE_TYPE_IMPORT_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3798 |
case TYPE_IMPORT_ON_DEMAND_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3799 |
case SINGLE_STATIC_IMPORT_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3800 |
case STATIC_IMPORT_ON_DEMAND_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3801 |
custom(FormatCase.IMPORT, ((ImportSnippet) sn).name()); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3802 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3803 |
case STATEMENT_SUBKIND: |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3804 |
custom(FormatCase.STATEMENT, null); |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3805 |
break; |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3806 |
} |
bf40906bf49d
8151755: jshell tool: properly cover resolution issues in output configuration
rfield
parents:
36715
diff
changeset
|
3807 |
} |
33362 | 3808 |
} |
3809 |
||
3810 |
/** The current version number as a string. |
|
3811 |
*/ |
|
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
3812 |
String version() { |
33362 | 3813 |
return version("release"); // mm.nn.oo[-milestone] |
3814 |
} |
|
3815 |
||
3816 |
/** The current full version number as a string. |
|
3817 |
*/ |
|
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
3818 |
String fullVersion() { |
33362 | 3819 |
return version("full"); // mm.mm.oo[-milestone]-build |
3820 |
} |
|
3821 |
||
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
3822 |
private String version(String key) { |
33362 | 3823 |
if (versionRB == null) { |
3824 |
try { |
|
36992
ddebebe611a1
8153417: jshell tool: use test passed locale to retrieve ResourceBundle
rfield
parents:
36990
diff
changeset
|
3825 |
versionRB = ResourceBundle.getBundle(VERSION_RB_NAME, locale); |
33362 | 3826 |
} catch (MissingResourceException e) { |
3827 |
return "(version info not available)"; |
|
3828 |
} |
|
3829 |
} |
|
3830 |
try { |
|
3831 |
return versionRB.getString(key); |
|
3832 |
} |
|
3833 |
catch (MissingResourceException e) { |
|
3834 |
return "(version info not available)"; |
|
3835 |
} |
|
3836 |
} |
|
3837 |
||
3838 |
class NameSpace { |
|
3839 |
final String spaceName; |
|
3840 |
final String prefix; |
|
3841 |
private int nextNum; |
|
3842 |
||
3843 |
NameSpace(String spaceName, String prefix) { |
|
3844 |
this.spaceName = spaceName; |
|
3845 |
this.prefix = prefix; |
|
3846 |
this.nextNum = 1; |
|
3847 |
} |
|
3848 |
||
3849 |
String tid(Snippet sn) { |
|
3850 |
String tid = prefix + nextNum++; |
|
3851 |
mapSnippet.put(sn, new SnippetInfo(sn, this, tid)); |
|
3852 |
return tid; |
|
3853 |
} |
|
3854 |
||
3855 |
String tidNext() { |
|
3856 |
return prefix + nextNum; |
|
3857 |
} |
|
3858 |
} |
|
3859 |
||
3860 |
static class SnippetInfo { |
|
3861 |
final Snippet snippet; |
|
3862 |
final NameSpace space; |
|
3863 |
final String tid; |
|
3864 |
||
3865 |
SnippetInfo(Snippet snippet, NameSpace space, String tid) { |
|
3866 |
this.snippet = snippet; |
|
3867 |
this.space = space; |
|
3868 |
this.tid = tid; |
|
3869 |
} |
|
3870 |
} |
|
40498
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3871 |
|
40765
6f9556cf4404
8164825: jshell tool: Completion for subcommand
shinyafox
parents:
40598
diff
changeset
|
3872 |
static class ArgSuggestion implements Suggestion { |
40498
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3873 |
|
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3874 |
private final String continuation; |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3875 |
|
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3876 |
/** |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3877 |
* Create a {@code Suggestion} instance. |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3878 |
* |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3879 |
* @param continuation a candidate continuation of the user's input |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3880 |
*/ |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3881 |
public ArgSuggestion(String continuation) { |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3882 |
this.continuation = continuation; |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3883 |
} |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3884 |
|
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3885 |
/** |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3886 |
* The candidate continuation of the given user's input. |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3887 |
* |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3888 |
* @return the continuation string |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3889 |
*/ |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3890 |
@Override |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3891 |
public String continuation() { |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3892 |
return continuation; |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3893 |
} |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3894 |
|
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3895 |
/** |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3896 |
* Indicates whether input continuation matches the target type and is thus |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3897 |
* more likely to be the desired continuation. A matching continuation is |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3898 |
* preferred. |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3899 |
* |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3900 |
* @return {@code false}, non-types analysis |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3901 |
*/ |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3902 |
@Override |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3903 |
public boolean matchesType() { |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3904 |
return false; |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3905 |
} |
f54048be4a57
8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents:
40304
diff
changeset
|
3906 |
} |
33362 | 3907 |
} |
3908 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3909 |
abstract class NonInteractiveIOContext extends IOContext { |
33362 | 3910 |
|
3911 |
@Override |
|
3912 |
public boolean interactiveOutput() { |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3913 |
return false; |
33362 | 3914 |
} |
3915 |
||
3916 |
@Override |
|
48939
ba545e52b932
8166232: jshell tool: cannot access previous history
rfield
parents:
48543
diff
changeset
|
3917 |
public Iterable<String> history(boolean currentSession) { |
33362 | 3918 |
return Collections.emptyList(); |
3919 |
} |
|
3920 |
||
3921 |
@Override |
|
3922 |
public boolean terminalEditorRunning() { |
|
3923 |
return false; |
|
3924 |
} |
|
3925 |
||
3926 |
@Override |
|
3927 |
public void suspend() { |
|
3928 |
} |
|
3929 |
||
3930 |
@Override |
|
3931 |
public void resume() { |
|
3932 |
} |
|
3933 |
||
3934 |
@Override |
|
3935 |
public void beforeUserCode() { |
|
3936 |
} |
|
3937 |
||
3938 |
@Override |
|
3939 |
public void afterUserCode() { |
|
3940 |
} |
|
3941 |
||
3942 |
@Override |
|
3943 |
public void replaceLastHistoryEntry(String source) { |
|
3944 |
} |
|
3945 |
} |
|
3946 |
||
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3947 |
class ScannerIOContext extends NonInteractiveIOContext { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3948 |
private final Scanner scannerIn; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3949 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3950 |
ScannerIOContext(Scanner scannerIn) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3951 |
this.scannerIn = scannerIn; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3952 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3953 |
|
42972
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3954 |
ScannerIOContext(Reader rdr) throws FileNotFoundException { |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3955 |
this(new Scanner(rdr)); |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3956 |
} |
47ca49eee534
8172102: jshell tool: remove print method forwarding to System.out from default startup
rfield
parents:
42969
diff
changeset
|
3957 |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3958 |
@Override |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3959 |
public String readLine(String prompt, String prefix) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3960 |
if (scannerIn.hasNextLine()) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3961 |
return scannerIn.nextLine(); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3962 |
} else { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3963 |
return null; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3964 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3965 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3966 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3967 |
@Override |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3968 |
public void close() { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3969 |
scannerIn.close(); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3970 |
} |
40767 | 3971 |
|
47840 | 3972 |
@Override |
40767 | 3973 |
public int readUserInput() { |
3974 |
return -1; |
|
3975 |
} |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3976 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3977 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3978 |
class ReloadIOContext extends NonInteractiveIOContext { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3979 |
private final Iterator<String> it; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3980 |
private final PrintStream echoStream; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3981 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3982 |
ReloadIOContext(Iterable<String> history, PrintStream echoStream) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3983 |
this.it = history.iterator(); |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3984 |
this.echoStream = echoStream; |
33362 | 3985 |
} |
3986 |
||
3987 |
@Override |
|
34999
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3988 |
public String readLine(String prompt, String prefix) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3989 |
String s = it.hasNext() |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3990 |
? it.next() |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3991 |
: null; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3992 |
if (echoStream != null && s != null) { |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3993 |
String p = "-: "; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3994 |
String p2 = "\n "; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3995 |
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
|
3996 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3997 |
return s; |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3998 |
} |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
3999 |
|
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
4000 |
@Override |
aacf94dab449
8081845: JShell: Need way to refresh relative to external state
rfield
parents:
34570
diff
changeset
|
4001 |
public void close() { |
33362 | 4002 |
} |
40767 | 4003 |
|
47840 | 4004 |
@Override |
40767 | 4005 |
public int readUserInput() { |
4006 |
return -1; |
|
4007 |
} |
|
33362 | 4008 |
} |