Skip to content

Commit 4c4031d

Browse files
committedNov 12, 2021
Groovy REPL: highlight comments in command line
1 parent 045b3c8 commit 4c4031d

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
lines changed
 

‎console/src/main/java/org/jline/console/impl/SystemHighlighter.java

+19-34
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class SystemHighlighter extends DefaultHighlighter {
3838
protected final SyntaxHighlighter langHighlighter;
3939
protected final SystemRegistry systemRegistry;
4040
protected final Map<String, FileHighlightCommand> fileHighlight = new HashMap<>();
41+
private int commandIndex;
4142

4243
public SystemHighlighter(SyntaxHighlighter commandHighlighter, SyntaxHighlighter argsHighlighter
4344
, SyntaxHighlighter langHighlighter) {
@@ -71,7 +72,9 @@ private boolean doDefaultHighlight(LineReader reader) {
7172
protected AttributedString systemHighlight(LineReader reader, String buffer) {
7273
AttributedString out;
7374
Parser parser = reader.getParser();
74-
String command = parser.getCommand(buffer);
75+
ParsedLine pl = parser.parse(buffer, 0, Parser.ParseContext.COMPLETE);
76+
String command = pl.words().size() > 0 ? parser.getCommand(pl.words().get(0)) : "";
77+
commandIndex = buffer.indexOf(command);
7578
if (buffer.trim().isEmpty()) {
7679
out = new AttributedStringBuilder().append(buffer).toAttributedString();
7780
} else if (fileHighlight.containsKey(command)) {
@@ -81,29 +84,28 @@ protected AttributedString systemHighlight(LineReader reader, String buffer) {
8184
} else {
8285
out = doFileOptsHighlight(reader, buffer, fhc);
8386
}
84-
} else if (systemRegistry.isCommandOrScript(command) || systemRegistry.isCommandAlias(command)) {
87+
} else if (systemRegistry.isCommandOrScript(command) || systemRegistry.isCommandAlias(command) || command.isEmpty()) {
8588
out = doCommandHighlight(buffer);
8689
} else if (langHighlighter != null) {
87-
out = langHighlighter.highlight(buffer);
90+
out = langHighlighter.reset().highlight(buffer);
8891
} else {
8992
out = new AttributedStringBuilder().append(buffer).toAttributedString();
9093
}
9194
return out;
9295
}
9396

9497
protected AttributedString doFileOptsHighlight(LineReader reader, String buffer, FileHighlightCommand fhc) {
95-
int idx0 = commandIndex(buffer);
9698
AttributedStringBuilder asb = new AttributedStringBuilder();
97-
if (idx0 < 0) {
99+
if (commandIndex < 0) {
98100
highlightCommand(buffer, asb);
99101
} else {
100-
highlightCommand(buffer.substring(0, idx0), asb);
102+
highlightCommand(buffer.substring(0, commandIndex), asb);
101103
ParsedLine parsedLine = reader.getParser().parse(buffer, buffer.length() + 1, Parser.ParseContext.COMPLETE);
102104
List<String> words = parsedLine.words();
103105
if (!fhc.isSubcommand() || (words.size() > 2 && fhc.getSubcommand().equals(words.get(1)))) {
104106
int firstArg = fhc.isSubcommand() ? 1 : 0;
105107
int idx = buffer.indexOf(words.get(firstArg)) + words.get(firstArg).length() + 1;
106-
highlightArgs(buffer.substring(idx0, idx), asb);
108+
highlightArgs(buffer.substring(commandIndex, idx), asb);
107109
boolean fileOption = false;
108110
for (int i = firstArg + 1; i < words.size(); i++) {
109111
int nextIdx = buffer.substring(idx).indexOf(words.get(i)) + idx;
@@ -126,25 +128,24 @@ protected AttributedString doFileOptsHighlight(LineReader reader, String buffer,
126128
idx = nextIdx + word.length();
127129
}
128130
} else {
129-
highlightArgs(buffer.substring(idx0), asb);
131+
highlightArgs(buffer.substring(commandIndex), asb);
130132
}
131133
}
132134
return asb.toAttributedString();
133135
}
134136

135137
protected AttributedString doFileArgsHighlight(LineReader reader, String buffer, FileHighlightCommand fhc) {
136-
int idx0 = commandIndex(buffer);
137138
AttributedStringBuilder asb = new AttributedStringBuilder();
138-
if (idx0 < 0) {
139+
if (commandIndex < 0) {
139140
highlightCommand(buffer, asb);
140141
} else {
141-
highlightCommand(buffer.substring(0, idx0), asb);
142+
highlightCommand(buffer.substring(0, commandIndex), asb);
142143
ParsedLine parsedLine = reader.getParser().parse(buffer, buffer.length() + 1, Parser.ParseContext.COMPLETE);
143144
List<String> words = parsedLine.words();
144145
if (!fhc.isSubcommand() || (words.size() > 2 && fhc.getSubcommand().equals(words.get(1)))) {
145146
int firstArg = fhc.isSubcommand() ? 1 : 0;
146147
int idx = buffer.indexOf(words.get(firstArg)) + words.get(firstArg).length();
147-
highlightArgs(buffer.substring(idx0, idx), asb);
148+
highlightArgs(buffer.substring(commandIndex, idx), asb);
148149
for (int i = firstArg + 1; i < words.size(); i++) {
149150
int nextIdx = buffer.substring(idx).indexOf(words.get(i)) + idx;
150151
for (int j = idx; j < nextIdx; j++) {
@@ -154,7 +155,7 @@ protected AttributedString doFileArgsHighlight(LineReader reader, String buffer,
154155
idx = nextIdx + words.get(i).length();
155156
}
156157
} else {
157-
highlightArgs(buffer.substring(idx0), asb);
158+
highlightArgs(buffer.substring(commandIndex), asb);
158159
}
159160
}
160161
return asb.toAttributedString();
@@ -163,13 +164,12 @@ protected AttributedString doFileArgsHighlight(LineReader reader, String buffer,
163164
protected AttributedString doCommandHighlight(String buffer) {
164165
AttributedString out;
165166
if (commandHighlighter != null || argsHighlighter != null) {
166-
int idx = commandIndex(buffer);
167167
AttributedStringBuilder asb = new AttributedStringBuilder();
168-
if (idx < 0) {
168+
if (commandIndex < 0) {
169169
highlightCommand(buffer, asb);
170170
} else {
171-
highlightCommand(buffer.substring(0, idx), asb);
172-
highlightArgs(buffer.substring(idx), asb);
171+
highlightCommand(buffer.substring(0, commandIndex), asb);
172+
highlightArgs(buffer.substring(commandIndex), asb);
173173
}
174174
out = asb.toAttributedString();
175175
} else {
@@ -178,21 +178,6 @@ protected AttributedString doCommandHighlight(String buffer) {
178178
return out;
179179
}
180180

181-
private int commandIndex(String buffer) {
182-
int idx = -1;
183-
boolean cmdFound = false;
184-
for (int i = 0; i < buffer.length(); i++) {
185-
char c = buffer.charAt(i);
186-
if (c != ' ') {
187-
cmdFound = true;
188-
} else if (cmdFound) {
189-
idx = i;
190-
break;
191-
}
192-
}
193-
return idx;
194-
}
195-
196181
private void highlightFileArg(LineReader reader, String arg, AttributedStringBuilder asb) {
197182
if (arg.startsWith("-")) {
198183
highlightArgs(arg, asb);
@@ -256,15 +241,15 @@ private void highlightFile(Path path, AttributedStringBuilder asb) {
256241

257242
private void highlightArgs(String args, AttributedStringBuilder asb) {
258243
if (argsHighlighter != null) {
259-
asb.append(argsHighlighter.highlight(args));
244+
asb.append(argsHighlighter.reset().highlight(args));
260245
} else {
261246
asb.append(args);
262247
}
263248
}
264249

265250
private void highlightCommand(String command, AttributedStringBuilder asb) {
266251
if (commandHighlighter != null) {
267-
asb.append(commandHighlighter.highlight(command));
252+
asb.append(commandHighlighter.reset().highlight(command));
268253
} else {
269254
asb.append(command);
270255
}

‎demo/src/main/scripts/args.nanorc

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ color brightcyan "\<(true|false)\>"
88
color brightyellow "\"(\\"|[^"])*\"\s*:" "'(\'|[^'])*'\s*:" "(\[|,)\s*[a-zA-Z0-9]*\s*:"
99
color white "(:|\[|,|\])"
1010
color magenta "\\u[0-9a-fA-F]{4}|\\[bfnrt'"/\\]"
11+
color blue start="/\*" end="\*/"
12+
color blue "(//.*|#.*)"
1113
color ,red " + +| + +"

‎demo/src/main/scripts/command.nanorc

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ syntax "COMMAND"
33
color green "[a-zA-Z]+[a-zA-Z0-9]*"
44
color yellow ".*="
55
color white "(\"|'|\.|=|:|\[|,|\])"
6+
color blue start="/\*" end="\*/"

‎demo/src/main/scripts/groovy.nanorc

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ color yellow "\<(true|false|null)\>"
1212
color yellow "\<[A-Z]+([_]{1}[A-Z]+){0,}\>"
1313
icolor yellow "\b(([1-9][0-9]+)|0+)\.[0-9]+\b" "\b[1-9][0-9]*\b" "\b0[0-7]*\b" "\b0x[1-9a-f][0-9a-f]*\b"
1414
color blue "//.*"
15-
#color blue start="/\*" end="\*/"
16-
#color brightblue start="/\*\*" end="\*/"
15+
color blue start="/\*" end="\*/"
16+
color brightblue start="/\*\*" end="\*/"
1717
color brightwhite,yellow "(FIXME|TODO|XXX)"

0 commit comments

Comments
 (0)
Please sign in to comment.