Permalink
Browse files

Move to struct init intention: missing some parens cases

  • Loading branch information...
1 parent 0bfabe5 commit 00f9d7094c8124e9301919eb674dba1e83d9d546 @wbars wbars committed Oct 28, 2016
@@ -97,7 +97,7 @@ private static GoReferenceExpression getFieldReferenceExpression(@NotNull PsiEle
}
@Nullable
- private static GoReferenceExpression unwrapParensAndCast(@NotNull PsiElement e) {
+ private static GoReferenceExpression unwrapParensAndCast(@Nullable PsiElement e) {
while (e instanceof GoParenthesesExpr) {
e = ((GoParenthesesExpr)e).getExpression();
}
@@ -116,18 +116,23 @@ private static boolean isFieldDefinition(@Nullable PsiElement element) {
private static boolean isAssignedInPreviousStatement(@NotNull GoExpression referenceExpression,
@NotNull GoAssignmentStatement assignment) {
- GoReferenceExpression rightExpression = ObjectUtils.tryCast(GoPsiImplUtil.getRightExpression(assignment, referenceExpression),
- GoReferenceExpression.class);
+ GoReferenceExpression rightExpression =
+ unwrapParensAndCast(GoPsiImplUtil.getRightExpression(assignment, getTopmostExpression(referenceExpression)));
PsiElement resolve = rightExpression != null ? rightExpression.resolve() : null;
GoStatement previousElement = resolve != null ? PsiTreeUtil.getPrevSiblingOfType(assignment, GoStatement.class) : null;
return previousElement != null && exists(getLeftHandElements(previousElement), e -> isResolvedTo(e, resolve));
}
+ @NotNull
+ private static GoExpression getTopmostExpression(@NotNull GoExpression expression) {
+ return ObjectUtils.notNull(PsiTreeUtil.getTopmostParentOfType(expression, GoExpression.class), expression);
+ }
+
private static boolean isResolvedTo(@Nullable PsiElement e, @Nullable PsiElement resolve) {
if (e instanceof GoVarDefinition) return resolve == e;
- GoReferenceExpression refExpression = ObjectUtils.tryCast(e, GoReferenceExpression.class);
+ GoReferenceExpression refExpression = unwrapParensAndCast(e);
return refExpression != null && refExpression.resolve() == resolve;
}
@@ -241,8 +246,7 @@ private static void moveFieldReferenceExpressions(@NotNull Data data) {
if (literalValue == null) return;
for (GoReferenceExpression expression : data.getReferenceExpressions()) {
- GoExpression parentExpression = PsiTreeUtil.getTopmostParentOfType(expression, GoExpression.class);
- GoExpression anchor = parentExpression != null ? parentExpression : expression;
+ GoExpression anchor = getTopmostExpression(expression);
GoExpression fieldValue = GoPsiImplUtil.getRightExpression(data.getAssignment(), anchor);
if (fieldValue == null) continue;
@@ -0,0 +1,11 @@
+package main
+
+type S struct {
+ foo string
+}
+
+func main() {
+ s := S{foo: "bar"}
+
+ print(s.foo)
+}
@@ -0,0 +1,11 @@
+package main
+
+type S struct {
+ foo string
+}
+
+func main() {
+ s := S{}
+ (s.foo) <caret>= "bar"
+ print(s.foo)
+}
@@ -0,0 +1,15 @@
+package main
+
+type S struct {
+ foo string
+ bar string
+}
+
+func main() {
+ var s S
+ var str string
+ s, (str) = S{}, "bar"
+ s.foo, (s.bar)<caret> = "foo", ((str))
+ print(s.foo)
+ print(str)
+}
@@ -0,0 +1,15 @@
+package main
+
+type S struct {
+ foo string
+ bar string
+}
+
+func main() {
+ var s S
+ var str string
+ s, (str) = S{}, "bar"
+ s.foo, s.bar<caret> = "foo", ((str))
+ print(s.foo)
+ print(str)
+}
@@ -0,0 +1,15 @@
+package main
+
+type S struct {
+ foo string
+ bar string
+}
+
+func main() {
+ var s S
+ var str string
+ s, str = S{}, "bar"
+ s.foo, s.bar<caret> = "foo", ((str))
+ print(s.foo)
+ print(str)
+}
@@ -49,13 +49,17 @@ protected String getBasePath() {
public void testMultipleAssignmentsMiddle() { doTest(); }
public void testMultipleFieldsPartlyAssigned() { doTest(); }
public void testWithParens() { doTest(); }
+ public void testFieldExtractedFromParens() { doTest(); }
public void testDuplicateFields() { doTest(); }
public void testMultiReturnFunction() { doTestNoFix(); }
public void testWrongStruct() { doTestNoFix(); }
public void testExistingDeclaration() { doTestNoFix(); }
public void testNotExistingField() { doTestNoFix(); }
public void testJustAssignedVarWrongCaret() { doTestNoFix(); }
+ public void testJustAssignedVarWrongCaretWithParens() { doTestNoFix(); }
public void testJustInitializedVarWrongCaret() { doTestNoFix(); }
+ public void testJustAssignedVarBothParens() { doTestNoFix(); }
+ public void testJustAssignedFieldParens() { doTestNoFix(); }
}

0 comments on commit 00f9d70

Please sign in to comment.