Skip to content

Commit

Permalink
fix: java.lang.IndexOutOfBoundsException after deleting text when
Browse files Browse the repository at this point in the history
editing application.properties

Fixes #1116

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Aug 23, 2023
1 parent 2cde322 commit a2a63b0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a2a63b0

Please sign in to comment.