Extending "Jump to Source" for Java



I have a project that generates java code based on some interface. I want to create a plugin that alters "Jump to source" java plugin functionality and Jump to the description file instead of the generated one. I tried to write Reference Contributor but I can't make it trigger on Java elements. Is what am I trying to achieve possible at all? If yes, please give me some guidelines how to achieve it.




Please clarify the use case.

What elements do you try to contribute references to? Are you trying to navigate to the code to declaration? Why do you use "Jump to Source" and not "Go to declaration"?


Ok. More context then. I have a code generation framework that generates java code from object description.

You can use the generated objects in regular code as they are valid java objects. However if you Ctrl+Click on the generated object, Idea navigates you to the generated object. I want to override this functionality so when ctrl+clicking on the generated object Idea to navigate you to the description instead. Is this possible?



Thanks for clarifying.

It depends on whether the elements you want to handle support reference contributors.

It's unclear what elements exactly you want to handle, but I assume variable/field at least. Let me describe the process of how you can check it.

We want to check whether a variable reference allows injecting custom references.

Let's see at the PSI tree for an example code to identify what element provides reference(s):

Let's see its getReferences() implementation (check the "class" row in the PsiViewer, it is: com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl).

It doesn't have its getReferences(), but inherits CompositePsiElement.getReferences() implementation:

public PsiReference @NotNull [] getReferences() {
return SharedPsiElementImplUtil.getReferences(this);

which effectively is:

public static PsiReference @NotNull [] getReferences(PsiElement thisElement) {
PsiReference ref = thisElement.getReference();
return ref != null ? new PsiReference[]{ref} : PsiReference.EMPTY_ARRAY;

So it returns PsiReferenceExpressionImpl's getReference() or an empty array. Let's get back to PsiReferenceExpressionImpl and see getReference():

public PsiReference getReference() {
return getReferenceNameElement() != null ? this : null;

It returns `this` or `null`, so I don't see a place where references can be injected from contributors or with another mechanism for PsiReferenceExpressionImpl.


Please sign in to leave a comment.