Skip to content

Commit 9c93e6d

Browse files
authored
Merge pull request #2813 from wbars/move-to-struct-init
Move to struct init intention: missed some parens-related cases
2 parents 0bfabe5 + 00f9d70 commit 9c93e6d

7 files changed

+81
-6
lines changed

src/com/goide/intentions/GoMoveToStructInitializationIntention.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static List<GoReferenceExpression> getFieldReferenceExpressions(@NotNull
9797
}
9898

9999
@Nullable
100-
private static GoReferenceExpression unwrapParensAndCast(@NotNull PsiElement e) {
100+
private static GoReferenceExpression unwrapParensAndCast(@Nullable PsiElement e) {
101101
while (e instanceof GoParenthesesExpr) {
102102
e = ((GoParenthesesExpr)e).getExpression();
103103
}
@@ -116,18 +116,23 @@ private static boolean isFieldDefinition(@Nullable PsiElement element) {
116116

117117
private static boolean isAssignedInPreviousStatement(@NotNull GoExpression referenceExpression,
118118
@NotNull GoAssignmentStatement assignment) {
119-
GoReferenceExpression rightExpression = ObjectUtils.tryCast(GoPsiImplUtil.getRightExpression(assignment, referenceExpression),
120-
GoReferenceExpression.class);
119+
GoReferenceExpression rightExpression =
120+
unwrapParensAndCast(GoPsiImplUtil.getRightExpression(assignment, getTopmostExpression(referenceExpression)));
121121

122122
PsiElement resolve = rightExpression != null ? rightExpression.resolve() : null;
123123
GoStatement previousElement = resolve != null ? PsiTreeUtil.getPrevSiblingOfType(assignment, GoStatement.class) : null;
124124
return previousElement != null && exists(getLeftHandElements(previousElement), e -> isResolvedTo(e, resolve));
125125
}
126126

127+
@NotNull
128+
private static GoExpression getTopmostExpression(@NotNull GoExpression expression) {
129+
return ObjectUtils.notNull(PsiTreeUtil.getTopmostParentOfType(expression, GoExpression.class), expression);
130+
}
131+
127132
private static boolean isResolvedTo(@Nullable PsiElement e, @Nullable PsiElement resolve) {
128133
if (e instanceof GoVarDefinition) return resolve == e;
129134

130-
GoReferenceExpression refExpression = ObjectUtils.tryCast(e, GoReferenceExpression.class);
135+
GoReferenceExpression refExpression = unwrapParensAndCast(e);
131136
return refExpression != null && refExpression.resolve() == resolve;
132137
}
133138

@@ -241,8 +246,7 @@ private static void moveFieldReferenceExpressions(@NotNull Data data) {
241246
if (literalValue == null) return;
242247

243248
for (GoReferenceExpression expression : data.getReferenceExpressions()) {
244-
GoExpression parentExpression = PsiTreeUtil.getTopmostParentOfType(expression, GoExpression.class);
245-
GoExpression anchor = parentExpression != null ? parentExpression : expression;
249+
GoExpression anchor = getTopmostExpression(expression);
246250
GoExpression fieldValue = GoPsiImplUtil.getRightExpression(data.getAssignment(), anchor);
247251
if (fieldValue == null) continue;
248252

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
s := S{foo: "bar"}
9+
10+
print(s.foo)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
s := S{}
9+
(s.foo) <caret>= "bar"
10+
print(s.foo)
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
bar string
6+
}
7+
8+
func main() {
9+
var s S
10+
var str string
11+
s, (str) = S{}, "bar"
12+
s.foo, (s.bar)<caret> = "foo", ((str))
13+
print(s.foo)
14+
print(str)
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
bar string
6+
}
7+
8+
func main() {
9+
var s S
10+
var str string
11+
s, (str) = S{}, "bar"
12+
s.foo, s.bar<caret> = "foo", ((str))
13+
print(s.foo)
14+
print(str)
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
bar string
6+
}
7+
8+
func main() {
9+
var s S
10+
var str string
11+
s, str = S{}, "bar"
12+
s.foo, s.bar<caret> = "foo", ((str))
13+
print(s.foo)
14+
print(str)
15+
}

tests/com/goide/quickfix/GoMoveToStructInitializationIntentionTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ protected String getBasePath() {
4949
public void testMultipleAssignmentsMiddle() { doTest(); }
5050
public void testMultipleFieldsPartlyAssigned() { doTest(); }
5151
public void testWithParens() { doTest(); }
52+
public void testFieldExtractedFromParens() { doTest(); }
5253

5354
public void testDuplicateFields() { doTest(); }
5455
public void testMultiReturnFunction() { doTestNoFix(); }
5556
public void testWrongStruct() { doTestNoFix(); }
5657
public void testExistingDeclaration() { doTestNoFix(); }
5758
public void testNotExistingField() { doTestNoFix(); }
5859
public void testJustAssignedVarWrongCaret() { doTestNoFix(); }
60+
public void testJustAssignedVarWrongCaretWithParens() { doTestNoFix(); }
5961
public void testJustInitializedVarWrongCaret() { doTestNoFix(); }
62+
public void testJustAssignedVarBothParens() { doTestNoFix(); }
63+
public void testJustAssignedFieldParens() { doTestNoFix(); }
6064
}
6165

0 commit comments

Comments
 (0)