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