Skip to content

Commit 2aedbe2

Browse files
#46: Rename refactoring for map/oneof fields
1 parent cb3b646 commit 2aedbe2

File tree

11 files changed

+137
-12
lines changed

11 files changed

+137
-12
lines changed

src/main/java/io/protostuff/jetbrains/plugin/psi/MapNode.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,20 @@
88
import com.intellij.navigation.ItemPresentation;
99
import com.intellij.navigation.ItemPresentationProviders;
1010
import java.util.Optional;
11-
import org.antlr.jetbrains.adapter.psi.AntlrPsiNode;
1211
import org.jetbrains.annotations.NotNull;
1312

1413
/**
1514
* Map node.
1615
*
1716
* @author Kostiantyn Shchepanovskyi
1817
*/
19-
public class MapNode extends AntlrPsiNode
18+
public class MapNode extends AbstractNamedNode
2019
implements MessageField, KeywordsContainer {
2120

2221
public MapNode(@NotNull ASTNode node) {
2322
super(node);
2423
}
2524

26-
2725
@Override
2826
public String getFieldName() {
2927
ASTNode nameNode = getFieldNameNode();

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.protostuff.jetbrains.plugin.psi.ExtendNode;
2525
import io.protostuff.jetbrains.plugin.psi.FieldNode;
2626
import io.protostuff.jetbrains.plugin.psi.FieldReferenceNode;
27+
import io.protostuff.jetbrains.plugin.psi.MapNode;
2728
import io.protostuff.jetbrains.plugin.psi.MessageField;
2829
import io.protostuff.jetbrains.plugin.psi.MessageNode;
2930
import io.protostuff.jetbrains.plugin.psi.ProtoPsiFileRoot;
@@ -58,6 +59,7 @@ public class FieldReferenceProviderImpl implements FieldReferenceProvider {
5859
private static final Map<Class<? extends PsiElement>, String> TARGET_MAPPING
5960
= ImmutableMap.<Class<? extends PsiElement>, String>builder()
6061
.put(FieldNode.class, MSG_FIELD_OPTIONS)
62+
.put(MapNode.class, MSG_FIELD_OPTIONS)
6163
.put(MessageNode.class, MSG_MESSAGE_OPTIONS)
6264
.put(EnumConstantNode.class, MSG_ENUM_VALUE_OPTIONS)
6365
.put(EnumNode.class, MSG_ENUM_OPTIONS)
@@ -102,11 +104,13 @@ public PsiReference[] getReferencesByElement(FieldReferenceNode fieldReference)
102104
message = null;
103105
if (targetField != null) {
104106
TypeReferenceNode fieldTypeRef = targetField.getFieldType();
105-
PsiReference reference = fieldTypeRef.getReference();
106-
if (reference != null) {
107-
PsiElement fieldType = reference.resolve();
108-
if (fieldType instanceof MessageNode) {
109-
message = (MessageNode) fieldType;
107+
if (fieldTypeRef != null) {
108+
PsiReference reference = fieldTypeRef.getReference();
109+
if (reference != null) {
110+
PsiElement fieldType = reference.resolve();
111+
if (fieldType instanceof MessageNode) {
112+
message = (MessageNode) fieldType;
113+
}
110114
}
111115
}
112116
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public void testStandardFieldOptionReference() {
2929
"StandardFieldOptionReferenceTestData.proto");
3030
}
3131

32+
public void testStandardMapFieldOptionReference() {
33+
checkReferenceToField(".google.protobuf.FieldOptions", "deprecated",
34+
"StandardMapFieldOptionReferenceTestData.proto");
35+
}
36+
3237
public void testStandardEnumOptionReference() {
3338
checkReferenceToField(".google.protobuf.EnumOptions", "deprecated",
3439
"StandardEnumOptionReferenceTestData.proto");

src/test/java/io/protostuff/jetbrains/plugin/rename/field/RenameFieldTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
44
import io.protostuff.jetbrains.plugin.psi.FieldNode;
5+
import io.protostuff.jetbrains.plugin.psi.MapNode;
56
import io.protostuff.jetbrains.plugin.psi.OptionNode;
67
import org.junit.Assert;
78

@@ -31,4 +32,32 @@ public void testRenameField_caretAtOption() {
3132
Assert.assertNotNull(option);
3233
}
3334

35+
public void testRenameOneofField_caretAtFieldName() {
36+
myFixture.configureByFiles("rename/field/RenameOneofField_CaretAtField.proto");
37+
myFixture.renameElementAtCaret("NewName");
38+
OptionNode option = myFixture.findElementByText("(foo).NewName", OptionNode.class);
39+
Assert.assertNotNull(option);
40+
}
41+
42+
public void testRenameOneofField_caretAtOption() {
43+
myFixture.configureByFiles("rename/field/RenameOneofField_CaretAtOption.proto");
44+
myFixture.renameElementAtCaret("NewName");
45+
FieldNode option = myFixture.findElementByText("NewName", FieldNode.class);
46+
Assert.assertNotNull(option);
47+
}
48+
49+
public void testRenameMapField_caretAtFieldName() {
50+
myFixture.configureByFiles("rename/field/RenameMapField_CaretAtField.proto");
51+
myFixture.renameElementAtCaret("NewName");
52+
OptionNode option = myFixture.findElementByText("(foo).NewName", OptionNode.class);
53+
Assert.assertNotNull(option);
54+
}
55+
56+
public void testRenameMapField_caretAtOption() {
57+
myFixture.configureByFiles("rename/field/RenameMapField_CaretAtOption.proto");
58+
myFixture.renameElementAtCaret("NewName");
59+
MapNode option = myFixture.findElementByText("NewName", MapNode.class);
60+
Assert.assertNotNull(option);
61+
}
62+
3463
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package reference.options;
4+
5+
message A {
6+
map<int32,int32> x = 1[depre<caret>cated = true];
7+
}

src/test/resources/rename/field/RenameField_CaretAtField.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ extend google.protobuf.FieldOptions {
1010
}
1111

1212
message Foo {
13-
optional int32 name = 1;
13+
optional int32 name<caret> = 1;
1414
}
1515

1616
message Bar {
17-
optional string field = 1 [(foo).name<caret> = 1];
17+
optional string field = 1 [(foo).name = 1];
1818
}

src/test/resources/rename/field/RenameField_CaretAtOption.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ extend google.protobuf.FieldOptions {
1010
}
1111

1212
message Foo {
13-
optional int32 name<caret> = 1;
13+
optional int32 name = 1;
1414
}
1515

1616
message Bar {
17-
optional string field = 1 [(foo).name = 1];
17+
optional string field = 1 [(foo).name<caret> = 1];
1818
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
syntax = "proto2";
2+
3+
package rename.field;
4+
5+
import "google/protobuf/descriptor.proto";
6+
7+
8+
extend google.protobuf.FieldOptions {
9+
optional Foo foo = 52003;
10+
}
11+
12+
message Foo {
13+
map<int32, int32> name<caret> = 1;
14+
}
15+
16+
message Bar {
17+
optional string field = 1 [(foo).name = {
18+
key: 1
19+
value: 1
20+
}];
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
syntax = "proto2";
2+
3+
package rename.field;
4+
5+
import "google/protobuf/descriptor.proto";
6+
7+
8+
extend google.protobuf.FieldOptions {
9+
optional Foo foo = 52003;
10+
}
11+
12+
message Foo {
13+
map<int32, int32> name = 1;
14+
}
15+
16+
message Bar {
17+
optional string field = 1 [(foo).name<caret> = {
18+
key: 1
19+
value: 1
20+
}];
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
syntax = "proto2";
2+
3+
package rename.field;
4+
5+
import "google/protobuf/descriptor.proto";
6+
7+
8+
extend google.protobuf.FieldOptions {
9+
optional Foo foo = 52003;
10+
}
11+
12+
message Foo {
13+
oneof oneof {
14+
int32 name<caret> = 1;
15+
}
16+
}
17+
18+
message Bar {
19+
optional string field = 1 [(foo).name = 1];
20+
}

0 commit comments

Comments
 (0)