How to trigger refresh on external annotator

Answered

Hi,

I'm creating a custom language plugin based on external annotator. Overall, it seems to be working fine. However, when there is a change (for example, changes in module or lib dependency) I want to "refresh" or re-trigger external annotator on opened editors. Is this possible?

Thanks!

0
5 comments

Here is the simplified code I use to reparse open files. You should add logic to only re-parse files relevant to your plugin. In my case it is all open Markdown files.

I cannot vouch for this being the correct way to do it but it does work. I am open for a better implementation.

import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.util.FileContentUtil
import com.vladsch.idea.multimarkdown.psi.MultiMarkdownFile

class ReparseOpenFiles {
    fun reparseOpenFiles(project: Project) {
        if (!project.isDisposed) {
            if (DumbService.isDumb(project)) {
                DumbService.getInstance(project).runWhenSmart {
                    reparseOpenFiles(project)
                }
            } else {
                // re-parse all open editors
                val files = FileEditorManager.getInstance(project).openFiles
                val psiManager = PsiManager.getInstance(project)
                val fileList = ArrayList<PsiFile>(files.size)

                for (file in files) {
                    val psiFile = psiManager.findFile(file)
                    if (psiFile != null && psiFile is MultiMarkdownFile) {
                        fileList.add(psiFile)
                    }
                }

                FileContentUtil.reparseFiles(fileList.map { it.virtualFile })
            }
        }
    }
}
0
Avatar
Permanently deleted user

Awesome! Thank you Vladimir!

0

See com.intellij.codeInsight.daemon.DaemonCodeAnalyzer#restart()

 

0
Avatar
Permanently deleted user

Alexandr Evstigneev, Will the `com.intellij.codeInsight.daemon.DaemonCodeAnalyzer#restart()` works for ExternalAnnotator? I tried, but doesn't seem to be working.

Vladimir Schneider Is there a thread restriction when calling FileContentUtil.reparseFiles, I tried running on UI thread as well as bg thread, nothing seem to be working.

ApplicationManager.getApplication().invokeAndWait(()-> {
FileContentUtil.reparseFiles(project, vfl, false);
});


0
Avatar
Permanently deleted user

Never mind, I figured it out. `com.intellij.codeInsight.daemon.DaemonCodeAnalyzer#restart()` is throttled restart the Annotator | ExternalAnnotator with respect to `getAutoReparseDelay`.

https://github.com/JetBrains/intellij-community/commit/75fa8aba9629d1cf5166b50e4b3051980204d8ab

After tweaking through Editor settings, it worked for me.

0

Please sign in to leave a comment.