author | tonyp |
Fri, 19 Jul 2019 12:39:31 -0400 | |
changeset 55751 | 014decdb5086 |
parent 52938 | 5ff7480c9e28 |
permissions | -rw-r--r-- |
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
1 |
/* |
52938 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
4 |
* |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
10 |
* |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
15 |
* accompanied this code). |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
16 |
* |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
20 |
* |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
23 |
* questions. |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
24 |
*/ |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
25 |
|
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
26 |
package jdk.nashorn.tools.jjs; |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
27 |
|
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
32240
diff
changeset
|
28 |
import java.io.File; |
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
29 |
import java.io.IOException; |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
30 |
import java.io.InputStream; |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
31 |
import java.io.PrintStream; |
38488 | 32 |
import java.io.Writer; |
33 |
import java.nio.file.Files; |
|
52938 | 34 |
import java.util.ArrayList; |
35 |
import java.util.List; |
|
34735
0f41d334aa24
8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents:
32525
diff
changeset
|
36 |
import java.util.function.Function; |
38488 | 37 |
import java.util.stream.Collectors; |
52938 | 38 |
import java.util.stream.StreamSupport; |
39 |
||
40 |
import jdk.internal.org.jline.reader.Candidate; |
|
41 |
import jdk.internal.org.jline.reader.CompletingParsedLine; |
|
42 |
import jdk.internal.org.jline.reader.EOFError; |
|
43 |
import jdk.internal.org.jline.reader.History; |
|
44 |
import jdk.internal.org.jline.reader.LineReader; |
|
45 |
import jdk.internal.org.jline.reader.LineReader.Option; |
|
46 |
import jdk.internal.org.jline.reader.LineReaderBuilder; |
|
47 |
import jdk.internal.org.jline.reader.Parser; |
|
48 |
import jdk.internal.org.jline.reader.Parser.ParseContext; |
|
49 |
import jdk.internal.org.jline.reader.Widget; |
|
50 |
import jdk.internal.org.jline.reader.impl.LineReaderImpl; |
|
51 |
import jdk.internal.org.jline.reader.impl.completer.ArgumentCompleter.ArgumentLine; |
|
52 |
import jdk.internal.org.jline.terminal.Attributes.LocalFlag; |
|
53 |
import jdk.internal.org.jline.terminal.Terminal; |
|
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
54 |
|
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
55 |
class Console implements AutoCloseable { |
34735
0f41d334aa24
8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents:
32525
diff
changeset
|
56 |
private static final String DOCUMENTATION_SHORTCUT = "\033\133\132"; //Shift-TAB |
52938 | 57 |
private final LineReader in; |
38488 | 58 |
private final File historyFile; |
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
59 |
|
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
32240
diff
changeset
|
60 |
Console(final InputStream cmdin, final PrintStream cmdout, final File historyFile, |
38488 | 61 |
final NashornCompleter completer, final Function<String, String> docHelper) throws IOException { |
62 |
this.historyFile = historyFile; |
|
63 |
||
52938 | 64 |
Parser parser = (line, cursor, context) -> { |
65 |
if (context == ParseContext.COMPLETE) { |
|
66 |
List<Candidate> candidates = new ArrayList<>(); |
|
67 |
int anchor = completer.complete(line, cursor, candidates); |
|
68 |
anchor = Math.min(anchor, line.length()); |
|
69 |
return new CompletionLine(line.substring(anchor), cursor, candidates); |
|
70 |
} else if (!completer.isComplete(line)) { |
|
71 |
throw new EOFError(cursor, cursor, line); |
|
38488 | 72 |
} |
52938 | 73 |
return new ArgumentLine(line, cursor); |
74 |
}; |
|
75 |
in = LineReaderBuilder.builder() |
|
76 |
.option(Option.DISABLE_EVENT_EXPANSION, true) |
|
77 |
.completer((in, line, candidates) -> candidates.addAll(((CompletionLine) line).candidates)) |
|
78 |
.parser(parser) |
|
79 |
.build(); |
|
80 |
if (historyFile.exists()) { |
|
81 |
StringBuilder line = new StringBuilder(); |
|
82 |
for (String h : Files.readAllLines(historyFile.toPath())) { |
|
83 |
if (line.length() > 0) { |
|
84 |
line.append("\n"); |
|
85 |
} |
|
86 |
line.append(h); |
|
87 |
try { |
|
88 |
parser.parse(line.toString(), line.length()); |
|
89 |
in.getHistory().add(line.toString()); |
|
90 |
line.delete(0, line.length()); |
|
91 |
} catch (EOFError e) { |
|
92 |
//continue; |
|
93 |
} |
|
94 |
} |
|
95 |
if (line.length() > 0) { |
|
96 |
in.getHistory().add(line.toString()); |
|
97 |
} |
|
98 |
} |
|
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
32240
diff
changeset
|
99 |
Runtime.getRuntime().addShutdownHook(new Thread((Runnable)this::saveHistory)); |
52938 | 100 |
bind(DOCUMENTATION_SHORTCUT, ()->showDocumentation(docHelper)); |
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
101 |
} |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
102 |
|
52938 | 103 |
String readLine(final String prompt, final String continuationPrompt) throws IOException { |
104 |
in.setVariable(LineReader.SECONDARY_PROMPT_PATTERN, continuationPrompt); |
|
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
105 |
return in.readLine(prompt); |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
106 |
} |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
107 |
|
52938 | 108 |
String readUserLine(final String prompt) throws IOException { |
109 |
Parser prevParser = in.getParser(); |
|
110 |
||
111 |
try { |
|
112 |
((LineReaderImpl) in).setParser((line, cursor, context) -> new ArgumentLine(line, cursor)); |
|
113 |
return in.readLine(prompt); |
|
114 |
} finally { |
|
115 |
((LineReaderImpl) in).setParser(prevParser); |
|
116 |
} |
|
117 |
} |
|
118 |
||
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
119 |
@Override |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
120 |
public void close() { |
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
32240
diff
changeset
|
121 |
saveHistory(); |
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
122 |
} |
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
123 |
|
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
32240
diff
changeset
|
124 |
private void saveHistory() { |
38488 | 125 |
try (Writer out = Files.newBufferedWriter(historyFile.toPath())) { |
126 |
String lineSeparator = System.getProperty("line.separator"); |
|
127 |
||
52938 | 128 |
out.write(StreamSupport.stream(getHistory().spliterator(), false) |
129 |
.map(e -> e.line()) |
|
130 |
.collect(Collectors.joining(lineSeparator))); |
|
32242
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
32240
diff
changeset
|
131 |
} catch (final IOException exp) {} |
bbc1ebbb5cdc
8133777: Use file based persistence for history instead of preferences
sundar
parents:
32240
diff
changeset
|
132 |
} |
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
133 |
|
52938 | 134 |
History getHistory() { |
135 |
return in.getHistory(); |
|
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
136 |
} |
32313
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
137 |
|
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
138 |
boolean terminalEditorRunning() { |
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
139 |
Terminal terminal = in.getTerminal(); |
52938 | 140 |
return !terminal.getAttributes().getLocalFlag(LocalFlag.ICANON); |
32313
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
141 |
} |
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
142 |
|
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
143 |
void suspend() { |
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
144 |
} |
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
145 |
|
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
146 |
void resume() { |
52938 | 147 |
} |
148 |
||
149 |
private void bind(String shortcut, Widget action) { |
|
150 |
in.getKeyMaps().get(LineReader.MAIN).bind(action, shortcut); |
|
32313
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
151 |
} |
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
152 |
|
52938 | 153 |
private boolean showDocumentation(final Function<String, String> docHelper) { |
154 |
final String buffer = in.getBuffer().toString(); |
|
155 |
final int cursor = in.getBuffer().cursor(); |
|
156 |
final String doc = docHelper.apply(buffer.substring(0, cursor)); |
|
157 |
if (doc != null) { |
|
158 |
in.getTerminal().writer().println(); |
|
159 |
in.printAbove(doc); |
|
160 |
return true; |
|
161 |
} else { |
|
162 |
return false; |
|
32313
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
163 |
} |
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
164 |
} |
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
165 |
|
52938 | 166 |
private static final class CompletionLine extends ArgumentLine implements CompletingParsedLine { |
167 |
public final List<Candidate> candidates; |
|
168 |
||
169 |
public CompletionLine(String word, int cursor, List<Candidate> candidates) { |
|
170 |
super(word, cursor); |
|
171 |
this.candidates = candidates; |
|
32313
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
172 |
} |
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
173 |
|
52938 | 174 |
public CharSequence escape(CharSequence candidate, boolean complete) { |
175 |
return candidate; |
|
32313
11b284f8c4c6
8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
32242
diff
changeset
|
176 |
} |
34735
0f41d334aa24
8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents:
32525
diff
changeset
|
177 |
|
52938 | 178 |
public int rawWordCursor() { |
179 |
return word().length(); |
|
34735
0f41d334aa24
8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents:
32525
diff
changeset
|
180 |
} |
0f41d334aa24
8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents:
32525
diff
changeset
|
181 |
|
52938 | 182 |
public int rawWordLength() { |
183 |
return word().length(); |
|
34735
0f41d334aa24
8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents:
32525
diff
changeset
|
184 |
} |
0f41d334aa24
8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents:
32525
diff
changeset
|
185 |
} |
32152
705d4af0d4d7
8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff
changeset
|
186 |
} |