Skip to content

Commit 9552488

Browse files
GH-130: Compare type references by type's full/qualified name
Resolve weird case when imports from current file and imports from imported file resolve to different descriptor.proto files, resulting in broken references for custom options.
1 parent 4155f09 commit 9552488

File tree

4 files changed

+865
-0
lines changed

4 files changed

+865
-0
lines changed

src/main/java/io/protostuff/jetbrains/plugin/reference/TypeReference.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.intellij.openapi.util.TextRange;
44
import com.intellij.psi.PsiElement;
55
import com.intellij.psi.PsiReferenceBase;
6+
import io.protostuff.jetbrains.plugin.psi.ProtoType;
7+
import java.util.Objects;
68
import org.jetbrains.annotations.NotNull;
79
import org.jetbrains.annotations.Nullable;
810

@@ -31,4 +33,16 @@ public PsiElement resolve() {
3133
public Object[] getVariants() {
3234
return new Object[0];
3335
}
36+
37+
@Override
38+
public boolean isReferenceTo(@NotNull PsiElement element) {
39+
if (target instanceof ProtoType) {
40+
ProtoType targetType = (ProtoType) target;
41+
if (element instanceof ProtoType) {
42+
ProtoType elementType = (ProtoType) element;
43+
return Objects.equals(targetType.getFullName(), elementType.getFullName());
44+
}
45+
}
46+
return super.isReferenceTo(element);
47+
}
3448
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ public void testImportedCustomOptionReference() {
4646
Assert.assertTrue(field.getParent() instanceof ExtendEntryNode);
4747
}
4848

49+
public void testImportedCustomOptionReference_withDescriptorProtoImport() {
50+
myFixture.configureByFiles("reference/options/custom/SimpleExtensionField.proto",
51+
"google/protobuf/__descriptor.proto");
52+
PsiReference reference = myFixture.getReferenceAtCaretPositionWithAssertion(
53+
"reference/options/custom/ImportedExtension_withDescriptorProtoImport.proto"
54+
);
55+
PsiElement target = reference.resolve();
56+
Assert.assertNotNull(target);
57+
Assert.assertTrue(target instanceof FieldNode);
58+
FieldNode field = (FieldNode) target;
59+
Assert.assertEquals("bar", field.getFieldName());
60+
Assert.assertTrue(field.getParent() instanceof ExtendEntryNode);
61+
}
62+
4963
public void testImportedImportedCustomOptionReference() {
5064
myFixture.configureByFiles(
5165
"reference/options/custom/SimpleExtensionField.proto",

0 commit comments

Comments
 (0)