Navigate to file in another module by clicking on java String

Answered

Hello, I am working on simple navigation functionality. My code looks like this.

public class TestClass extends BaseClass {
public TestClass() {
super("folder/File.txt");
}
}

I have this type of java file that extends BaseClass. In the constructor, I have a String that basically a path to a txt file in another module. I want to add a reference to this String, so after clicking Ctrl+Click, I will navigate to the txt file

0
5 comments

Hello again, I tried following the steps described in the links above, but I have the same issue as the guy from the https://intellij-support.jetbrains.com/hc/en-us/community/posts/360004129840-PsiFileReference-for-custom-java-literal thread. 

Do I need to create something else except a class that derives from PsiReferenceContributor? I believe that I just miss something.

public class ControlFileReferenceContributor extends PsiReferenceContributor {
@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {

registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression.class),
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element,
@NotNull ProcessingContext
context) {
PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
String value = literalExpression.getValue() instanceof String ?
(String) literalExpression.getValue() : null;
if (value != null && value.endsWith(".lst")) {
return new FileReferenceSet(value, element, ElementManipulators.getOffsetInElement(element), this, false).getAllReferences();
}
return PsiReference.EMPTY_ARRAY;
}
});
}
}
0

Hi,

I don't see what the issue can be. Could you please share the code and steps to reproduce+test project?

0

Hi, this is a plugin code https://github.com/DenysKisliak/PluginAA/tree/dkisliak/reference_sampe

And this is an example project https://github.com/DenysKisliak/sample 

Steps to reproduce:

1. Navigate to JavaModule/src/main/java/org/example/clientOne/JavaMigrationForClientOne.java

2. Ctrl + R Click on "clientOne/ControlFilesWithAllArtifacts.lst" string in the constructor

3. User should be navigated to ArtifactsModule/src/main/resources/schema/clientOne/ControlFilesWithAllArtifacts.lst

Other errors that may occur because the structure of the sample file differs from the target project, please ignore them

0

Hi,

FileReferenceSet works by finding files in the same directory as the Java file containing the reference. You can verify it by adding e.g., test/mytest.lst file next to JavaMigrationForClientOne.java file and changing the code to super("test/mytest.lst"). Reference will work correctly.

To make it work for the case you need, I suggest extending FileReferenceSet and overriding computeDefaultContexts() to return directories you expect your references in (e.g. .../sample/test project/ArtifactsModule/src/main/resources/schema if I understand your project structure correctly).

1

Please sign in to leave a comment.