Programmaticlly insert Error Annotation into Document Follow
What's the best way to add an error annotation to a Document? I need to do this for a custom plugin I'm writing. I found a method: UpdateHighlightersUtil.setHighlightersToEditor that sort of works, but I was wondering if there's a better way.
Sample code:
val annot = new Annotation(20, 30, HighlightSeverity.ERROR, "Test!", "Test Message!")
val info = HighlightInfo.fromAnnotation(annot)
val al = new ArrayList[HighlightInfo]()
al.add(info)
UpdateHighlightersUtil.setHighlightersToEditor(project, doc, 20, 30, al, null, 0)
Jamie
Please sign in to leave a comment.
Hello Jamie,
Most likely the correct solution is to add the highlighting on the PSI, not
document, level by means of writing an inspection or an annotator for your
custom language. I'll probably be able to give you better advice if you describe
what problem you're trying to solve.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Hi Dmitry,
The problem is to modify the scala environment (through the sbt plugin) to use a different/complimentary syntax checker to the scala plugin's inspection pass.
What I was trying to do was parse the output from SBT (simple-build-tool, a scala build system) and capture that output. From that I would then update the document with the captured errors.
I had seen the more straight-foward PSI tree error checking, and one of my possible solutions might be to wait on the sbt process' finishing, then pass in all the errors through an inspector. This might add some complexity to my code, in exchange for possibly using the more standard way of showing errors in IDEA? Any light on the subject would be welcome.
Jamie
Hello Jamie,
The cleanest solution for this problem is to implement the ExternalAnnotator
interface, and to create annotations for all problem reported by SBT for
the current file. This will ensure that your annotations won't be randomly
overwritten by IDEA's own error highlighting, or vice versa.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Thanks Dmitry, I guess that answers my question. I was hoping there was a simple way of just tossing in some random errors, but I guess a little bit more work doesn't hurt.
Hey Dmitri,
I was trying the method you suggested, and was able to pass in the annotations. I am having a problem now with the timing. The ExternalAnnotator doesn't know when the compile process starts, because the user runs it in a console window. I can detect when it finishes, and was wondering, is there a way I can trigger a rescan of the PsiFile? That is, is there a way to force a refresh of the PsiTree somehow so the ExternalAnnotator gets triggered? I tried a few things but didn't have any success.
My other option would be to start a new invisible SBT process in the background somehow.
Thanks,
Jamie
Hello Jamie,
You can use DaemonCodeAnalyzer.restart(PsiFile) to re-run the inspections
and the external annotator for a file.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Thanks Dmitry, worked like a charm. Now on to hacking sbt!
Jamie