Skip to content

Commit

Permalink
Nano SyntaxHighlighter fails to highlight strings with line delimite…
Browse files Browse the repository at this point in the history
…rs, fixes #742
  • Loading branch information
mattirn committed Nov 12, 2021
1 parent fc22be3 commit 045b3c8
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -1521,25 +1521,43 @@ private void addRules(List<HighlightRule> rules) {
this.rules.addAll(rules);
}

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

public AttributedString highlight(String string) {
return highlight(new AttributedString(string));
return splitAndHighlight(new AttributedString(string));
}

public AttributedString highlight(AttributedStringBuilder asb) {
return highlight(asb.toAttributedString());
return splitAndHighlight(asb.toAttributedString());
}

public AttributedString highlight(AttributedString line) {
if (rules.isEmpty()) {
return line;
public AttributedString highlight(AttributedString attributedString) {
return splitAndHighlight(attributedString);
}

private AttributedString splitAndHighlight(AttributedString attributedString) {
AttributedStringBuilder asb = new AttributedStringBuilder();
boolean first = true;
for (AttributedString s : attributedString.columnSplitLength(Integer.MAX_VALUE)) {
if (!first) {
asb.append("\n");
}
asb.append(_highlight(s));
first = false;
}
return asb.toAttributedString();
}

private AttributedStringBuilder _highlight(AttributedString line) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(line);
if (rules.isEmpty()) {
return asb;
}
int startId = ruleStartId;
boolean endHighlight = startEndHighlight;
for (int i = startId; i < (endHighlight ? startId + 1 : rules.size()); i++) {
Expand Down Expand Up @@ -1574,7 +1592,7 @@ public AttributedString highlight(AttributedString line) {
} else {
ruleStartId = i;
startEndHighlight = true;
a.append(asb.columnSubSequence(start.start(),asb.length()), rule.getStyle());
a.append(asb.columnSubSequence(start.start(), asb.length()), rule.getStyle());
done = true;
}
asb = a;
Expand All @@ -1586,7 +1604,7 @@ public AttributedString highlight(AttributedString line) {
break;
}
}
return asb.toAttributedString();
return asb;
}

}
Expand Down

0 comments on commit 045b3c8

Please sign in to comment.