diff --git a/src/com/goide/intentions/GoMoveToStructInitializationIntention.java b/src/com/goide/intentions/GoMoveToStructInitializationIntention.java index 0040439367..9cc7789727 100644 --- a/src/com/goide/intentions/GoMoveToStructInitializationIntention.java +++ b/src/com/goide/intentions/GoMoveToStructInitializationIntention.java @@ -60,12 +60,34 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull Psi private static Data getData(@NotNull PsiElement element) { if (!element.isValid() || !element.isWritable()) return null; GoAssignmentStatement assignment = getValidAssignmentParent(element); - GoReferenceExpression selectedFieldReference = assignment != null ? getFieldReferenceExpression(element, assignment) : null; - GoCompositeLit compositeLit = selectedFieldReference != null ? getStructLiteralByReference(selectedFieldReference, assignment) : null; - if (compositeLit == null) return null; + GoStatement prevStatement = assignment != null ? PsiTreeUtil.getPrevSiblingOfType(assignment, GoStatement.class) : null; + GoReferenceExpression selectedExpression = prevStatement != null + ? getFieldReferenceExpression(element, assignment, prevStatement) : null; + if (selectedExpression == null) return null; - List references = getUninitializedSingleFieldReferences(assignment, selectedFieldReference, compositeLit); - return !references.isEmpty() ? new Data(assignment, compositeLit, references) : null; + GoVarDefinition definition = getDefinition(selectedExpression); + GoType type = definition != null ? definition.getGoType(null) : null; + GoStructType structType = type != null ? ObjectUtils.tryCast(type.getUnderlyingType(), GoStructType.class) : null; + if (structType == null) return null; + + boolean needReplaceDeclaration = isUnassigned(getSingleVarSpec(prevStatement, definition)); + GoCompositeLit compositeLit = !needReplaceDeclaration ? getStructLiteralByReference(selectedExpression, prevStatement) : null; + if (compositeLit == null && !needReplaceDeclaration) return null; + + List expressions = getUninitializedReferenceExpressions(assignment, prevStatement, definition, compositeLit, + structType); + return !expressions.isEmpty() ? new Data(assignment, compositeLit, expressions, prevStatement, definition) : null; + } + + @Nullable + private static GoCompositeLit createStructLiteral(@NotNull GoVarDefinition definition, @NotNull Project project) { + GoType type = definition.getGoType(null); + return type != null ? GoElementFactory.createCompositeLit(project, type) : null; + } + + @Nullable + private static GoVarDefinition getDefinition(@NotNull GoReferenceExpression referenceExpressions) { + return ObjectUtils.tryCast(resolveQualifier(referenceExpressions), GoVarDefinition.class); } @Nullable @@ -77,17 +99,21 @@ private static GoAssignmentStatement getValidAssignmentParent(@Nullable PsiEleme @Nullable private static GoReferenceExpression getFieldReferenceExpression(@NotNull PsiElement selectedElement, - @NotNull GoAssignmentStatement assignment) { - GoReferenceExpression selectedReferenceExpression = PsiTreeUtil.getTopmostParentOfType(selectedElement, GoReferenceExpression.class); - if (isFieldReferenceExpression(selectedReferenceExpression)) { - return !isAssignedInPreviousStatement(selectedReferenceExpression, assignment) ? selectedReferenceExpression : null; + @NotNull GoAssignmentStatement assignment, + @NotNull GoStatement prevStatement) { + GoReferenceExpression selectedExpression = PsiTreeUtil.getTopmostParentOfType(selectedElement, GoReferenceExpression.class); + if (isFieldReferenceExpression(selectedExpression)) { + return !isAssignedInStatement(getRightExpression(selectedExpression, assignment), prevStatement) ? selectedExpression : null; } List fieldReferenceExpressions = getFieldReferenceExpressions(assignment); - if (exists(fieldReferenceExpressions, expression -> isAssignedInPreviousStatement(expression, assignment))) return null; + if (exists(fieldReferenceExpressions, expression -> + isAssignedInStatement(getRightExpression(expression, assignment), prevStatement))) { + return null; + } - Set resolvedFields = map2Set(fieldReferenceExpressions, GoMoveToStructInitializationIntention::resolveQualifier); - return resolvedFields.size() == 1 ? getFirstItem(fieldReferenceExpressions) : null; + Set resolvedQualifiers = map2Set(fieldReferenceExpressions, GoMoveToStructInitializationIntention::resolveQualifier); + return resolvedQualifiers.size() == 1 ? getFirstItem(fieldReferenceExpressions) : null; } @NotNull @@ -97,16 +123,31 @@ private static List getFieldReferenceExpressions(@NotNull } @Nullable - private static GoReferenceExpression unwrapParensAndCast(@Nullable PsiElement e) { - while (e instanceof GoParenthesesExpr) { - e = ((GoParenthesesExpr)e).getExpression(); + private static GoReferenceExpression unwrapParensAndCast(@Nullable PsiElement element) { + while (element instanceof GoParenthesesExpr) { + element = ((GoParenthesesExpr)element).getExpression(); } - return ObjectUtils.tryCast(e, GoReferenceExpression.class); + return ObjectUtils.tryCast(element, GoReferenceExpression.class); + } + + @Nullable + @Contract("_, null -> null; null, _ -> null") + private static GoVarSpec getSingleVarSpec(@Nullable GoStatement statement, @Nullable GoVarDefinition definition) { + GoVarDeclaration declaration = statement != null ? statement.getVarDeclaration() : null; + List varSpecs = declaration != null ? declaration.getVarSpecList() : emptyList(); + GoVarSpec singleVarSpec = varSpecs.size() == 1 ? getFirstItem(varSpecs) : null; + List varDefinitions = singleVarSpec != null ? singleVarSpec.getVarDefinitionList() : emptyList(); + return varDefinitions.size() == 1 && definition == getFirstItem(varDefinitions) ? singleVarSpec : null; + } + + @Contract("null -> false") + private static boolean isUnassigned(@Nullable GoVarSpec varSpec) { + return varSpec != null && varSpec.getExpressionList().isEmpty(); } @Contract("null -> false") - private static boolean isFieldReferenceExpression(@Nullable PsiElement element) { - return element instanceof GoReferenceExpression && isFieldDefinition(((GoReferenceExpression)element).resolve()); + private static boolean isFieldReferenceExpression(@Nullable GoReferenceExpression element) { + return element != null && isFieldDefinition(element.resolve()); } @Contract("null -> false") @@ -114,14 +155,14 @@ private static boolean isFieldDefinition(@Nullable PsiElement element) { return element instanceof GoFieldDefinition || element instanceof GoAnonymousFieldDefinition; } - private static boolean isAssignedInPreviousStatement(@NotNull GoExpression referenceExpression, - @NotNull GoAssignmentStatement assignment) { - GoReferenceExpression rightExpression = - unwrapParensAndCast(GoPsiImplUtil.getRightExpression(assignment, getTopmostExpression(referenceExpression))); + private static boolean isAssignedInStatement(@Nullable GoReferenceExpression referenceExpression, @NotNull GoStatement statement) { + PsiElement resolve = referenceExpression != null ? referenceExpression.resolve() : null; + return resolve != null && exists(getLeftHandElements(statement), element -> isResolvedTo(element, resolve)); + } - 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)); + @Nullable + private static GoReferenceExpression getRightExpression(@NotNull GoExpression expression, @NotNull GoAssignmentStatement assignment) { + return unwrapParensAndCast(GoPsiImplUtil.getRightExpression(assignment, getTopmostExpression(expression))); } @NotNull @@ -129,35 +170,36 @@ private static GoExpression getTopmostExpression(@NotNull GoExpression expressio 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; + private static boolean isResolvedTo(@Nullable PsiElement element, @NotNull PsiElement resolve) { + if (element instanceof GoVarDefinition) return resolve == element; - GoReferenceExpression refExpression = unwrapParensAndCast(e); + GoReferenceExpression refExpression = unwrapParensAndCast(element); return refExpression != null && refExpression.resolve() == resolve; } @NotNull - private static List getUninitializedSingleFieldReferences(@NotNull GoAssignmentStatement assignment, - @NotNull GoReferenceExpression fieldReferenceExpression, - @NotNull GoCompositeLit compositeLit) { - PsiElement resolve = resolveQualifier(fieldReferenceExpression); - List uninitializedFieldReferencesByQualifier = - filter(getUninitializedFieldReferenceExpressions(assignment, compositeLit), e -> isResolvedTo(e.getQualifier(), resolve)); - MultiMap resolved = groupBy(uninitializedFieldReferencesByQualifier, GoReferenceExpression::resolve); + private static List getUninitializedReferenceExpressions(@NotNull GoAssignmentStatement assignment, + @NotNull GoStatement prevStatement, + @NotNull GoVarDefinition definition, + @Nullable GoCompositeLit compositeLit, + @NotNull GoStructType type) { + List uninitializedFieldReferences = filter( + getUninitializedFieldReferenceExpressions(assignment, prevStatement, compositeLit, type), + element -> isResolvedTo(element.getQualifier(), definition)); + MultiMap resolved = groupBy(uninitializedFieldReferences, GoReferenceExpression::resolve); return map(filter(resolved.entrySet(), set -> set.getValue().size() == 1), set -> getFirstItem(set.getValue())); } @Nullable private static GoCompositeLit getStructLiteralByReference(@NotNull GoReferenceExpression fieldReferenceExpression, - @NotNull GoAssignmentStatement assignment) { - GoStatement previousStatement = PsiTreeUtil.getPrevSiblingOfType(assignment, GoStatement.class); - if (previousStatement instanceof GoSimpleStatement) { - return getStructLiteral(fieldReferenceExpression, (GoSimpleStatement)previousStatement); + @NotNull GoStatement statement) { + if (statement instanceof GoSimpleStatement) { + return getStructLiteral(fieldReferenceExpression, (GoSimpleStatement)statement); } - if (previousStatement instanceof GoAssignmentStatement) { - return getStructLiteral(fieldReferenceExpression, (GoAssignmentStatement)previousStatement); + if (statement instanceof GoAssignmentStatement) { + return getStructLiteral(fieldReferenceExpression, (GoAssignmentStatement)statement); } - return null; + return getStructLiteral(getDefinition(fieldReferenceExpression), statement); } @Nullable @@ -172,6 +214,7 @@ private static GoCompositeLit getStructLiteral(@NotNull GoReferenceExpression fi } @Nullable + @Contract("null -> null") private static PsiElement resolveQualifier(@NotNull GoReferenceExpression fieldReferenceExpression) { GoReferenceExpression qualifier = fieldReferenceExpression.getQualifier(); return qualifier != null ? qualifier.resolve() : null; @@ -180,7 +223,7 @@ private static PsiElement resolveQualifier(@NotNull GoReferenceExpression fieldR @Nullable private static GoCompositeLit getStructLiteral(@NotNull GoReferenceExpression fieldReferenceExpression, @NotNull GoAssignmentStatement structAssignment) { - GoVarDefinition varDefinition = ObjectUtils.tryCast(resolveQualifier(fieldReferenceExpression), GoVarDefinition.class); + GoVarDefinition varDefinition = getDefinition(fieldReferenceExpression); PsiElement field = fieldReferenceExpression.resolve(); if (varDefinition == null || !isFieldDefinition(field) || !hasStructTypeWithField(varDefinition, (GoNamedElement)field)) { return null; @@ -193,6 +236,13 @@ private static GoCompositeLit getStructLiteral(@NotNull GoReferenceExpression fi return ObjectUtils.tryCast(compositeLit, GoCompositeLit.class); } + @Nullable + @Contract("null, _ -> null") + private static GoCompositeLit getStructLiteral(@Nullable GoVarDefinition definition, @NotNull GoStatement statement) { + GoVarSpec varSpec = definition != null ? getSingleVarSpec(statement, definition) : null; + return varSpec != null ? ObjectUtils.tryCast(getFirstItem(varSpec.getRightExpressionsList()), GoCompositeLit.class) : null; + } + private static boolean hasStructTypeWithField(@NotNull GoVarDefinition structVarDefinition, @NotNull GoNamedElement field) { GoType type = structVarDefinition.getGoType(null); GoStructType structType = type != null ? ObjectUtils.tryCast(type.getUnderlyingType(), GoStructType.class) : null; @@ -207,19 +257,24 @@ private static boolean isFieldInitialization(@NotNull GoElement element, @NotNul @NotNull private static List getUninitializedFieldReferenceExpressions(@NotNull GoAssignmentStatement assignment, - @NotNull GoCompositeLit structLiteral) { + @NotNull GoStatement prevStatement, + @Nullable GoCompositeLit compositeLit, + @NotNull GoStructType type) { return filter(getFieldReferenceExpressions(assignment), expression -> - isUninitializedFieldReferenceExpression(expression, structLiteral) && !isAssignedInPreviousStatement(expression, assignment)); + isUninitializedFieldReferenceExpression(expression, compositeLit, type) && + !isAssignedInStatement(getRightExpression(expression, assignment), prevStatement)); } - @Contract("null, _-> false") - private static boolean isUninitializedFieldReferenceExpression(@Nullable GoReferenceExpression fieldReferenceExpression, - @NotNull GoCompositeLit structLiteral) { - if (fieldReferenceExpression == null) return false; - GoLiteralValue literalValue = structLiteral.getLiteralValue(); + private static boolean isUninitializedFieldReferenceExpression(@NotNull GoReferenceExpression fieldReferenceExpression, + @Nullable GoCompositeLit compositeLit, + @NotNull GoStructType type) { PsiElement resolve = fieldReferenceExpression.resolve(); - return literalValue != null && isFieldDefinition(resolve) && - !exists(literalValue.getElementList(), element -> isFieldInitialization(element, resolve)); + if (resolve == null || !PsiTreeUtil.isAncestor(type, resolve, true)) return false; + + GoLiteralValue literalValue = compositeLit != null ? compositeLit.getLiteralValue() : null; + if (literalValue == null) return true; + + return isFieldDefinition(resolve) && !exists(literalValue.getElementList(), element -> isFieldInitialization(element, resolve)); } @NotNull @@ -238,19 +293,29 @@ private static List getLeftHandElements(@NotNull GoStateme public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { Data data = getData(element); if (data == null) return; - moveFieldReferenceExpressions(data); + + boolean needReplaceDeclaration = data.myCompositeLit == null; + GoCompositeLit compositeLit = needReplaceDeclaration ? createStructLiteral(data.myStructDefinition, project) : data.myCompositeLit; + if (compositeLit == null) return; + + moveFieldReferenceExpressions(data.myReferenceExpressions, compositeLit.getLiteralValue(), data.myAssignment); + if (needReplaceDeclaration) { + String definitionText = data.myStructDefinition.getText(); + GoStatement shortVarStatement = GoElementFactory.createShortVarDeclarationStatement(project, definitionText, compositeLit.getText()); + data.myStructDeclaration.replace(shortVarStatement); + } } - private static void moveFieldReferenceExpressions(@NotNull Data data) { - GoLiteralValue literalValue = data.getCompositeLit().getLiteralValue(); + private static void moveFieldReferenceExpressions(@NotNull List referenceExpressions, + @Nullable GoLiteralValue literalValue, + @NotNull GoAssignmentStatement parentAssignment) { if (literalValue == null) return; - - for (GoReferenceExpression expression : data.getReferenceExpressions()) { + for (GoReferenceExpression expression : referenceExpressions) { GoExpression anchor = getTopmostExpression(expression); - GoExpression fieldValue = GoPsiImplUtil.getRightExpression(data.getAssignment(), anchor); + GoExpression fieldValue = GoPsiImplUtil.getRightExpression(parentAssignment, anchor); if (fieldValue == null) continue; - GoPsiImplUtil.deleteExpressionFromAssignment(data.getAssignment(), anchor); + GoPsiImplUtil.deleteExpressionFromAssignment(parentAssignment, anchor); addFieldDefinition(literalValue, expression.getIdentifier().getText(), fieldValue.getText()); } } @@ -258,39 +323,32 @@ private static void moveFieldReferenceExpressions(@NotNull Data data) { private static void addFieldDefinition(@NotNull GoLiteralValue literalValue, @NotNull String name, @NotNull String value) { Project project = literalValue.getProject(); PsiElement newField = GoElementFactory.createLiteralValueElement(project, name, value); - GoElement lastElement = getLastItem(literalValue.getElementList()); - if (lastElement == null) { - literalValue.addAfter(newField, literalValue.getLbrace()); - } - else { - lastElement.add(GoElementFactory.createComma(project)); - lastElement.add(newField); + PsiElement rbrace = literalValue.getRbrace(); + if (!literalValue.getElementList().isEmpty()) { + literalValue.addBefore(GoElementFactory.createComma(project), rbrace); } + literalValue.addBefore(newField, rbrace); } private static class Data { - private final GoCompositeLit myCompositeLit; - private final GoAssignmentStatement myAssignment; - private final List myReferenceExpressions; + @Nullable private final GoCompositeLit myCompositeLit; + @NotNull private final GoAssignmentStatement myAssignment; + @NotNull private final List myReferenceExpressions; + @NotNull private final GoStatement myStructDeclaration; + @NotNull private final GoVarDefinition myStructDefinition; public Data(@NotNull GoAssignmentStatement assignment, - @NotNull GoCompositeLit compositeLit, - @NotNull List referenceExpressions) { + @Nullable GoCompositeLit compositeLit, + @NotNull List referenceExpressions, + @NotNull GoStatement structDeclaration, + @NotNull GoVarDefinition structDefinition) { myCompositeLit = compositeLit; myAssignment = assignment; myReferenceExpressions = referenceExpressions; - } - - public GoCompositeLit getCompositeLit() { - return myCompositeLit; - } - - public GoAssignmentStatement getAssignment() { - return myAssignment; - } - - public List getReferenceExpressions() { - return myReferenceExpressions; + myStructDeclaration = structDeclaration; + myStructDefinition = structDefinition; } } } + + diff --git a/src/com/goide/psi/impl/GoElementFactory.java b/src/com/goide/psi/impl/GoElementFactory.java index a9c4939ad2..d81feb2471 100644 --- a/src/com/goide/psi/impl/GoElementFactory.java +++ b/src/com/goide/psi/impl/GoElementFactory.java @@ -266,4 +266,10 @@ public static GoTypeDeclaration createTypeDeclaration(@NotNull Project project, GoFile file = createFileFromText(project, "package a; type " + name + " " + type.getText()); return PsiTreeUtil.findChildOfType(file, GoTypeDeclaration.class); } + + @NotNull + public static GoCompositeLit createCompositeLit(@NotNull Project project, @NotNull GoType type) { + GoFile file = createFileFromText(project, "package a; var _ = " + type.getText() + "{}"); + return PsiTreeUtil.findChildOfType(file, GoCompositeLit.class); + } } \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/fieldOfAnonymousField.go b/testData/intentions/move-to-struct-initialization/fieldOfAnonymousField.go new file mode 100644 index 0000000000..2b0fe6fa3f --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/fieldOfAnonymousField.go @@ -0,0 +1,15 @@ +package main + +type S struct { + B +} + +type B struct { + bar string +} + +func main() { + var s S + s.bar = "bar" + print(s.bar) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/notStructDeclaration.go b/testData/intentions/move-to-struct-initialization/notStructDeclaration.go new file mode 100644 index 0000000000..03cfe90a2a --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/notStructDeclaration.go @@ -0,0 +1,11 @@ +package main + +type S struct { + foo string +} + +func main() { + var s string + s.foo = "bar" + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/parensStructDeclaration-after.go b/testData/intentions/move-to-struct-initialization/parensStructDeclaration-after.go new file mode 100644 index 0000000000..18f6832ca8 --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/parensStructDeclaration-after.go @@ -0,0 +1,11 @@ +package main + +type S struct { + foo string +} + +func main() { + s := S{foo: "bar"} + + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/parensStructDeclaration.go b/testData/intentions/move-to-struct-initialization/parensStructDeclaration.go new file mode 100644 index 0000000000..5f78e6c2fd --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/parensStructDeclaration.go @@ -0,0 +1,11 @@ +package main + +type S struct { + foo string +} + +func main() { + var (s S) + s.foo = "bar" + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/structDeclaration-after.go b/testData/intentions/move-to-struct-initialization/structDeclaration-after.go new file mode 100644 index 0000000000..18f6832ca8 --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/structDeclaration-after.go @@ -0,0 +1,11 @@ +package main + +type S struct { + foo string +} + +func main() { + s := S{foo: "bar"} + + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/structDeclaration.go b/testData/intentions/move-to-struct-initialization/structDeclaration.go new file mode 100644 index 0000000000..a5ac8a3ba4 --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/structDeclaration.go @@ -0,0 +1,11 @@ +package main + +type S struct { + foo string +} + +func main() { + var s S + s.foo = "bar" + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/structDeclarationMultipleExpressions.go b/testData/intentions/move-to-struct-initialization/structDeclarationMultipleExpressions.go new file mode 100644 index 0000000000..525fe58a61 --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/structDeclarationMultipleExpressions.go @@ -0,0 +1,11 @@ +package main + +type S struct { + foo string +} + +func main() { + var s, b S + s.foo = "bar" + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/structDeclarationWithEmptyInit-after.go b/testData/intentions/move-to-struct-initialization/structDeclarationWithEmptyInit-after.go new file mode 100644 index 0000000000..6a7a17315e --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/structDeclarationWithEmptyInit-after.go @@ -0,0 +1,11 @@ +package main + +type S struct { + foo string +} + +func main() { + var s S = S{foo: "bar"} + + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/structDeclarationWithEmptyInit.go b/testData/intentions/move-to-struct-initialization/structDeclarationWithEmptyInit.go new file mode 100644 index 0000000000..faea7a738a --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/structDeclarationWithEmptyInit.go @@ -0,0 +1,11 @@ +package main + +type S struct { + foo string +} + +func main() { + var s S = S{} + s.foo = "bar" + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/structDeclarationWithNonEmptyInit-after.go b/testData/intentions/move-to-struct-initialization/structDeclarationWithNonEmptyInit-after.go new file mode 100644 index 0000000000..7da47a03be --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/structDeclarationWithNonEmptyInit-after.go @@ -0,0 +1,12 @@ +package main + +type S struct { + foo string + bar string +} + +func main() { + var s S = S{bar: "a", foo: "bar"} + + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/structDeclarationWithNonEmptyInit.go b/testData/intentions/move-to-struct-initialization/structDeclarationWithNonEmptyInit.go new file mode 100644 index 0000000000..64441d8677 --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/structDeclarationWithNonEmptyInit.go @@ -0,0 +1,12 @@ +package main + +type S struct { + foo string + bar string +} + +func main() { + var s S = S{bar: "a"} + s.foo = "bar" + print(s.foo) +} \ No newline at end of file diff --git a/testData/intentions/move-to-struct-initialization/structDeclarationWrongQualifier.go b/testData/intentions/move-to-struct-initialization/structDeclarationWrongQualifier.go new file mode 100644 index 0000000000..ef50a9c4ec --- /dev/null +++ b/testData/intentions/move-to-struct-initialization/structDeclarationWrongQualifier.go @@ -0,0 +1,12 @@ +package main + +type S struct { + foo string +} + +func main() { + var s S + var b S + s.foo = "bar" + print(b.foo) +} \ No newline at end of file diff --git a/tests/com/goide/quickfix/GoMoveToStructInitializationIntentionTest.java b/tests/com/goide/quickfix/GoMoveToStructInitializationIntentionTest.java index 84fe19eda1..e8750cdf0b 100644 --- a/tests/com/goide/quickfix/GoMoveToStructInitializationIntentionTest.java +++ b/tests/com/goide/quickfix/GoMoveToStructInitializationIntentionTest.java @@ -50,10 +50,15 @@ protected String getBasePath() { public void testMultipleFieldsPartlyAssigned() { doTest(); } public void testWithParens() { doTest(); } public void testFieldExtractedFromParens() { doTest(); } + public void testStructDeclaration() { doTest(); } + public void testParensStructDeclaration() { doTest(); } + public void testStructDeclarationWithEmptyInit() { doTest(); } + public void testStructDeclarationWithNonEmptyInit() { doTest(); } public void testDuplicateFields() { doTest(); } public void testMultiReturnFunction() { doTestNoFix(); } public void testWrongStruct() { doTestNoFix(); } + public void testNotStructDeclaration() { doTestNoFix(); } public void testExistingDeclaration() { doTestNoFix(); } public void testNotExistingField() { doTestNoFix(); } public void testJustAssignedVarWrongCaret() { doTestNoFix(); } @@ -61,5 +66,8 @@ protected String getBasePath() { public void testJustInitializedVarWrongCaret() { doTestNoFix(); } public void testJustAssignedVarBothParens() { doTestNoFix(); } public void testJustAssignedFieldParens() { doTestNoFix(); } + public void testStructDeclarationMultipleExpressions() { doTestNoFix(); } + public void testStructDeclarationWrongQualifier() { doTestNoFix(); } + public void testFieldOfAnonymousField() { doTestNoFix(); } }