How to project compiler output onto editor

Answered

I have a language plugin and I have an action to run the compiler, And what I want to do is to take the compiler output and highlight those parts of code that are considered bad by compiler.

As far as I know there are ```Annotator``` and ``` ExternalAnnotator```, however they seem to be executed automatically by IntelliJ once Psi Tree has been changed and do not have any methods to handle events or to be executed from another parts of code.

Is there a way to subscribe for compiler messages and call something like ```Annotator``` from listener. If so where and when should I subscribe and what API to call, because the only way I interract with IntelliJ is through xml configuration, so the sequence of particular method calls is very vague.

Thanks!

5 comments
Comment actions Permalink

See com.intellij.openapi.compiler.CompilationStatusListener/com.intellij.openapi.compiler.CompilerTopics#COMPILATION_STATUS if using builtin Compiler only.

See com.intellij.task.ProjectTaskListener/com.intellij.task.ProjectTaskListener#TOPIC if you need to work with external build system additionally.

You can then force re-highlighting relevant files using com.intellij.codeInsight.daemon.DaemonCodeAnalyzer#restart(com.intellij.psi.PsiFile)

0
Comment actions Permalink

I managed to register a listener for my compiler events. But I still missing the way to highlight e.g. erroneous line.

I dont get the way com.intellij.codeInsight.daemon.DaemonCodeAnalyzer#restart(com.intellij.psi.PsiFile) works since I don't know what methods are called to rehighlight the file.

In the event from a compiler I get a message and line and column information, and I tried the following code to highlight an element but it doesn't work

```

ApplicationManager.getApplication().runReadAction {
val virtFile = VfsUtil.findFile(File("/home/gerwant/IdeaProjects/test/src/com/company/test.vy").toPath(), false)
val psiFile = PsiManager.getInstance(project).findFile(virtFile!!)

AnnotationHolderImpl(AnnotationSession(psiFile!!)).createErrorAnnotation(psiFile.firstChild.nextSibling.firstChild,
"Compilaton Error")
}

```

0
Comment actions Permalink

restart() will simply invoke highlighting for all (or given) files, so your inspections/annotator will run again.

0
Comment actions Permalink

Thanks!  But then how should I supply my compiler output to Annotator? Since as far as I know Annotators get called on each psi element with predefined AnnotationHolder.

I can supply each PsiElement with UserHolderData that stores needed information from the compiler, but may be there is more simple way?

0
Comment actions Permalink

Annotator will run on all source files with PSI in your project automatically.

0

Please sign in to leave a comment.