Skip to content

Commit fbffbc7

Browse files
#46: Field rename refactoring (basic)
1 parent 202f1d9 commit fbffbc7

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.protostuff.jetbrains.plugin.psi.manipulator;
2+
3+
import com.intellij.openapi.project.Project;
4+
import com.intellij.openapi.util.TextRange;
5+
import com.intellij.psi.AbstractElementManipulator;
6+
import com.intellij.psi.PsiElement;
7+
import com.intellij.psi.PsiFileFactory;
8+
import com.intellij.psi.impl.PsiFileFactoryImpl;
9+
import com.intellij.util.IncorrectOperationException;
10+
import io.protostuff.compiler.parser.ProtoParser;
11+
import io.protostuff.jetbrains.plugin.ProtoLanguage;
12+
import io.protostuff.jetbrains.plugin.ProtoParserDefinition;
13+
import io.protostuff.jetbrains.plugin.psi.FieldReferenceNode;
14+
import org.antlr.jetbrains.adapter.lexer.RuleIElementType;
15+
import org.antlr.jetbrains.adapter.psi.ScopeNode;
16+
import org.jetbrains.annotations.NotNull;
17+
18+
/**
19+
* Field reference node manipulator.
20+
*
21+
* @author Kostiantyn Shchepanovskyi
22+
*/
23+
public class FieldReferenceNodeManipulator extends AbstractElementManipulator<FieldReferenceNode> {
24+
25+
@Override
26+
public FieldReferenceNode handleContentChange(@NotNull FieldReferenceNode node, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
27+
String oldText = node.getText();
28+
String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset());
29+
Project project = node.getProject();
30+
PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project);
31+
RuleIElementType type = ProtoParserDefinition.rule(ProtoParser.RULE_fieldRerefence);
32+
ScopeNode context = node.getContext();
33+
PsiElement newNode = factory.createElementFromText(newText, ProtoLanguage.INSTANCE, type, context);
34+
if (newNode == null) {
35+
throw new IncorrectOperationException();
36+
}
37+
return (FieldReferenceNode) node.replace(newNode);
38+
}
39+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@
173173
<lang.elementManipulator
174174
forClass="io.protostuff.jetbrains.plugin.psi.TypeReferenceNode"
175175
implementationClass="io.protostuff.jetbrains.plugin.psi.manipulator.TypeReferenceNodeManipulator"/>
176+
<lang.elementManipulator
177+
forClass="io.protostuff.jetbrains.plugin.psi.FieldReferenceNode"
178+
implementationClass="io.protostuff.jetbrains.plugin.psi.manipulator.FieldReferenceNodeManipulator"/>
176179

177180
</extensions>
178181

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.protostuff.jetbrains.plugin.rename.field;
2+
3+
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
4+
import io.protostuff.jetbrains.plugin.psi.FieldNode;
5+
import io.protostuff.jetbrains.plugin.psi.OptionNode;
6+
import org.junit.Assert;
7+
8+
/**
9+
* Tests for rename message refactoring.
10+
*
11+
* @author Kostiantyn Shchepanovskyi
12+
*/
13+
public class RenameFieldTest extends LightCodeInsightFixtureTestCase {
14+
15+
@Override
16+
protected String getTestDataPath() {
17+
return "src/test/resources/";
18+
}
19+
20+
public void testRenameField_caretAtFieldName() {
21+
myFixture.configureByFiles("rename/field/RenameField_CaretAtField.proto");
22+
myFixture.renameElementAtCaret("NewName");
23+
OptionNode option = myFixture.findElementByText("(foo).NewName", OptionNode.class);
24+
Assert.assertNotNull(option);
25+
}
26+
27+
public void testRenameField_caretAtOption() {
28+
myFixture.configureByFiles("rename/field/RenameField_CaretAtOption.proto");
29+
myFixture.renameElementAtCaret("NewName");
30+
FieldNode option = myFixture.findElementByText("NewName", FieldNode.class);
31+
Assert.assertNotNull(option);
32+
}
33+
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
optional int32 name = 1;
14+
}
15+
16+
message Bar {
17+
optional string field = 1 [(foo).name<caret> = 1];
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
optional int32 name<caret> = 1;
14+
}
15+
16+
message Bar {
17+
optional string field = 1 [(foo).name = 1];
18+
}

0 commit comments

Comments
 (0)