Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NPE in LSPIJUtils.editorForElement #1162

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading