Skip to content

Commit d91675f

Browse files
#66: Resolve imports relative to file's location
Part II: resolve relative import references (first part was about type references). Legend: Many projects are storing proto files in a locations that are not marked as source/resource root. As a result, imports are highlighted with error "file does not exist", and all imported types are not resolvable. Plugin can try to resolve imports relative to source file's location using following rules: 1. If `package` is not set, try to look in the same folder where source file is. 2. If package is set, try to look from parent folder. 3. Parent folder should be computed as source file's folders, with removed corresponding parts of the package. For example: `/home/user/project/foo/bar/hello.proto`: ```proto package foo.bar; import "baz/import.proto"; ``` Import should be resolved to `/home/user/project/baz/import.proto`.
1 parent 0ee75f2 commit d91675f

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/main/java/io/protostuff/jetbrains/plugin/reference/file/FilePathReferenceProvider.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element,
102102
int offset,
103103
final boolean soft,
104104
@NotNull final Module... forModules) {
105+
PsiElement tmp = element;
106+
while (!(tmp instanceof ProtoPsiFileRoot || tmp == null)) {
107+
tmp = tmp.getParent();
108+
}
109+
ProtoPsiFileRoot root = (ProtoPsiFileRoot) tmp;
105110
return new FileReferenceSet(text, element, offset, this, true, myEndingSlashNotAllowed) {
106111

107112

@@ -132,12 +137,12 @@ public Collection<PsiFileSystemItem> computeDefaultContexts() {
132137
if (forModules.length > 0) {
133138
Set<PsiFileSystemItem> rootsForModules = ContainerUtil.newLinkedHashSet();
134139
for (Module forModule : forModules) {
135-
rootsForModules.addAll(getRoots(forModule, null));
140+
rootsForModules.addAll(getRoots(forModule, root));
136141
}
137142
return rootsForModules;
138143
}
139144

140-
return getRoots(ModuleUtilCore.findModuleForPsiElement(getElement()), null);
145+
return getRoots(ModuleUtilCore.findModuleForPsiElement(getElement()), root);
141146
}
142147

143148
@Override

src/test/java/io/protostuff/jetbrains/plugin/reference/ReferenceTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.intellij.psi.PsiReference;
55
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
66
import io.protostuff.jetbrains.plugin.psi.DataType;
7+
import io.protostuff.jetbrains.plugin.psi.ProtoPsiFileRoot;
78

89
/**
910
* Tests for resolving type references.
@@ -54,6 +55,18 @@ public void testImportedRelativelyTwoLevelsMessageReference() {
5455
"reference/relative/import/ImportedRelativelyTestData.proto");
5556
}
5657

58+
public void testImportedRelativelyFileReference() {
59+
String[] file = new String[]{"reference/relative/import/ImportedRelativelyFileReferenceTestData.proto",
60+
"reference/relative/import/ImportedRelativelyTestData.proto"};
61+
myFixture.configureByFiles(file);
62+
PsiReference referenceAtCaretPosition = myFixture.getReferenceAtCaretPositionWithAssertion(file);
63+
PsiElement target = referenceAtCaretPosition.resolve();
64+
assertNotNull(target);
65+
assertTrue(target instanceof ProtoPsiFileRoot);
66+
ProtoPsiFileRoot protoPsiFileRoot = (ProtoPsiFileRoot) target;
67+
assertEquals("ImportedRelativelyTestData.proto", protoPsiFileRoot.getName());
68+
}
69+
5770
/**
5871
* Check that type references work correctly inside of group block.
5972
*
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto3";
2+
3+
package relative.import;
4+
5+
import "relative/import/<caret>ImportedRelativelyTestData.proto";
6+
7+
message TestMessage {
8+
import.ImportedMessage1 message = 1;
9+
}

0 commit comments

Comments
 (0)