From 28c405faa991eb8c20b641867ae06a8dc376f7ed Mon Sep 17 00:00:00 2001 From: azerr Date: Tue, 12 Sep 2023 14:54:38 +0200 Subject: [PATCH] fix: NPE in LSPIJUtils.editorForElement Fixes #1156 Signed-off-by: azerr --- .../devtools/intellij/lsp4ij/LSPIJUtils.java | 4 +-- .../LSPDocumentationProvider.java | 29 ++++++++++++++----- 2 files changed, 24 insertions(+), 9 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 f3edb724c..ee59687d8 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java @@ -428,8 +428,8 @@ public static Editor editorForFile(VirtualFile file) { return editors.length > 0 ? editors[0] : null; } - public static Editor editorForElement(PsiElement element) { - if (element.getContainingFile() != null && element.getContainingFile().getVirtualFile() != null) { + public static Editor editorForElement(@Nullable PsiElement element) { + if (element != null && element.getContainingFile() != null && element.getContainingFile().getVirtualFile() != null) { return editorForFile(element.getContainingFile().getVirtualFile()); } return null; diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentation/LSPDocumentationProvider.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentation/LSPDocumentationProvider.java index ffb9804c1..1aba08130 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentation/LSPDocumentationProvider.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentation/LSPDocumentationProvider.java @@ -66,21 +66,36 @@ public String getQuickNavigateInfo(PsiElement element, PsiElement originalElemen @Nullable @Override - public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) { + public String generateDoc(@NotNull PsiElement element, @Nullable PsiElement originalElement) { try { Project project = element.getProject(); if (project.isDisposed()) { return null; } - Editor editor = LSPIJUtils.editorForElement(element); - if (editor == null) { - return null; + Editor editor = null; + List markupContent = null; + if (element instanceof LSPPsiElementForLookupItem) { + // Show documentation for a given completion item in the "documentation popup" (see IJ Completion setting) + // (LSP textDocument/completion request) + editor = LSPIJUtils.editorForElement(element); + markupContent = ((LSPPsiElementForLookupItem) element).getDocumentation(); + } else { + // Show documentation for a hovered element (LSP textDocument/hover request). + if (originalElement == null) { + return null; + } + editor = LSPIJUtils.editorForElement(originalElement); + VirtualFile file = originalElement.getContainingFile().getVirtualFile(); + if (LSPVirtualFileWrapper.hasWrapper(file)) { + int targetOffset = getTargetOffset(originalElement); + markupContent = LSPVirtualFileWrapper.getLSPVirtualFileWrapper(file).getHoverContent(originalElement, targetOffset, editor); + } } - List result = getMarkupContents(element, originalElement); - if (result == null || result.isEmpty()) { + + if (editor == null || markupContent == null || markupContent.isEmpty()) { return null; } - String s = result + String s = markupContent .stream() .map(m -> m.getValue()) .collect(Collectors.joining("\n\n"));