How to trigger InlayHintsCollector to render inlay hints?

已回答

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());
        }
    }
0

请先登录再写评论。