Navigate to file in another module by clicking on java String
已回答
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
请先登录再写评论。
Hi,
Please see:
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;
}
});
}
}
Hi,
I don't see what the issue can be. Could you please share the code and steps to reproduce+test project?
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
Hi,
FileReferenceSetworks by finding files in the same directory as the Java file containing the reference. You can verify it by adding e.g.,test/mytest.lstfile next toJavaMigrationForClientOne.javafile and changing the code tosuper("test/mytest.lst"). Reference will work correctly.To make it work for the case you need, I suggest extending
FileReferenceSetand overridingcomputeDefaultContexts()to return directories you expect your references in (e.g..../sample/test project/ArtifactsModule/src/main/resources/schemaif I understand your project structure correctly).