PsiReferenceContributor is not working

Answered

When attempting to use the "Go to Declaration" feature, the expected behavior of navigating to the declaration of a variable or function is not occurring as intended. Th Config Object contains the PsiElement and the PsiFile where the Element is declared but somehwo if a hold Strg and get over the text. i get the message “Can not find Declaration to go to”.

Can someone maybe help me with this?

This is my code:

public class ConfigReferenceContributor extends PsiReferenceContributor {
    @Override
    public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
        registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiElement.class), new ConfigReferenceProvider());
    }
}

 

public class ConfigReference extends PsiReferenceBase<PsiElement> {
    private final Config config;

    ConfigReference(Config config) {
        super(config.getElement(), config.getElement().getTextRange());
        this.config = config;
    }

    @Override
    public @Nullable PsiElement resolve() {
        return config.getElement();
    }
}

 

public class ConfigReferenceProvider extends PsiReferenceProvider {
    @Override
    public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
        List<PsiReference> psiReferences = new ArrayList<>();
        if (element instanceof ParameterListImpl) {
            PsiElement method = element.getParent();
            if (method instanceof FunctionReferenceImpl function) {
                PsiElement configKey = element.getFirstChild();
                if (configKey != null) {
                    iterateOverConfigFiles(element.getProject()).stream()
                            .filter(config -> configKey.getText().replace("\"", "").equals(config.getKey()))
                            .findFirst().ifPresent(result -> psiReferences.add(new ConfigReference(result)));

                }
            }
        }
        System.out.println(Arrays.toString(psiReferences.toArray(new PsiReference[0])));
        return psiReferences.toArray(new PsiReference[0]);
    }
}

 

 

 

0
1 comment

It is impossible to debug code from this information. Please see https://plugins.jetbrains.com/docs/intellij/element-patterns.html#tools-and-debugging and make sure that your references are actually installed on the correct element in the first place.

If you're still stuck, please link your full plugin's sources.

0

Please sign in to leave a comment.