-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Groovy REPL: highlight command and groovy language syntax
- Loading branch information
Showing
8 changed files
with
172 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
console/src/main/java/org/jline/console/impl/SystemHighlighter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2002-2020, the original author or authors. | ||
* | ||
* This software is distributable under the BSD license. See the terms of the | ||
* BSD license in the documentation provided with this software. | ||
* | ||
* https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
package org.jline.console.impl; | ||
|
||
import org.jline.builtins.Nano.SyntaxHighlighter; | ||
import org.jline.console.SystemRegistry; | ||
import org.jline.reader.LineReader; | ||
import org.jline.reader.Parser; | ||
import org.jline.reader.impl.DefaultHighlighter; | ||
import org.jline.utils.AttributedString; | ||
import org.jline.utils.AttributedStringBuilder; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Highlight command and language syntax using nanorc highlighter. | ||
* | ||
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a> | ||
*/ | ||
public class SystemHighlighter extends DefaultHighlighter { | ||
private final SyntaxHighlighter commandHighlighter; | ||
private final SyntaxHighlighter argsHighlighter; | ||
private final SyntaxHighlighter langHighlighter; | ||
private Pattern errorPattern; | ||
private int errorIndex = -1; | ||
|
||
public SystemHighlighter(SyntaxHighlighter commandHighlighter, SyntaxHighlighter argsHighlighter | ||
, SyntaxHighlighter langHighlighter) { | ||
this.commandHighlighter = commandHighlighter; | ||
this.argsHighlighter = argsHighlighter; | ||
this.langHighlighter = langHighlighter; | ||
} | ||
|
||
@Override | ||
public void setErrorPattern(Pattern errorPattern) { | ||
this.errorPattern = errorPattern; | ||
super.setErrorPattern(errorPattern); | ||
} | ||
|
||
@Override | ||
public void setErrorIndex(int errorIndex) { | ||
this.errorIndex = errorIndex; | ||
super.setErrorIndex(errorIndex); | ||
} | ||
|
||
@Override | ||
public AttributedString highlight(LineReader reader, String buffer) { | ||
return doDefaultHighlight(reader) ? super.highlight(reader, buffer) : systemHighlight(reader.getParser(), buffer); | ||
} | ||
|
||
private boolean doDefaultHighlight(LineReader reader) { | ||
String search = reader.getSearchTerm(); | ||
return ((search != null && search.length() > 0) || reader.getRegionActive() != LineReader.RegionType.NONE | ||
|| errorIndex > -1 || errorPattern != null); | ||
} | ||
|
||
private AttributedString systemHighlight(Parser parser, String buffer) { | ||
AttributedString out; | ||
if (buffer.trim().isEmpty()) { | ||
out = new AttributedStringBuilder().append(buffer).toAttributedString(); | ||
} else if (SystemRegistry.get().isCommandOrScript(parser.getCommand(buffer.trim().split("\\s+")[0]))) { | ||
if (commandHighlighter != null || argsHighlighter != null) { | ||
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; | ||
} | ||
} | ||
AttributedStringBuilder asb = new AttributedStringBuilder(); | ||
if (idx < 0) { | ||
highlightCommand(buffer, asb); | ||
} else { | ||
highlightCommand(buffer.substring(0, idx), asb); | ||
if (argsHighlighter != null) { | ||
asb.append(argsHighlighter.highlight(buffer.substring(idx))); | ||
} else { | ||
asb.append(buffer.substring(idx)); | ||
} | ||
} | ||
out = asb.toAttributedString(); | ||
} else { | ||
out = new AttributedStringBuilder().append(buffer).toAttributedString(); | ||
} | ||
} else if (langHighlighter != null) { | ||
out = langHighlighter.highlight(buffer); | ||
} else { | ||
out = new AttributedStringBuilder().append(buffer).toAttributedString(); | ||
} | ||
return out; | ||
} | ||
|
||
private void highlightCommand(String command, AttributedStringBuilder asb) { | ||
if (commandHighlighter != null) { | ||
asb.append(commandHighlighter.highlight(command)); | ||
} else { | ||
asb.append(command); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
syntax "ARGS" | ||
|
||
color brightblue "\<[-]?[0-9]*([Ee][+-]?[0-9]+)?\>" "\<[-]?[0](\.[0-9]+)?\>" | ||
color yellow ""(\\.|[^"])*"|'(\\.|[^'])*'|[a-zA-Z]+[a-zA-Z0-9]*" | ||
color green "\<(console|grab|inspect)\>" | ||
color cyan "\<null\>" | ||
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 ,red " + +| + +" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
syntax "COMMAND" | ||
|
||
color green "[a-zA-Z]+[a-zA-Z0-9]*" | ||
color yellow ".*=" | ||
color white "(\"|'|\.|=|:|\[|,|\])" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
## Here is an example for Groovy. | ||
## | ||
syntax "Groovy" "\.groovy$" | ||
color green "\<(boolean|byte|char|double|float|int|long|new|short|this|transient|void|def|it)\>" | ||
color red "\<(break|case|catch|continue|default|do|else|finally|for|if|return|switch|throw|try|while)\>" | ||
color green,,faint "(([a-z]{2,}[.]{1}){2,10}([a-z]{2,}){0,1})" | ||
color green,,faint "\<(print|println|sleep)\>" | ||
color green "\<[A-Z]{0,2}([A-Z]{1}[a-z]+){1,}\>" | ||
color cyan "\<(abstract|class|extends|final|implements|import|instanceof|interface|native|package|private|protected|public|static|strictfp|super|synchronized|throws|volatile)\>" | ||
color red ""[^"]*"" | ||
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 brightwhite,yellow "(FIXME|TODO|XXX)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters