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