Skip to content

Commit

Permalink
Nano: make SyntaxHighlighter public
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Oct 25, 2019
1 parent 0fb4a53 commit 7a9396d
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -1419,16 +1419,16 @@ void replaceFromCursor(int chars, String string) {
}
}

protected static class SyntaxHighlighter {
public static class SyntaxHighlighter {
private List<HighlightRule> rules = new ArrayList<>();
private int ruleStartId = 0;

private SyntaxHighlighter() {}

public static SyntaxHighlighter build(List<Path> syntaxFiles, String file, String syntaxName) {
protected static SyntaxHighlighter build(List<Path> syntaxFiles, String file, String syntaxName) {
SyntaxHighlighter out = new SyntaxHighlighter();
List<HighlightRule> 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 {
Expand All @@ -1447,6 +1447,46 @@ public static SyntaxHighlighter build(List<Path> 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<Path> 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<String> 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<HighlightRule> rules) {
this.rules.addAll(rules);
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -1584,6 +1628,10 @@ private static class NanorcParser {
private List<HighlightRule> 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;
Expand Down

0 comments on commit 7a9396d

Please sign in to comment.