Skip to content

Commit b5963ff

Browse files
nixel2007Copilot
andcommitted
fix: handle tokens with trailing line terminators in highlighter
Tokens from bsl-parser 0.31.0 may include trailing CR/LF characters in their text. When computing the highlight range end offset, this caused the offset to exceed the line length, resulting in IllegalArgumentException from SonarQube. Truncate token text at the first newline/CR to compute correct single-line highlight ranges. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 36bb40c commit b5963ff

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/main/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighter.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,22 @@ public void highlightToken(
184184
var charPositionInLine = token.getCharPositionInLine();
185185
String tokenText = token.getText();
186186

187+
var firstNewLine = tokenText.indexOf('\n');
188+
var firstCR = tokenText.indexOf('\r');
189+
int effectiveLength;
190+
if (firstNewLine >= 0 || firstCR >= 0) {
191+
var boundary = (firstNewLine >= 0 && firstCR >= 0)
192+
? Math.min(firstNewLine, firstCR)
193+
: Math.max(firstNewLine, firstCR);
194+
effectiveLength = boundary;
195+
} else {
196+
effectiveLength = (int) tokenText.codePoints().count();
197+
}
198+
187199
var range = Ranges.create(
188200
line,
189201
charPositionInLine,
190-
charPositionInLine + (int) tokenText.codePoints().count()
202+
charPositionInLine + effectiveLength
191203
);
192204

193205
highlightingData.add(new HighlightingData(range, typeOfText));

0 commit comments

Comments
 (0)