Restarting inlay hints provider
已回答
How can I trigger the inlay hints to be computed again for any open (selected) editors?
DaemonCodeAnalyzer.getInstance(project).restart(); doesnt't work.
I also tried:
DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(project);
final FileEditor selectedEditor = FileEditorManager.getInstance(project).getSelectedEditor();
if (selectedEditor != null) {
final VirtualFile file = selectedEditor.getFile();
if (file != null) {
final PsiFile psiFile = ReadAction.compute(() -> PsiManager.getInstance(project).findFile(file));
final Document document = ReadAction.compute(() -> FileDocumentManager.getInstance().getDocument(file));
final ProgressIndicator daemonIndicator = new DaemonProgressIndicator();
ProgressManager.getInstance().runProcess(() -> {
ReadAction.run(() -> codeAnalyzer.runMainPasses(psiFile, document, daemonIndicator));
}, daemonIndicator);
codeAnalyzer.restart();
}
}
Writing anything in the open file causes the inlay hints to be recomputed but I want to trigger it programmatically.
Thanks!
请先登录再写评论。
ParameterHintsPassFactory#forceHintsUpdateOnNextPass before DaemonCodeAnalyzer#restart to force redraw
Thank you, Yann Cebron!
I'm trying to get an InlayHintProvider (subclass of com.intellij.codeInsight.hints.InlayHintsProvider) to trigger again with this method in IC 2023.1 but it doesn't seem to do anything.
Is there a new approach to force the inlay provider to re-render?
For context, the reason why I need to re-render is because my plugin's inlay provider needs to load information asynchronously (i.e. in a background thread), while collect() is called on the UI thread. Perhaps there's a better way to do this!
Make sure these calls are performed from EDT. If it still doesn't work, please share your code.
Thank you Yann Cebron. This doesn't work for me, so here's a minimal repo you can clone to reproduce the issue: https://github.com/fwouts/intellij-inlays-refresh. This is a contrived example with an InlayProvider that should show a counter that increases once per second.
Thanks for the sample. It works when using
InlayHintsPassFactory.forceHintsUpdateOnNextPass()instead ofParameterHintsPassFactory.forceHintsUpdateOnNextPass().ParameterHintsPassFactoryonly highlights inlays contributed viacom.intellij.codeInsight.hints.InlayParameterHintsProviderextension point.Thank you so much Yann Cebron, this was exactly what I needed. My plugin can finally work without blocking the main thread 🥳 Here's the source for anyone else looking for a concrete example.
FYI to anyone reading this thread after December 2023:
InlayHintsPassFactory.forceHintsUpdateOnNextPass()has been moved to an internal package and you can no longer use this. You can useDaemonCodeAnalyzer.getInstance(project).restart(psiFile)instead, although I'm not sure it's as efficient. See https://youtrack.jetbrains.com/issue/IDEA-333164 for discussion.I discovered this worked for me since I was using declaritvie Inlay Hints