Programmaticlly insert Error Annotation into Document

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
7 comments
Comment actions Permalink

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.

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

---
Original message URL:
http://devnet.jetbrains.net/message/5281996#5281996


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

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

0
Comment actions Permalink

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.

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.


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

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.

0
Comment actions Permalink

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

0
Comment actions Permalink

Hello Jamie,

You can use DaemonCodeAnalyzer.restart(PsiFile) to re-run the inspections
and the external annotator for a file.

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.


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


1
Comment actions Permalink

Thanks Dmitry, worked like a charm. Now on to hacking sbt!

Jamie

0

Please sign in to leave a comment.