Skip to content

Commit

Permalink
Nano SyntaxHighlighter: build highlighter from given nanorc url
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed May 23, 2020
1 parent f671797 commit 0eb2410
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
20 changes: 13 additions & 7 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1008,14 +1008,20 @@ private AttributedString highlight(CmdDesc cmdDesc) {
}

private SyntaxHighlighter valueHighlighter(String style) {
Path nanorc = configPath != null ? configPath.getConfig("jnanorc") : null;
if (engine.hasVariable(VAR_NANORC)) {
nanorc = Paths.get((String)engine.get(VAR_NANORC));
}
if (nanorc == null) {
nanorc = Paths.get("/etc/nanorc");
SyntaxHighlighter out;
if (style.matches("[a-z]+:.*")) {
out = SyntaxHighlighter.build(style);
} else {
Path nanorc = configPath != null ? configPath.getConfig("jnanorc") : null;
if (engine.hasVariable(VAR_NANORC)) {
nanorc = Paths.get((String)engine.get(VAR_NANORC));
}
if (nanorc == null) {
nanorc = Paths.get("/etc/nanorc");
}
out = nanorc != null ? SyntaxHighlighter.build(nanorc, style) : null;
}
return nanorc != null ? SyntaxHighlighter.build(nanorc, style) : null;
return out;
}

private String truncate4nanorc(String obj) {
Expand Down
37 changes: 30 additions & 7 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand Down Expand Up @@ -1430,8 +1431,8 @@ protected static SyntaxHighlighter build(List<Path> syntaxFiles, String file, St
List<HighlightRule> defaultRules = new ArrayList<>();
if (syntaxName == null || (syntaxName != null && !syntaxName.equals("none"))) {
for (Path p: syntaxFiles) {
NanorcParser parser = new NanorcParser(p, syntaxName, file);
try {
NanorcParser parser = new NanorcParser(p, syntaxName, file);
parser.parse();
if (parser.matches()) {
out.addRules(parser.getHighlightRules());
Expand Down Expand Up @@ -1487,6 +1488,29 @@ public static SyntaxHighlighter build(Path nanorc, String syntaxName) {
return out;
}

/**
* Build SyntaxHighlighter
*
* @param nanorcUrl Url of nanorc file
* @return SyntaxHighlighter
*/
public static SyntaxHighlighter build(String nanorcUrl) {
SyntaxHighlighter out = new SyntaxHighlighter();
InputStream inputStream;
try {
if (nanorcUrl.startsWith("classpath:")) {
inputStream = new Source.ResourceSource(nanorcUrl.substring(10), null).read();
} else {
inputStream = new Source.URLSource(new URL(nanorcUrl), null).read();
}
NanorcParser parser = new NanorcParser(inputStream, null, null);
parser.parse();
out.addRules(parser.getHighlightRules());
} catch (IOException e) {
}
return out;
}

private void addRules(List<HighlightRule> rules) {
this.rules.addAll(rules);
}
Expand Down Expand Up @@ -1621,25 +1645,24 @@ public static RuleType evalRuleType(List<String> colorCfg) {

private static class NanorcParser {
private static final String DEFAULT_SYNTAX = "default";
private File file;
private String name;
private String target;
private boolean matches = false;
private List<HighlightRule> highlightRules = new ArrayList<>();
private String syntaxName;
private BufferedReader reader;

public NanorcParser(Path file, String name) {
this(file, name, null);
public NanorcParser(Path file, String name, String target) throws IOException {
this(new Source.PathSource(file, null).read(), name, target);
}

public NanorcParser(Path file, String name, String target) {
this.file = file.toFile();
public NanorcParser(InputStream in, String name, String target) {
this.reader = new BufferedReader(new InputStreamReader(in));
this.name = name;
this.target = target;
}

public void parse() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line!= null) {
line = line.trim();
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/scripts/gron.nanorc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ color yellow ""(\\.|[^"])*"|'(\\.|[^'])*'|[a-zA-Z]+[a-zA-Z0-9]*"
color cyan "\<null\>"
color brightcyan "\<(true|false)\>"
color brightyellow "\"(\\"|[^"])*\"\s*:" "'(\'|[^'])*'\s*:" "(\[|,)\s*[a-zA-Z0-9]*\s*:"
color white "(:|\[|,)"
color white "(:|\[|,|\])"
color magenta "\\u[0-9a-fA-F]{4}|\\[bfnrt'"/\\]"
color ,green "[[:space:]]+$"
color ,red " + +| + +"
2 changes: 2 additions & 0 deletions groovy/src/main/java/org/jline/script/GroovyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public Object grab(CommandInput input) {
options.put(Printer.SKIP_DEFAULT_OPTIONS, true);
options.put(Printer.MAX_DEPTH, 1);
options.put(Printer.INDENTION, 4);
options.put(Printer.VALUE_STYLE, "classpath:/org/jline/groovy/gron.nanorc");
printer.println(options, resp);
} else if (arg.startsWith("-")) {
throw new IllegalArgumentException("Unknown command option: " + arg);
Expand Down Expand Up @@ -197,6 +198,7 @@ public Object inspect(CommandInput input) {
options.put(Printer.COLUMNS, ObjectInspector.METHOD_COLUMNS);
options.put(Printer.MAX_DEPTH, 1);
options.put(Printer.INDENTION, 4);
options.put(Printer.VALUE_STYLE, "classpath:/org/jline/groovy/gron.nanorc");
printer.println(options, out);
} catch (Exception e) {
saveException(e);
Expand Down
12 changes: 12 additions & 0 deletions groovy/src/main/resources/org/jline/groovy/gron.nanorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax "GRON" "\.gron$"
header "^\[$"

color brightblue "\<[-]?[0-9]*([Ee][+-]?[0-9]+)?\>" "\<[-]?[0](\.[0-9]+)?\>"
color yellow ""(\\.|[^"])*"|'(\\.|[^'])*'|[a-zA-Z]+[a-zA-Z0-9]*"
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 ,green "[[:space:]]+$"
color ,red " + +| + +"

0 comments on commit 0eb2410

Please sign in to comment.