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: Support null file in LSPHighlightUsagesHandlerFactory #1198

Merged
merged 1 commit into from
Oct 3, 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 @@ -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
Loading