Skip to content

Commit

Permalink
fix: Support null file in LSPHighlightUsagesHandlerFactory
Browse files Browse the repository at this point in the history
Fixes #1197

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Oct 3, 2023
1 parent 3757228 commit 152d70f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
/**
* LSP textDocument/hover support for a given file.
*/
public class LSPTextHoverForFile implements Disposable {
public class LSPTextHoverForFile implements Disposable {

private static final Logger LOGGER = LoggerFactory.getLogger(LSPTextHoverForFile.class);
private PsiElement lastElement;
Expand All @@ -49,7 +49,7 @@ public class LSPTextHoverForFile implements Disposable {

public LSPTextHoverForFile(Editor editor) {
if (editor instanceof EditorImpl) {
Disposer.register(((EditorImpl)editor).getDisposable(), this);
Disposer.register(((EditorImpl) editor).getDisposable(), this);
}
}

Expand Down Expand Up @@ -91,10 +91,14 @@ private void initiateHoverRequest(PsiElement element, int offset) {
// The previous LSP hover request is not finished,cancel it
this.previousCancellationSupport.cancel();
}
PsiDocumentManager manager = PsiDocumentManager.getInstance(element.getProject());
PsiFile psiFile = element.getContainingFile();
VirtualFile file = LSPIJUtils.getFile(element);
final Document document = manager.getDocument(psiFile);
if (file == null) {
return;
}
final Document document = LSPIJUtils.getDocument(file);
if (document == null) {
return;
}
if (offset != -1 && (this.lspRequest == null || !element.equals(this.lastElement) || offset != this.lastOffset)) {
this.lastElement = element;
this.lastOffset = offset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils;
import com.redhat.devtools.intellij.lsp4ij.LanguageServiceAccessor;
Expand Down Expand Up @@ -50,17 +51,18 @@ public class LSPHighlightUsagesHandlerFactory implements HighlightUsagesHandlerF
}

private List<LSPHighlightPsiElement> getTargets(Editor editor, PsiFile psiFile) {
VirtualFile file = LSPIJUtils.getFile(psiFile);
if (file == null) {
return Collections.emptyList();
}
URI uri = LSPIJUtils.toUri(file);
List<LSPHighlightPsiElement> elements = new ArrayList<>();
final CancellationSupport cancellationSupport = new CancellationSupport();
try {
int offset = TargetElementUtil.adjustOffset(psiFile, editor.getDocument(), editor.getCaretModel().getOffset());
Document document = editor.getDocument();
Position position;
position = LSPIJUtils.toPosition(offset, document);
URI uri = LSPIJUtils.toUri(document);
if (uri == null) {
return Collections.emptyList();
}
Position position = LSPIJUtils.toPosition(offset, document);

ProgressManager.checkCanceled();
TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri.toString());
DocumentHighlightParams params = new DocumentHighlightParams(identifier, position);
Expand All @@ -77,14 +79,14 @@ private List<LSPHighlightPsiElement> getTargets(Editor editor, PsiFile psiFile)
}
})).toArray(CompletableFuture[]::new))));
while (!future.isDone() || !highlights.isEmpty()) {
ProgressManager.checkCanceled();
DocumentHighlight highlight = highlights.poll(25, TimeUnit.MILLISECONDS);
if (highlight != null) {
TextRange textRange = LSPIJUtils.toTextRange(highlight.getRange(), document);
if (textRange != null) {
elements.add(new LSPHighlightPsiElement(textRange, highlight.getKind()));
}
ProgressManager.checkCanceled();
DocumentHighlight highlight = highlights.poll(25, TimeUnit.MILLISECONDS);
if (highlight != null) {
TextRange textRange = LSPIJUtils.toTextRange(highlight.getRange(), document);
if (textRange != null) {
elements.add(new LSPHighlightPsiElement(textRange, highlight.getKind()));
}
}
}
} catch (ProcessCanceledException cancellation) {
cancellationSupport.cancel();
Expand Down

0 comments on commit 152d70f

Please sign in to comment.