From 7a9396da2fc1f2c63528928da1500056f38fecdc Mon Sep 17 00:00:00 2001 From: mattirn Date: Fri, 25 Oct 2019 18:24:42 +0200 Subject: [PATCH] Nano: make SyntaxHighlighter public --- .../main/java/org/jline/builtins/Nano.java | 54 +++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/builtins/src/main/java/org/jline/builtins/Nano.java b/builtins/src/main/java/org/jline/builtins/Nano.java index 447a7c309..c09ce5df0 100644 --- a/builtins/src/main/java/org/jline/builtins/Nano.java +++ b/builtins/src/main/java/org/jline/builtins/Nano.java @@ -1419,16 +1419,16 @@ void replaceFromCursor(int chars, String string) { } } - protected static class SyntaxHighlighter { + public static class SyntaxHighlighter { private List rules = new ArrayList<>(); private int ruleStartId = 0; private SyntaxHighlighter() {} - public static SyntaxHighlighter build(List syntaxFiles, String file, String syntaxName) { + protected static SyntaxHighlighter build(List syntaxFiles, String file, String syntaxName) { SyntaxHighlighter out = new SyntaxHighlighter(); List defaultRules = new ArrayList<>(); - if (file != null && (syntaxName == null || (syntaxName != null && !syntaxName.equals("none")))) { + if (syntaxName == null || (syntaxName != null && !syntaxName.equals("none"))) { for (Path p: syntaxFiles) { NanorcParser parser = new NanorcParser(p, syntaxName, file); try { @@ -1447,6 +1447,46 @@ public static SyntaxHighlighter build(List syntaxFiles, String file, Strin return out; } + /** + * Build SyntaxHighlighter + * + * @param nanorc Path of nano config file jnanorc + * @param syntaxName syntax name e.g 'Java' + * @return SyntaxHighlighter + */ + public static SyntaxHighlighter build(Path nanorc, String syntaxName) { + SyntaxHighlighter out = new SyntaxHighlighter(); + List syntaxFiles = new ArrayList<>(); + try { + BufferedReader reader = new BufferedReader(new FileReader(nanorc.toFile())); + String line = reader.readLine(); + while (line != null) { + line = line.trim(); + if (line.length() > 0 && !line.startsWith("#")) { + List parts = Parser.split(line); + if (parts.get(0).equals("include")) { + if (parts.get(1).contains("*") || parts.get(1).contains("?")) { + PathMatcher pathMatcher = FileSystems + .getDefault().getPathMatcher("glob:" + parts.get(1)); + Files.find( + Paths.get(new File(parts.get(1)).getParent()), + Integer.MAX_VALUE, + (path, f) -> pathMatcher.matches(path)) + .forEach(p -> syntaxFiles.add(p)); + } else { + syntaxFiles.add(Paths.get(parts.get(1))); + } + } + } + line = reader.readLine(); + } + reader.close(); + out = build(syntaxFiles, null, syntaxName); + } catch (Exception e) { + } + return out; + } + private void addRules(List rules) { this.rules.addAll(rules); } @@ -1455,6 +1495,10 @@ public void reset() { ruleStartId = 0; } + public AttributedString highlight(String string) { + return highlight(new AttributedString(string)); + } + public AttributedString highlight(AttributedStringBuilder asb) { return highlight(asb.toAttributedString()); } @@ -1584,6 +1628,10 @@ private static class NanorcParser { private List highlightRules = new ArrayList<>(); private String syntaxName; + public NanorcParser(Path file, String name) { + this(file, name, null); + } + public NanorcParser(Path file, String name, String target) { this.file = file.toFile(); this.name = name;