Skip to content

Commit

Permalink
Nano highlighter xml highlighting differs considerable from GNU nano…
Browse files Browse the repository at this point in the history
… highlight, fixes #614
  • Loading branch information
mattirn committed Dec 5, 2020
1 parent 30860bc commit 172644f
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,7 @@ void replaceFromCursor(int chars, String string) {
*/
public static class SyntaxHighlighter {
private final List<HighlightRule> rules = new ArrayList<>();
private boolean startEndHighlight;
private int ruleStartId = 0;

private SyntaxHighlighter() {}
Expand Down Expand Up @@ -1534,6 +1535,7 @@ private void addRules(List<HighlightRule> rules) {

public void reset() {
ruleStartId = 0;
startEndHighlight = false;
}

public AttributedString highlight(String string) {
Expand All @@ -1550,7 +1552,9 @@ public AttributedString highlight(AttributedString line) {
}
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(line);
for (int i = ruleStartId; i < rules.size(); i++) {
int startId = ruleStartId;
boolean endHighlight = startEndHighlight;
for (int i = startId; i < (endHighlight ? startId + 1 : rules.size()); i++) {
HighlightRule rule = rules.get(i);
switch (rule.getType()) {
case PATTERN:
Expand All @@ -1562,12 +1566,12 @@ public AttributedString highlight(AttributedString line) {
Matcher end = rule.getEnd().matcher(asb.toAttributedString());
while (!done) {
AttributedStringBuilder a = new AttributedStringBuilder();
if (ruleStartId == i) { // first rule should never be type
// START_END or we will fail here!
if (startEndHighlight && ruleStartId == i) {
if (end.find()) {
a.append(asb.columnSubSequence(0, end.end()),rule.getStyle());
a.append(asb.columnSubSequence(0, end.end()), rule.getStyle());
a.append(asb.columnSubSequence(end.end(), asb.length()));
ruleStartId = 0;
startEndHighlight = false;
} else {
a.append(asb, rule.getStyle());
done = true;
Expand All @@ -1581,6 +1585,7 @@ public AttributedString highlight(AttributedString line) {
a.append(asb.columnSubSequence(end.end(), asb.length()));
} else {
ruleStartId = i;
startEndHighlight = true;
a.append(asb.columnSubSequence(start.start(),asb.length()), rule.getStyle());
done = true;
}
Expand All @@ -1595,6 +1600,7 @@ public AttributedString highlight(AttributedString line) {
}
return asb.toAttributedString();
}

}

private static class HighlightRule {
Expand Down

0 comments on commit 172644f

Please sign in to comment.