Can't figure out why I have two instances of annotator.

Answered

Hi!

I'm working on a plugin and part of functionality is dependency highlighting in .gradle files (I want to recognize the dependency decription as a text, I mean the ‘org.junit:junit-core:5.11’ part of the dependency definition).

I have an annotator which logs the text of the dependency if it's a potential candidate for highlighting and it logs each dependency twice. I found during debugging that for some reason there are two instances of my annotator.

Relevant parts of the code:

public class GradleDependencyAnnotator implements Annotator {

    private final ScopeArgumentResolver scopeArgumentResolver;

    // TODO Remove and replace with DI
    public GradleDependencyAnnotator() {
        this(new ElementTypeScopeArgumentResolver());
    }

    public GradleDependencyAnnotator(ScopeArgumentResolver scopeArgumentResolver) {
        this.scopeArgumentResolver = scopeArgumentResolver;
    }

    @Override
    public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
        if (holder.isBatchMode()) {
            return;
        }

        final PsiFile containingFile = element.getContainingFile();
        if (containingFile.getName().endsWith(".gradle")) {

            if (element instanceof LeafPsiElement lpe) {
                if (scopeArgumentResolver.resolve(lpe)) {
                    System.out.printf("%s at %d%n", lpe.getText(), System.currentTimeMillis());
                }
            }
        }
    }
}
<extensions defaultExtensionNs="com.intellij">
        <annotator language="Groovy" implementationClass="io.github.vcvitaly.depopener.annotator.GradleDependencyAnnotator"/>
    </extensions>

Could you help me please? I've been debugging it for a while and can't figure out why there are 2 of them.

0
2 comments

Hi Vitaly,

As far as I can see, annotators can actually be created many times for a file, probably as a result of highlighting restart caused by some events. Could you please clarify what problems it causes for you? Are there two annotators running constantly in each case, even after opening a file and editing it?

If there are no side effects, I suggest ignoring this.

0

Hi Karol! Thanks for a quick reply.
It didn't cause me any real issues, but I was wondering if it's wrong and might cause me issues going forward and that's why I created this question. I'll ignore it since I didn't find any issues so far. Thanks!

0

Please sign in to leave a comment.