Enumerate annotations on PsiElement

I can't figure out how to enumerate the kinds of annotations applied to a PsiElement in a Java file. Specifically I'd like to inspect for non-syntax errors like `Cannot resolve symbol`, messages relating to incorrect method parameters, etc.

I've had success detecting syntax errors (missing parenthesis, double quote, semicolon, etc.) by detecting an instanceof com.intellij.psi.PsiErrorElement, but the other Java language-specific error annotations have eluded my grasp thusfar!

Please help point me in the right direction. Thanks so much!

6 comments

these "annotations" are called RangeHighlighters , are applied highly asynchronously and can be retrieved by e.g.

`

MarkupModel model = DocumentMarkupModel.forDocument(document, project, false);
RangeHighlighter[] highlighters = model.getAllHighlighters();
`
when the highlighting completed.
0

This is great information! I found out a few things: It turns out that there's a catch... The

RangeHighlighter[] highlighters = model.getAllHighlighters();

inspection can only happen in the event dispatch thread. I can fire off a runner which will grab all the RangeHighlighters. But the RangeHighlighters only have TextRanges to indicate which PsiElement they are attached to. Matching TextRanges isn't too hard, but I need access to those RangeHighlighters as they relate to the PsiElements in my implementation of com.intellij.codeInsight.daemon.LineMarkerProvider#getLineMarkerInfo ... The only sensible thing I could think of would be to maintain a list in-memory of all RangeHighlighters in all open documents, and keep it updated on its own schedule. I would have look to that list (which could get massive) in getLineMarkerInfo. That seems like a really bad idea.

Is there another way to take a PsiElement and query the MarkupModel in the current thread for its markup state at that very moment?

0

Unfortunately no. MarkupModel is inherently UI-specific and should be accessed from EDT only.

0

Understood.

Can you suggest a different design pattern than the one I'm suggesting for tracking the RangeHighlighters for all open documents?

Where are the RangeHighlighters applied? Could I create a listener and hook it in to be notified when the RangeHighlighters are added or removed? That would be so much more efficient.

0

- That depends on what exactly do you want to achieve.

- RangeHighlighters created in EDT some time after the highlighting is completed.

- Yes, but that, being an advanced magic, requires some classes from Restricted Section of Hogwarts library.

<code>

MarkupModelEx modelEx = (MarkupModelEx)DocumentMarkupModel.forDocument(getDocument(getFile()), getProject(), true);
modelEx.addMarkupModelListener(disposable, listener);
</code>

where disposable is when your listener should be removed.

0

You're the wizard! Thanks so much. I'm pretty sure this is perfect.

I'm going to roll up my sleeves and get to work.

0

Please sign in to leave a comment.