Problems to add PsiReferenceContributor

Hallo,
I'm trying to create a custom language. I now wanted to create a ReferenceContributor.
This is the code of the ReferenceContributor:

public class SOLReferenceContributor extends PsiReferenceContributor {     @Override     public void registerReferenceProviders(PsiReferenceRegistrar registrar) {         registrar.registerReferenceProvider(PlatformPatterns.psiElement(SOLFeatureValue.class),                 new PsiReferenceProvider() {                     @NotNull                     @Override                     public PsiReference[] getReferencesByElement(@NotNull PsiElement element,                             @NotNull ProcessingContext context) {                         PsiLiteralExpression literalExpression = (PsiLiteralExpression)element;                         String text = (String)literalExpression.getValue();                         if (text != null && text.startsWith("ref:")) {                             return new PsiReference[] {new SOLReference(element, new TextRange(8, text.length() + 1))};                         }                         return new PsiReference[0];                     }                 });     } }


I added an extension point in plugin.xml:

<psi.referenceContributor implementation="com.sol.SOLReferenceContributor"/>


I set a breakpoint in the registerReferenceProviders-Method. However when I debug the plugin it never stops so I assume the contributor is not registered correctly. Thanks for any help.

6 comments
Comment actions Permalink

Your reference contributor is from SOLFeatureValue.class. The Javadoc for PsiReferenceContributor states:

Note that, if you're implementing a custom language, it won't by default support references registered through PsiReferenceContributor. If you want to support that, you need to call ReferenceProvidersRegistry#getReferencesFromProviders(PsiElement) from your implementation of PsiElement.getReferences().


Are you doing that in getReferences() in SOLFeatureValue?

0
Comment actions Permalink

Thank you for the help. Now the registerReferenceProviders-Method is executed. I added the following snippet to SOLFeatureValueImpl:

    @NotNull     @Override     public PsiReference[] getReferences() {         return ReferenceProvidersRegistry.getReferencesFromProviders(this);     }


There remain problems. However I can go on now thank you.

0
Comment actions Permalink

No problem. Don't forget to also add the reference from the SOLFeatureValue itself, if any. See for example com.intellij.psi.impl.source.xml.XmlDoctypeImpl#getReferences.

0
Comment actions Permalink

Hallo,
I now changed the ReferenceContributor to the following:

public class SOLReferenceContributor extends PsiReferenceContributor {     @Override     public void registerReferenceProviders(PsiReferenceRegistrar registrar) {         registrar.registerReferenceProvider(PlatformPatterns.psiElement(SOLTypes.FEATURE_VALUE_NAME),                 new PsiReferenceProvider() {                     @NotNull                     @Override                     public PsiReference[] getReferencesByElement(@NotNull PsiElement element,                             @NotNull ProcessingContext context) {                         String text = element.getText();                         return new PsiReference[] {new SOLReference(element, new TextRange(0, text.length()))};                     }                 });     } }


Because FEATURE_VALUE_NAME is a leaf I no longer have a class implementing PsiElement, So I can't override getReference. Now the registerReferenceProviders is not called anymore.

Here is a snippet of my language:
PROPERTY_NAME {
    ## the FEATURE_VALUE_DEFINITION element
    AN_IDENTIFIER {
        en {
            a term
            another term
            one more term
        }
    }
    ## the FEATURE_VALUE_NAME element
    AN_IDENTIFIER
}

The second AN_IDENTIFER is the FEATURE_VALUE_NAME that should reference the FEATURE_VALUE_DEFINITION.
Thanks for any help,
regards Sebastian

0
Comment actions Permalink

Ah! I think this should be added to the sdk doc. This post has saved my life.

2
Comment actions Permalink

I have debugged this issue for more than 1 week. This post has saved my life, too.

0

Please sign in to leave a comment.