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