From 4c4031d4133ae53e270e0d57a19ff0e170c1e1a1 Mon Sep 17 00:00:00 2001 From: mattirn Date: Fri, 12 Nov 2021 18:47:34 +0100 Subject: [PATCH] Groovy REPL: highlight comments in command line --- .../jline/console/impl/SystemHighlighter.java | 53 +++++++------------ demo/src/main/scripts/args.nanorc | 2 + demo/src/main/scripts/command.nanorc | 1 + demo/src/main/scripts/groovy.nanorc | 4 +- 4 files changed, 24 insertions(+), 36 deletions(-) diff --git a/console/src/main/java/org/jline/console/impl/SystemHighlighter.java b/console/src/main/java/org/jline/console/impl/SystemHighlighter.java index c391006fc..f84df8126 100644 --- a/console/src/main/java/org/jline/console/impl/SystemHighlighter.java +++ b/console/src/main/java/org/jline/console/impl/SystemHighlighter.java @@ -38,6 +38,7 @@ public class SystemHighlighter extends DefaultHighlighter { protected final SyntaxHighlighter langHighlighter; protected final SystemRegistry systemRegistry; protected final Map fileHighlight = new HashMap<>(); + private int commandIndex; public SystemHighlighter(SyntaxHighlighter commandHighlighter, SyntaxHighlighter argsHighlighter , SyntaxHighlighter langHighlighter) { @@ -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)) { @@ -81,10 +84,10 @@ 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(); } @@ -92,18 +95,17 @@ protected AttributedString systemHighlight(LineReader reader, String buffer) { } 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 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; @@ -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 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++) { @@ -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(); @@ -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 { @@ -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); @@ -256,7 +241,7 @@ 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); } @@ -264,7 +249,7 @@ private void highlightArgs(String args, AttributedStringBuilder asb) { 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); } diff --git a/demo/src/main/scripts/args.nanorc b/demo/src/main/scripts/args.nanorc index e94e6c5b5..30de60777 100644 --- a/demo/src/main/scripts/args.nanorc +++ b/demo/src/main/scripts/args.nanorc @@ -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 " + +| + +" \ No newline at end of file diff --git a/demo/src/main/scripts/command.nanorc b/demo/src/main/scripts/command.nanorc index 2f48d612b..dc55cab81 100644 --- a/demo/src/main/scripts/command.nanorc +++ b/demo/src/main/scripts/command.nanorc @@ -3,3 +3,4 @@ syntax "COMMAND" color green "[a-zA-Z]+[a-zA-Z0-9]*" color yellow ".*=" color white "(\"|'|\.|=|:|\[|,|\])" +color blue start="/\*" end="\*/" diff --git a/demo/src/main/scripts/groovy.nanorc b/demo/src/main/scripts/groovy.nanorc index 606f81004..175942388 100644 --- a/demo/src/main/scripts/groovy.nanorc +++ b/demo/src/main/scripts/groovy.nanorc @@ -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)"