Skip to content

Commit

Permalink
Fixes NPE for completions without textEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophKaser authored and Christoph Kaser committed Oct 13, 2023
1 parent c846b97 commit 10f9173
Showing 1 changed file with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,25 @@ private String getVariableValue(String variableName) {
case TM_FILENAME -> LSPEclipseUtils.toPath(document).lastSegment();
case TM_FILEPATH -> getAbsoluteLocation(LSPEclipseUtils.toPath(document));
case TM_DIRECTORY -> getAbsoluteLocation(LSPEclipseUtils.toPath(document).removeLastSegments(1));
case TM_LINE_INDEX -> Integer.toString(getTextEditRange().getStart().getLine()); // TODO probably wrong, should use viewer state
case TM_LINE_NUMBER -> Integer.toString(getTextEditRange().getStart().getLine() + 1); // TODO probably wrong, should use viewer state
case TM_LINE_INDEX -> {
try {
yield Integer.toString(getTextEditRange().getStart().getLine()); // TODO probably wrong, should use viewer state
} catch (BadLocationException e) {
LanguageServerPlugin.logWarning(e.getMessage(), e);
yield ""; //$NON-NLS-1$
}
}
case TM_LINE_NUMBER -> {
try {
yield Integer.toString(getTextEditRange().getStart().getLine() + 1); // TODO probably wrong, should use viewer state
} catch (BadLocationException e) {
LanguageServerPlugin.logWarning(e.getMessage(), e);
yield ""; //$NON-NLS-1$
}
}
case TM_CURRENT_LINE -> { // TODO probably wrong, should use viewer state
int currentLineIndex = getTextEditRange().getStart().getLine();
try {
int currentLineIndex = getTextEditRange().getStart().getLine();
IRegion lineInformation = document.getLineInformation(currentLineIndex);
String line = document.get(lineInformation.getOffset(), lineInformation.getLength());
yield line;
Expand Down Expand Up @@ -641,8 +655,15 @@ private String getVariableValue(String variableName) {
};
}

private Range getTextEditRange() {
return item.getTextEdit().map(TextEdit::getRange, InsertReplaceEdit::getInsert);
private Range getTextEditRange() throws BadLocationException {
Either<TextEdit, InsertReplaceEdit> textEdit = item.getTextEdit();
if (textEdit != null) {
return textEdit.map(TextEdit::getRange, InsertReplaceEdit::getInsert);
} else {
Position start = LSPEclipseUtils.toPosition(this.bestOffset, document);
Position end = LSPEclipseUtils.toPosition(initialOffset, document);
return new Range(start, end);
}
}

/**
Expand Down

0 comments on commit 10f9173

Please sign in to comment.