Custom annotations constantly showing and disappearing

已回答

Hello, I have implemented given class:

class DuplicateAnnotator extends PsiElementVisitor implements Annotator

Inside there is given annotate() method:

@Override
public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder holder) {
    try {
        myHolder = holder;
        psiElement.accept(this);
    } finally {
        myHolder = null;
    }
}

I have also added given code inside visitFile() method:

myHolder.newAnnotation(HighlightSeverity.WARNING,
                featureFile.getName() + ": lines " + firstLine + "-" + lastLine)
        .range(steps.get(integerStringEntry.getKey()))
        .create();

The problem is that my annotations are constantly disappearing and showing again. Is there anything I am missing in my code?

Edit: I also added flag that disallows visiting the same file multiple times. In that case annotations appear and then disappear so it does not resolve my problem.

0

Hi Patryk,

First, a common implementation of an Annotator checks whether a passed element is of the type you want to annotate and does the job without an additional visitor. So in your case:

class DuplicateAnnotator implements Annotator {
    @Override
    public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder holder) {
        if (psiElement instanceof PsiFile) {
            ...
            myHolder.newAnnotation(HighlightSeverity.WARNING,
                    featureFile.getName() + ": lines " + firstLine + "-" + lastLine)
            .range(steps.get(integerStringEntry.getKey()))
            .create();
        }
    }
}

In your case, you accept the visitor for each psiElement passed to the annotator, which executes visitor methods unnecessarily (default implementations, which are cheap, but still).

Also, annotators should work on the lower possible element level and PsiFile is the root. Annotating high-level elements may indeed cause blinking. Is it possible that you work on lower-level elements?

0

请先登录再写评论。