Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a simple test class:

public class Test {    
    public void foo() {
        Object a = getClass();
    }
}

Playing with the DefinitionsReferenceContributor I expect it to get the reference, but nothing happens. Breakpoint inside getReferencesByElement is not reached.

public class DefinitionsReferenceContributor extends PsiReferenceContributor {


    @Override
    public void registerReferenceProviders(PsiReferenceRegistrar registrar) {

        PsiElementPattern.Capture<PsiMethodCallExpression> psiJavaTokenCapture = psiElement(PsiMethodCallExpression.class);

        registrar.registerReferenceProvider(psiJavaTokenCapture, new PsiReferenceProvider() {
            @NotNull
            @Override
            public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
                
                return new PsiReference[0];
            }
        });
    }
}

Changes to plugin.xml:

  <depends>com.intellij.modules.platform</depends>
  <depends>com.intellij.java</depends>

  <extensions defaultExtensionNs="com.intellij">
    <psi.referenceContributor implementation="DefinitionsReferenceContributor"/>
  </extensions>

What am I doing wrong?

question from:https://stackoverflow.com/questions/65870661/psireferencecontributor-not-catching-element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

I was able to solve it this way:

    PsiElementPattern.Capture<PsiLiteralExpression> psiLiteralExpressionCapture = PlatformPatterns.psiElement(PsiLiteralExpression.class);
    registrar.registerReferenceProvider(psiLiteralExpressionCapture, psiReferenceProvider, PsiReferenceRegistrar.HIGHER_PRIORITY);

This fires the

public PsiReference[] getReferencesByElement(PsiElement psiElement, ProcessingContext processingContext)

Method where i can process the element according to my needs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...