Recognizing code patterns and jumping to a method
Answered
Hi,
i need a bump into a right direction how to archieve this:
I want to recognize specific patterns in code. We have 2 cases "getList", "getElement". Example:
List<Bucket> buckets = baseJsonRpcConnector.getList(Bucket.class,
"findBySourceIdAndBetween",
"bucket-service",
new Param("sourceId", sourceId),
);
In one of the loaded modules of the project, there is a method annotated like this:
@SpxServiceCommunicationDoc(methodName = "findBySourceIdAndBetween", description = "foo")
public void findBySourceIdAndBetween(UUID sourceId) {
....
}
So i would like "findBySourceIdAndBetween" in first block ( the call) to be highlighted and STRG-Click should jump to the annotated method in block2.
I was able to verify code and mark it if required by using:
public class CustomAnnotator implements Annotator
But I dont know how to implement STRG+Click on parts of the Method now.
I tried defining a chstom ReferenceContributor, but the getReferencesByElement() method is never fired. What am I doing wrong?
public class DefinitionsReferenceContributor extends PsiReferenceContributor {
@Override
public void registerReferenceProviders(PsiReferenceRegistrar registrar) {
PsiReferenceProvider psiReferenceProvider = new PsiReferenceProvider() {
@Override
public PsiReference[] getReferencesByElement(PsiElement psiElement, ProcessingContext processingContext) {
return new PsiReference[0];
}
};
registrar.registerReferenceProvider(PlatformPatterns. psiElement(PsiMethodCallExpression.class), psiReferenceProvider);
}
}
ReferenceContributor is defined in plugin.xml:
<psi.referenceContributor implementation="DefinitionsReferenceContributor"/>
Thanks!
Please sign in to leave a comment.
You need to provide references inside String literal ("findBySource..."), not PsiMethodCallExpression, that resolve to corresponding PsiMethod.
https://github.com/JetBrains/intellij-sdk-docs/blob/main/code_samples/simple_language_plugin/src/main/java/org/intellij/sdk/language/SimpleReferenceContributor.java is a minimal setup, discussed in https://plugins.jetbrains.com/docs/intellij/reference-contributor.html