How to trigger InlayHintsCollector to render inlay hints?
Answered
I was develop a jetbrains plugin.
And it show some inlay hints by InlayHintsProviderFactory
,InlayHintsProvider
and InlayHintsCollector
。
When user change my plugin settings, I want to trigger render inlay hints for the editor that user has opened.
How to do that?
Now I've tried a few things, but is not work:
// try by codeAnalyzer
// FileEditorManager.getInstance(project).getOpenFiles();
// (virtualFile: The file that the user has opened in editor)
public static void tryTriggerVFCodeAnalyzerDone(final Project project, final VirtualFile virtualFile) {
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
final PsiManager psiManager = PsiManager.getInstance(project);
final DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(project);
final PsiFile psiFile = psiManager.findFile(virtualFile);
if (psiFile != null) {
// trigger
codeAnalyzer.restart(psiFile);
}
}
// try by TextEditorHighlightingPass doCollectInformation. Refer to InlayHintsPass
public static void tryTriggerVFCodeAnalyzerDone(final Project project, final VirtualFile virtualFile) {
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
final PsiManager psiManager = PsiManager.getInstance(project);
final PsiFile psiFile = psiManager.findFile(virtualFile);
if (psiFile != null) {
Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
DaemonProgressIndicator progress = new DaemonProgressIndicator();
ApplicationManager.getApplication().executeOnPooledThread(() -> {
ProgressManager.getInstance().runProcess(() -> {
ApplicationManager.getApplication().runReadAction(() -> {
HighlightingSessionImpl.runInsideHighlightingSession(psiFile
, editor.getColorsScheme()
, new ProperTextRange(0, document.getTextLength())
, false, session -> {
List<TextEditorHighlightingPass> mainPasses = TextEditorHighlightingPassRegistrarEx.getInstanceEx(project)
.instantiateMainPasses(psiFile, document, HighlightInfoProcessor.getEmpty());
JobLauncher.getInstance().invokeConcurrentlyUnderProgress(
mainPasses
, progress
, pass -> ReadAction.compute(() -> {
pass.doCollectInformation(progress);
return true;
}));
});
});
}, progress);
});
} else {
log.info("[INLAY_HINTS] tryTriggerVFCodeAnalyzer, ignore, psiFile is null, virtualFile:" + virtualFile.getPath());
}
}
Please sign in to leave a comment.
Unfortunately, there is currently no clean solution, please see https://youtrack.jetbrains.com/issue/IJPL-29622/Breaking-change-with-InlayHintsPassFactory-class-moved-in-another-package
thanks