Skip to content

Commit 82f9f99

Browse files
author
Sergey Ibragimov
committed
fix case with parentheses expressions
rename
1 parent 316e5d9 commit 82f9f99

File tree

6 files changed

+48
-15
lines changed

6 files changed

+48
-15
lines changed

src/com/goide/inspections/GoStructInitializationInspection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public GoReplaceWithNamedStructFieldQuickFix(@NotNull String structField) {
108108
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
109109
PsiElement startElement = descriptor.getStartElement();
110110
if (startElement instanceof GoElement) {
111-
startElement.replace(GoElementFactory.createNamedStructField(project, myStructField, startElement.getText()));
111+
startElement.replace(GoElementFactory.createLiteralValueElement(project, myStructField, startElement.getText()));
112112
}
113113
}
114114
}

src/com/goide/intentions/GoMoveToStructInitializationIntention.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,16 @@ private static GoReferenceExpression getFieldReferenceExpression(@NotNull PsiEle
9292

9393
@NotNull
9494
private static List<GoReferenceExpression> getFieldReferenceExpressions(@NotNull GoAssignmentStatement assignment) {
95-
return map(filter(getLeftHandElements(assignment), GoMoveToStructInitializationIntention::isFieldReferenceExpression),
96-
GoReferenceExpression.class::cast);
95+
return filter(map(getLeftHandElements(assignment), GoMoveToStructInitializationIntention::unwrapParensAndCast),
96+
GoMoveToStructInitializationIntention::isFieldReferenceExpression);
97+
}
98+
99+
@Nullable
100+
private static GoReferenceExpression unwrapParensAndCast(@NotNull PsiElement e) {
101+
while (e instanceof GoParenthesesExpr) {
102+
e = ((GoParenthesesExpr)e).getExpression();
103+
}
104+
return ObjectUtils.tryCast(e, GoReferenceExpression.class);
97105
}
98106

99107
@Contract("null -> false")
@@ -233,17 +241,19 @@ private static void moveFieldReferenceExpressions(@NotNull Data data) {
233241
if (literalValue == null) return;
234242

235243
for (GoReferenceExpression expression : data.getReferenceExpressions()) {
236-
GoExpression fieldValue = GoPsiImplUtil.getRightExpression(data.getAssignment(), expression);
244+
GoExpression parentExpression = PsiTreeUtil.getTopmostParentOfType(expression, GoExpression.class);
245+
GoExpression anchor = parentExpression != null ? parentExpression : expression;
246+
GoExpression fieldValue = GoPsiImplUtil.getRightExpression(data.getAssignment(), anchor);
237247
if (fieldValue == null) continue;
238248

239-
GoPsiImplUtil.deleteExpressionFromAssignment(data.getAssignment(), expression);
249+
GoPsiImplUtil.deleteExpressionFromAssignment(data.getAssignment(), anchor);
240250
addFieldDefinition(literalValue, expression.getIdentifier().getText(), fieldValue.getText());
241251
}
242252
}
243253

244254
private static void addFieldDefinition(@NotNull GoLiteralValue literalValue, @NotNull String name, @NotNull String value) {
245255
Project project = literalValue.getProject();
246-
PsiElement newField = GoElementFactory.createNamedStructField(project, name, value);
256+
PsiElement newField = GoElementFactory.createLiteralValueElement(project, name, value);
247257
GoElement lastElement = getLastItem(literalValue.getElementList());
248258
if (lastElement == null) {
249259
literalValue.addAfter(newField, literalValue.getLbrace());

src/com/goide/psi/impl/GoElementFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ public static GoType createType(@NotNull Project project, @NotNull String text)
256256
return PsiTreeUtil.findChildOfType(file, GoType.class);
257257
}
258258

259-
public static PsiElement createNamedStructField(@NotNull Project project, @NotNull String field, @NotNull String element) {
260-
GoFile file = createFileFromText(project, "package a; var _ = struct { a string } { " + field + ": " + element + " }");
259+
public static PsiElement createLiteralValueElement(@NotNull Project project, @NotNull String key, @NotNull String value) {
260+
GoFile file = createFileFromText(project, "package a; var _ = struct { a string } { " + key + ": " + value + " }");
261261
return PsiTreeUtil.findChildOfType(file, GoElement.class);
262262
}
263263

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+
string
5+
}
6+
7+
func main() {
8+
s := S{string: "bar"}
9+
(_) = "foo"
10+
print(s.string)
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+
string
5+
}
6+
7+
func main() {
8+
s := S{}
9+
(_), ((s.string)) = "foo", "bar"<caret>
10+
print(s.string)
11+
}

tests/com/goide/quickfix/GoMoveToStructInitializationIntentionTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,20 @@ protected String getBasePath() {
4242
public void testTwoSameStructuresAssignment() { doTest(); }
4343
public void testJustAssignedVar() { doTest(); }
4444
public void testJustInitializedVar() { doTest(); }
45-
public void testMultiReturnFunction() { doTestNoFix(); }
46-
public void testWrongStruct() { doTestNoFix(); }
47-
public void testExistingDeclaration() { doTestNoFix(); }
48-
public void testNotExistingField() { doTestNoFix(); }
49-
public void testJustAssignedVarWrongCaret() { doTestNoFix(); }
50-
public void testJustInitializedVarWrongCaret() { doTestNoFix(); }
5145
public void testInvalidAssignment() { doTest(); }
5246
public void testExistingField() { doTest(); }
5347
public void testMultipleAssignmentsLeftmost() { doTest(); }
5448
public void testMultipleAssignmentsRightmost() { doTest(); }
5549
public void testMultipleAssignmentsMiddle() { doTest(); }
5650
public void testMultipleFieldsPartlyAssigned() { doTest(); }
57-
public void testDuplicateFields() { doTest(); }
51+
public void testWithParens() { doTest(); }
5852

53+
public void testDuplicateFields() { doTest(); }
54+
public void testMultiReturnFunction() { doTestNoFix(); }
55+
public void testWrongStruct() { doTestNoFix(); }
56+
public void testExistingDeclaration() { doTestNoFix(); }
57+
public void testNotExistingField() { doTestNoFix(); }
58+
public void testJustAssignedVarWrongCaret() { doTestNoFix(); }
59+
public void testJustInitializedVarWrongCaret() { doTestNoFix(); }
5960
}
6061

0 commit comments

Comments
 (0)