Skip to content

Commit

Permalink
fix: NPE in LSPIJUtils.editorForElement
Browse files Browse the repository at this point in the history
Fixes #1156

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr authored and fbricon committed Sep 12, 2023
1 parent 349e806 commit a3f4add
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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<MarkupContent> 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"));
Expand Down

0 comments on commit a3f4add

Please sign in to comment.