Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

intellij plugin - PsiReferenceContributor not catching element

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...