From 152d70fefe0ef775742afc7ff482e891ad6f95c3 Mon Sep 17 00:00:00 2001 From: azerr Date: Tue, 3 Oct 2023 20:47:20 +0200 Subject: [PATCH] fix: Support null file in LSPHighlightUsagesHandlerFactory Fixes #1197 Signed-off-by: azerr --- .../documentation/LSPTextHoverForFile.java | 14 ++++++---- .../LSPHighlightUsagesHandlerFactory.java | 28 ++++++++++--------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentation/LSPTextHoverForFile.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentation/LSPTextHoverForFile.java index 7fab76466..6b8b812be 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentation/LSPTextHoverForFile.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentation/LSPTextHoverForFile.java @@ -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; @@ -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); } } @@ -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; diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java index 829a29869..13a46f75a 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java @@ -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; @@ -50,17 +51,18 @@ public class LSPHighlightUsagesHandlerFactory implements HighlightUsagesHandlerF } private List getTargets(Editor editor, PsiFile psiFile) { + VirtualFile file = LSPIJUtils.getFile(psiFile); + if (file == null) { + return Collections.emptyList(); + } + URI uri = LSPIJUtils.toUri(file); List 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); @@ -77,14 +79,14 @@ private List 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();