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