8133549: Generalize jshell's EditingHistory
Summary: Using a generic EditingHistory in jjs
Reviewed-by: sundar
--- a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Console.java Wed May 18 18:44:44 2016 +0200
+++ b/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Console.java Wed May 18 21:00:22 2016 +0200
@@ -30,12 +30,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
+import java.io.Writer;
+import java.nio.file.Files;
import java.util.function.Function;
+import java.util.stream.Collectors;
import jdk.internal.jline.NoInterruptUnixTerminal;
import jdk.internal.jline.Terminal;
import jdk.internal.jline.TerminalFactory;
@@ -43,23 +41,28 @@
import jdk.internal.jline.WindowsTerminal;
import jdk.internal.jline.console.ConsoleReader;
import jdk.internal.jline.console.KeyMap;
-import jdk.internal.jline.console.completer.Completer;
-import jdk.internal.jline.console.history.FileHistory;
+import jdk.internal.jline.extra.EditingHistory;
class Console implements AutoCloseable {
private static final String DOCUMENTATION_SHORTCUT = "\033\133\132"; //Shift-TAB
private final ConsoleReader in;
- private final FileHistory history;
+ private final File historyFile;
Console(final InputStream cmdin, final PrintStream cmdout, final File historyFile,
- final Completer completer, final Function<String, String> docHelper) throws IOException {
+ final NashornCompleter completer, final Function<String, String> docHelper) throws IOException {
+ this.historyFile = historyFile;
+
TerminalFactory.registerFlavor(Flavor.WINDOWS, isCygwin()? JJSUnixTerminal::new : JJSWindowsTerminal::new);
TerminalFactory.registerFlavor(Flavor.UNIX, JJSUnixTerminal::new);
in = new ConsoleReader(cmdin, cmdout);
in.setExpandEvents(false);
in.setHandleUserInterrupt(true);
in.setBellEnabled(true);
- in.setHistory(history = new FileHistory(historyFile));
+ in.setHistory(new EditingHistory(in, Files.readAllLines(historyFile.toPath())) {
+ @Override protected boolean isComplete(CharSequence input) {
+ return completer.isComplete(input.toString());
+ }
+ });
in.addCompleter(completer);
Runtime.getRuntime().addShutdownHook(new Thread((Runnable)this::saveHistory));
bind(DOCUMENTATION_SHORTCUT, (ActionListener)evt -> showDocumentation(docHelper));
@@ -75,13 +78,17 @@
}
private void saveHistory() {
- try {
- getHistory().flush();
+ try (Writer out = Files.newBufferedWriter(historyFile.toPath())) {
+ String lineSeparator = System.getProperty("line.separator");
+
+ out.write(getHistory().save()
+ .stream()
+ .collect(Collectors.joining(lineSeparator)));
} catch (final IOException exp) {}
}
- FileHistory getHistory() {
- return (FileHistory) in.getHistory();
+ EditingHistory getHistory() {
+ return (EditingHistory) in.getHistory();
}
boolean terminalEditorRunning() {
--- a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/HistoryObject.java Wed May 18 18:44:44 2016 +0200
+++ b/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/HistoryObject.java Wed May 18 21:00:22 2016 +0200
@@ -36,7 +36,6 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
-import jdk.internal.jline.console.history.FileHistory;
import jdk.internal.jline.console.history.History;
import jdk.nashorn.api.scripting.AbstractJSObject;
import jdk.nashorn.api.scripting.JSObject;
@@ -61,11 +60,11 @@
props = Collections.unmodifiableSet(s);
}
- private final FileHistory hist;
+ private final History hist;
private final PrintWriter err;
private final Consumer<String> evaluator;
- HistoryObject(final FileHistory hist, final PrintWriter err,
+ HistoryObject(final History hist, final PrintWriter err,
final Consumer<String> evaluator) {
this.hist = hist;
this.err = err;
--- a/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java Wed May 18 18:44:44 2016 +0200
+++ b/nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/NashornCompleter.java Wed May 18 21:00:22 2016 +0200
@@ -185,6 +185,22 @@
}
}
+ public boolean isComplete(String input) {
+ try {
+ parser.parse("<shell>", input, null);
+ } catch (final Exception pexp) {
+ // Do we have a parse error at the end of current line?
+ // If so, read more lines from the console.
+ int line = input.split("\n").length;
+ int lastLineLen = input.length() - (input.lastIndexOf("\n") + 1);
+
+ if (isParseErrorAt(pexp, line, lastLineLen)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
// Pattern to match a unfinished member selection expression. object part and "."
// but property name missing pattern.
private static final Pattern SELECT_PROP_MISSING = Pattern.compile(".*\\.\\s*");