diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java index a257a8cfa..3b7d8f2de 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java @@ -163,7 +163,7 @@ public static Document getDocument(VirtualFile docFile) { return null; } - public static int toOffset(Position start, Document document) { + public static int toOffset(Position start, Document document) throws IndexOutOfBoundsException { int lineStartOffset = document.getLineStartOffset(start.getLine()); return lineStartOffset + start.getCharacter(); } @@ -202,13 +202,19 @@ public static Range toRange(TextRange range, Document document) { * @return the IJ {@link TextRange} from the given LSP range and null otherwise. */ public static @Nullable TextRange toTextRange(Range range, Document document) { - final int start = LSPIJUtils.toOffset(range.getStart(), document); - final int end = LSPIJUtils.toOffset(range.getEnd(), document); - if (start >= end || end > document.getTextLength()) { + try { + final int start = LSPIJUtils.toOffset(range.getStart(), document); + final int end = LSPIJUtils.toOffset(range.getEnd(), document); + if (start >= end || end > document.getTextLength()) { + // Language server reports invalid diagnostic, ignore it. + return null; + } + return new TextRange(start, end); + } catch (IndexOutOfBoundsException e) { // Language server reports invalid diagnostic, ignore it. + LOGGER.warn("Invalid LSP text range", e); return null; } - return new TextRange(start, end); } public static Location toLocation(PsiElement psiMember) {