Skip to content

Commit

Permalink
Merge pull request #2813 from wbars/move-to-struct-init
Browse files Browse the repository at this point in the history
Move to struct init intention: missed some parens-related cases
  • Loading branch information
zolotov authored Oct 28, 2016
2 parents 0bfabe5 + 00f9d70 commit 9c93e6d
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static List<GoReferenceExpression> getFieldReferenceExpressions(@NotNull
}

@Nullable
private static GoReferenceExpression unwrapParensAndCast(@NotNull PsiElement e) {
private static GoReferenceExpression unwrapParensAndCast(@Nullable PsiElement e) {
while (e instanceof GoParenthesesExpr) {
e = ((GoParenthesesExpr)e).getExpression();
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

type S struct {
foo string
}

func main() {
s := S{foo: "bar"}

print(s.foo)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

type S struct {
foo string
}

func main() {
s := S{}
(s.foo) <caret>= "bar"
print(s.foo)
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 9c93e6d

Please sign in to comment.