Skip to content

Commit

Permalink
Groovy REPL: highlight comments in command line
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Nov 12, 2021
1 parent 045b3c8 commit 4c4031d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 36 deletions.
53 changes: 19 additions & 34 deletions console/src/main/java/org/jline/console/impl/SystemHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class SystemHighlighter extends DefaultHighlighter {
protected final SyntaxHighlighter langHighlighter;
protected final SystemRegistry systemRegistry;
protected final Map<String, FileHighlightCommand> fileHighlight = new HashMap<>();
private int commandIndex;

public SystemHighlighter(SyntaxHighlighter commandHighlighter, SyntaxHighlighter argsHighlighter
, SyntaxHighlighter langHighlighter) {
Expand Down Expand Up @@ -71,7 +72,9 @@ private boolean doDefaultHighlight(LineReader reader) {
protected AttributedString systemHighlight(LineReader reader, String buffer) {
AttributedString out;
Parser parser = reader.getParser();
String command = parser.getCommand(buffer);
ParsedLine pl = parser.parse(buffer, 0, Parser.ParseContext.COMPLETE);
String command = pl.words().size() > 0 ? parser.getCommand(pl.words().get(0)) : "";
commandIndex = buffer.indexOf(command);
if (buffer.trim().isEmpty()) {
out = new AttributedStringBuilder().append(buffer).toAttributedString();
} else if (fileHighlight.containsKey(command)) {
Expand All @@ -81,29 +84,28 @@ protected AttributedString systemHighlight(LineReader reader, String buffer) {
} else {
out = doFileOptsHighlight(reader, buffer, fhc);
}
} else if (systemRegistry.isCommandOrScript(command) || systemRegistry.isCommandAlias(command)) {
} else if (systemRegistry.isCommandOrScript(command) || systemRegistry.isCommandAlias(command) || command.isEmpty()) {
out = doCommandHighlight(buffer);
} else if (langHighlighter != null) {
out = langHighlighter.highlight(buffer);
out = langHighlighter.reset().highlight(buffer);
} else {
out = new AttributedStringBuilder().append(buffer).toAttributedString();
}
return out;
}

protected AttributedString doFileOptsHighlight(LineReader reader, String buffer, FileHighlightCommand fhc) {
int idx0 = commandIndex(buffer);
AttributedStringBuilder asb = new AttributedStringBuilder();
if (idx0 < 0) {
if (commandIndex < 0) {
highlightCommand(buffer, asb);
} else {
highlightCommand(buffer.substring(0, idx0), asb);
highlightCommand(buffer.substring(0, commandIndex), asb);
ParsedLine parsedLine = reader.getParser().parse(buffer, buffer.length() + 1, Parser.ParseContext.COMPLETE);
List<String> words = parsedLine.words();
if (!fhc.isSubcommand() || (words.size() > 2 && fhc.getSubcommand().equals(words.get(1)))) {
int firstArg = fhc.isSubcommand() ? 1 : 0;
int idx = buffer.indexOf(words.get(firstArg)) + words.get(firstArg).length() + 1;
highlightArgs(buffer.substring(idx0, idx), asb);
highlightArgs(buffer.substring(commandIndex, idx), asb);
boolean fileOption = false;
for (int i = firstArg + 1; i < words.size(); i++) {
int nextIdx = buffer.substring(idx).indexOf(words.get(i)) + idx;
Expand All @@ -126,25 +128,24 @@ protected AttributedString doFileOptsHighlight(LineReader reader, String buffer,
idx = nextIdx + word.length();
}
} else {
highlightArgs(buffer.substring(idx0), asb);
highlightArgs(buffer.substring(commandIndex), asb);
}
}
return asb.toAttributedString();
}

protected AttributedString doFileArgsHighlight(LineReader reader, String buffer, FileHighlightCommand fhc) {
int idx0 = commandIndex(buffer);
AttributedStringBuilder asb = new AttributedStringBuilder();
if (idx0 < 0) {
if (commandIndex < 0) {
highlightCommand(buffer, asb);
} else {
highlightCommand(buffer.substring(0, idx0), asb);
highlightCommand(buffer.substring(0, commandIndex), asb);
ParsedLine parsedLine = reader.getParser().parse(buffer, buffer.length() + 1, Parser.ParseContext.COMPLETE);
List<String> words = parsedLine.words();
if (!fhc.isSubcommand() || (words.size() > 2 && fhc.getSubcommand().equals(words.get(1)))) {
int firstArg = fhc.isSubcommand() ? 1 : 0;
int idx = buffer.indexOf(words.get(firstArg)) + words.get(firstArg).length();
highlightArgs(buffer.substring(idx0, idx), asb);
highlightArgs(buffer.substring(commandIndex, idx), asb);
for (int i = firstArg + 1; i < words.size(); i++) {
int nextIdx = buffer.substring(idx).indexOf(words.get(i)) + idx;
for (int j = idx; j < nextIdx; j++) {
Expand All @@ -154,7 +155,7 @@ protected AttributedString doFileArgsHighlight(LineReader reader, String buffer,
idx = nextIdx + words.get(i).length();
}
} else {
highlightArgs(buffer.substring(idx0), asb);
highlightArgs(buffer.substring(commandIndex), asb);
}
}
return asb.toAttributedString();
Expand All @@ -163,13 +164,12 @@ protected AttributedString doFileArgsHighlight(LineReader reader, String buffer,
protected AttributedString doCommandHighlight(String buffer) {
AttributedString out;
if (commandHighlighter != null || argsHighlighter != null) {
int idx = commandIndex(buffer);
AttributedStringBuilder asb = new AttributedStringBuilder();
if (idx < 0) {
if (commandIndex < 0) {
highlightCommand(buffer, asb);
} else {
highlightCommand(buffer.substring(0, idx), asb);
highlightArgs(buffer.substring(idx), asb);
highlightCommand(buffer.substring(0, commandIndex), asb);
highlightArgs(buffer.substring(commandIndex), asb);
}
out = asb.toAttributedString();
} else {
Expand All @@ -178,21 +178,6 @@ protected AttributedString doCommandHighlight(String buffer) {
return out;
}

private int commandIndex(String buffer) {
int idx = -1;
boolean cmdFound = false;
for (int i = 0; i < buffer.length(); i++) {
char c = buffer.charAt(i);
if (c != ' ') {
cmdFound = true;
} else if (cmdFound) {
idx = i;
break;
}
}
return idx;
}

private void highlightFileArg(LineReader reader, String arg, AttributedStringBuilder asb) {
if (arg.startsWith("-")) {
highlightArgs(arg, asb);
Expand Down Expand Up @@ -256,15 +241,15 @@ private void highlightFile(Path path, AttributedStringBuilder asb) {

private void highlightArgs(String args, AttributedStringBuilder asb) {
if (argsHighlighter != null) {
asb.append(argsHighlighter.highlight(args));
asb.append(argsHighlighter.reset().highlight(args));
} else {
asb.append(args);
}
}

private void highlightCommand(String command, AttributedStringBuilder asb) {
if (commandHighlighter != null) {
asb.append(commandHighlighter.highlight(command));
asb.append(commandHighlighter.reset().highlight(command));
} else {
asb.append(command);
}
Expand Down
2 changes: 2 additions & 0 deletions demo/src/main/scripts/args.nanorc
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ color brightcyan "\<(true|false)\>"
color brightyellow "\"(\\"|[^"])*\"\s*:" "'(\'|[^'])*'\s*:" "(\[|,)\s*[a-zA-Z0-9]*\s*:"
color white "(:|\[|,|\])"
color magenta "\\u[0-9a-fA-F]{4}|\\[bfnrt'"/\\]"
color blue start="/\*" end="\*/"
color blue "(//.*|#.*)"
color ,red " + +| + +"
1 change: 1 addition & 0 deletions demo/src/main/scripts/command.nanorc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ syntax "COMMAND"
color green "[a-zA-Z]+[a-zA-Z0-9]*"
color yellow ".*="
color white "(\"|'|\.|=|:|\[|,|\])"
color blue start="/\*" end="\*/"
4 changes: 2 additions & 2 deletions demo/src/main/scripts/groovy.nanorc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ color yellow "\<(true|false|null)\>"
color yellow "\<[A-Z]+([_]{1}[A-Z]+){0,}\>"
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"
color blue "//.*"
#color blue start="/\*" end="\*/"
#color brightblue start="/\*\*" end="\*/"
color blue start="/\*" end="\*/"
color brightblue start="/\*\*" end="\*/"
color brightwhite,yellow "(FIXME|TODO|XXX)"

0 comments on commit 4c4031d

Please sign in to comment.