From a2a63b06a0c9bb020d74f193fd0b69ab48e2ec53 Mon Sep 17 00:00:00 2001 From: azerr Date: Wed, 23 Aug 2023 12:41:46 +0200 Subject: [PATCH] fix: java.lang.IndexOutOfBoundsException after deleting text when editing application.properties Fixes #1116 Signed-off-by: azerr --- .../devtools/intellij/lsp4ij/LSPIJUtils.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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) {