Skip to content

Commit 12d025d

Browse files
Copilotnixel2007
andauthored
Fix: catch IllegalArgumentException during highlighting to prevent scanner crash
Agent-Logs-Url: https://github.com/1c-syntax/sonar-bsl-plugin-community/sessions/dbfba67b-b20e-4a21-bc5f-4f57190271e2 Co-authored-by: nixel2007 <1132840+nixel2007@users.noreply.github.com>
1 parent ff753eb commit 12d025d

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import lombok.Data;
2929
import lombok.EqualsAndHashCode;
3030
import lombok.RequiredArgsConstructor;
31+
import lombok.extern.slf4j.Slf4j;
3132
import org.antlr.v4.runtime.Token;
3233
import org.antlr.v4.runtime.Tokenizer;
3334
import org.eclipse.lsp4j.Range;
@@ -45,6 +46,7 @@
4546
import java.util.Set;
4647
import java.util.stream.Collectors;
4748

49+
@Slf4j
4850
@RequiredArgsConstructor
4951
public class BSLHighlighter {
5052

@@ -158,15 +160,19 @@ public void saveHighlighting(InputFile inputFile, DocumentContext documentContex
158160

159161
highlightingData.stream()
160162
.filter(HighlightingData::isActive)
161-
.forEach(data ->
162-
highlighting.highlight(
163-
data.getRange().getStart().getLine(),
164-
data.getRange().getStart().getCharacter(),
165-
data.getRange().getEnd().getLine(),
166-
data.getRange().getEnd().getCharacter(),
167-
data.getType()
168-
)
169-
);
163+
.forEach(data -> {
164+
try {
165+
highlighting.highlight(
166+
data.getRange().getStart().getLine(),
167+
data.getRange().getStart().getCharacter(),
168+
data.getRange().getEnd().getLine(),
169+
data.getRange().getEnd().getCharacter(),
170+
data.getType()
171+
);
172+
} catch (IllegalArgumentException e) {
173+
LOGGER.error("Unable to highlight file {}", inputFile, e);
174+
}
175+
});
170176

171177
highlighting.save();
172178
}

src/test/java/com/github/_1c_syntax/bsl/sonar/BSLHighlighterTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.stream.IntStream;
4646

4747
import static org.assertj.core.api.Assertions.assertThat;
48+
import static org.assertj.core.api.Assertions.assertThatNoException;
4849
import static org.mockito.Mockito.mock;
4950
import static org.mockito.Mockito.when;
5051

@@ -130,6 +131,29 @@ void testMergeHighlightingTokens() {
130131

131132
}
132133

134+
@Test
135+
void testSaveHighlightingWithInvalidTokenPosition() {
136+
// given
137+
context = SensorContextTester.create(Path.of("."));
138+
highlighter = new BSLHighlighter(context);
139+
documentContext = mock(DocumentContext.class);
140+
141+
// Create a token with position exceeding line length
142+
var token = new CommonToken(BSLLexer.IF_KEYWORD, "Если");
143+
token.setLine(1);
144+
token.setCharPositionInLine(20);
145+
146+
when(documentContext.getTokens()).thenReturn(List.of(token));
147+
148+
// Create InputFile with short content (line has less than 20 characters)
149+
inputFile = Tools.inputFileBSL(FILE_NAME, BASE_DIR, "А = 1;");
150+
151+
// when/then - should not throw
152+
assertThatNoException().isThrownBy(() ->
153+
highlighter.saveHighlighting(inputFile, documentContext)
154+
);
155+
}
156+
133157
private void testHighlighting(Vocabulary vocabulary, Map<String, TypeOfText> highlightingMap) {
134158
// given
135159
initContext(vocabulary);

0 commit comments

Comments
 (0)