diff --git a/src/com/goide/intentions/GoMoveToStructInitializationIntention.java b/src/com/goide/intentions/GoMoveToStructInitializationIntention.java index 0040439367..9fe19ce0b2 100644 --- a/src/com/goide/intentions/GoMoveToStructInitializationIntention.java +++ b/src/com/goide/intentions/GoMoveToStructInitializationIntention.java @@ -60,12 +60,21 @@ 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 previousStatement = assignment != null ? PsiTreeUtil.getPrevSiblingOfType(assignment, GoStatement.class) : null; + GoReferenceExpression selectedFieldReferenceExpression = + previousStatement != null ? getFieldReferenceExpression(element, assignment, previousStatement) : null; + if (selectedFieldReferenceExpression == null) return null; - List references = getUninitializedSingleFieldReferences(assignment, selectedFieldReference, compositeLit); - return !references.isEmpty() ? new Data(assignment, compositeLit, references) : null; + GoVarDefinition structDefinition = getDefinition(selectedFieldReferenceExpression); + boolean needReplaceDeclarationWithShortVar = isUnassigned(getSingleVarSpecByDefinition(previousStatement, structDefinition)); + + GoCompositeLit compositeLit = structDefinition != null ? getStructLiteralByDefinition(structDefinition, previousStatement) : null; + GoNamedElement field = ObjectUtils.tryCast(selectedFieldReferenceExpression.resolve(), GoNamedElement.class); + if (compositeLit == null && !needReplaceDeclarationWithShortVar || !hasStructTypeWithField(structDefinition, field)) return null; + + List references = + getUninitializedSingleFieldReferences(assignment, previousStatement, structDefinition, compositeLit); + return !references.isEmpty() ? new Data(assignment, compositeLit, references, previousStatement, structDefinition) : null; } @Nullable @@ -77,17 +86,29 @@ private static GoAssignmentStatement getValidAssignmentParent(@Nullable PsiEleme @Nullable private static GoReferenceExpression getFieldReferenceExpression(@NotNull PsiElement selectedElement, - @NotNull GoAssignmentStatement assignment) { + @NotNull GoAssignmentStatement assignment, + @NotNull GoStatement previousStatement) { GoReferenceExpression selectedReferenceExpression = PsiTreeUtil.getTopmostParentOfType(selectedElement, GoReferenceExpression.class); if (isFieldReferenceExpression(selectedReferenceExpression)) { - return !isAssignedInPreviousStatement(selectedReferenceExpression, assignment) ? selectedReferenceExpression : null; + return !isAssignedInStatement(getRightExpression(selectedReferenceExpression, assignment), previousStatement) + ? selectedReferenceExpression : null; } List fieldReferenceExpressions = getFieldReferenceExpressions(assignment); - if (exists(fieldReferenceExpressions, expression -> isAssignedInPreviousStatement(expression, assignment))) return null; + if (exists(fieldReferenceExpressions, + expression -> isAssignedInStatement(getRightExpression(expression, assignment), previousStatement))) { + return null; + } + + Set resolvedDefinition = map2Set(fieldReferenceExpressions, GoMoveToStructInitializationIntention::getDefinition); + return resolvedDefinition.size() == 1 ? getFirstItem(fieldReferenceExpressions) : null; + } - Set resolvedFields = map2Set(fieldReferenceExpressions, GoMoveToStructInitializationIntention::resolveQualifier); - return resolvedFields.size() == 1 ? getFirstItem(fieldReferenceExpressions) : null; + @Nullable + @Contract("null -> null") + private static GoVarDefinition getDefinition(@Nullable GoReferenceExpression referenceExpressions) { + GoReferenceExpression qualifier = referenceExpressions != null ? referenceExpressions.getQualifier() : null; + return qualifier != null ? ObjectUtils.tryCast(qualifier.resolve(), GoVarDefinition.class) : null; } @NotNull @@ -97,11 +118,27 @@ 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 getSingleVarSpecByDefinition(@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") @@ -114,14 +151,16 @@ 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 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,72 +168,63 @@ 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, @Nullable 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); + @NotNull GoStatement previousStatement, + @Nullable GoVarDefinition definition, + @Nullable GoCompositeLit compositeLit) { List uninitializedFieldReferencesByQualifier = - filter(getUninitializedFieldReferenceExpressions(assignment, compositeLit), e -> isResolvedTo(e.getQualifier(), resolve)); + filter(getUninitializedFieldReferenceExpressions(assignment, compositeLit, previousStatement), + element -> isResolvedTo(element.getQualifier(), definition)); MultiMap resolved = groupBy(uninitializedFieldReferencesByQualifier, 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); + private static GoCompositeLit getStructLiteralByDefinition(@NotNull GoVarDefinition definition, @NotNull GoStatement statement) { + if (statement instanceof GoSimpleStatement) { + return getStructLiteral(definition, (GoSimpleStatement)statement); } - if (previousStatement instanceof GoAssignmentStatement) { - return getStructLiteral(fieldReferenceExpression, (GoAssignmentStatement)previousStatement); + if (statement instanceof GoAssignmentStatement) { + return getStructLiteral(definition, (GoAssignmentStatement)statement); } - return null; + return getStructLiteral(definition, statement); } @Nullable - private static GoCompositeLit getStructLiteral(@NotNull GoReferenceExpression fieldReferenceExpression, - @NotNull GoSimpleStatement structDeclaration) { - GoShortVarDeclaration varDeclaration = structDeclaration.getShortVarDeclaration(); - if (varDeclaration == null) return null; - - PsiElement resolve = resolveQualifier(fieldReferenceExpression); - GoVarDefinition structVarDefinition = find(varDeclaration.getVarDefinitionList(), definition -> resolve == definition); - return structVarDefinition != null ? ObjectUtils.tryCast(structVarDefinition.getValue(), GoCompositeLit.class) : null; + @Contract("null, _ -> null") + private static GoCompositeLit getStructLiteral(@Nullable GoVarDefinition definition, @NotNull GoSimpleStatement declaration) { + GoShortVarDeclaration varDeclaration = definition != null ? declaration.getShortVarDeclaration() : null; + return varDeclaration != null && containsIdentity(varDeclaration.getVarDefinitionList(), definition) + ? ObjectUtils.tryCast(definition.getValue(), GoCompositeLit.class) : null; } @Nullable - private static PsiElement resolveQualifier(@NotNull GoReferenceExpression fieldReferenceExpression) { - GoReferenceExpression qualifier = fieldReferenceExpression.getQualifier(); - return qualifier != null ? qualifier.resolve() : null; + private static GoCompositeLit getStructLiteral(@NotNull GoVarDefinition definition, @NotNull GoAssignmentStatement structAssignment) { + GoExpression structReferenceExpression = find(structAssignment.getLeftHandExprList().getExpressionList(), + expression -> isResolvedTo(expression, definition)); + GoExpression compositeLit = + structReferenceExpression != null ? GoPsiImplUtil.getRightExpression(structAssignment, structReferenceExpression) : null; + return ObjectUtils.tryCast(compositeLit, GoCompositeLit.class); } @Nullable - private static GoCompositeLit getStructLiteral(@NotNull GoReferenceExpression fieldReferenceExpression, - @NotNull GoAssignmentStatement structAssignment) { - GoVarDefinition varDefinition = ObjectUtils.tryCast(resolveQualifier(fieldReferenceExpression), GoVarDefinition.class); - PsiElement field = fieldReferenceExpression.resolve(); - if (varDefinition == null || !isFieldDefinition(field) || !hasStructTypeWithField(varDefinition, (GoNamedElement)field)) { - return null; - } - - GoExpression structReferenceExpression = find(structAssignment.getLeftHandExprList().getExpressionList(), - expression -> isResolvedTo(expression, varDefinition)); - if (structReferenceExpression == null) return null; - GoExpression compositeLit = GoPsiImplUtil.getRightExpression(structAssignment, structReferenceExpression); - return ObjectUtils.tryCast(compositeLit, GoCompositeLit.class); + @Contract("null, _ -> null") + private static GoCompositeLit getStructLiteral(@Nullable GoVarDefinition definition, @NotNull GoStatement statement) { + GoVarSpec varSpec = definition != null ? getSingleVarSpecByDefinition(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); + @Contract("_, null -> false; null, !null -> false") + private static boolean hasStructTypeWithField(@Nullable GoVarDefinition definition, @Nullable GoNamedElement field) { + GoType type = field != null && definition != null ? definition.getGoType(null) : null; GoStructType structType = type != null ? ObjectUtils.tryCast(type.getUnderlyingType(), GoStructType.class) : null; return structType != null && PsiTreeUtil.isAncestor(structType, field, true); } @@ -207,15 +237,19 @@ private static boolean isFieldInitialization(@NotNull GoElement element, @NotNul @NotNull private static List getUninitializedFieldReferenceExpressions(@NotNull GoAssignmentStatement assignment, - @NotNull GoCompositeLit structLiteral) { - return filter(getFieldReferenceExpressions(assignment), expression -> - isUninitializedFieldReferenceExpression(expression, structLiteral) && !isAssignedInPreviousStatement(expression, assignment)); + @Nullable GoCompositeLit structLiteral, + @NotNull GoStatement previousStatement) { + return filter(getFieldReferenceExpressions(assignment), + expression -> isUninitializedFieldReferenceExpression(expression, structLiteral) && + !isAssignedInStatement(getRightExpression(expression, assignment), previousStatement)); } - @Contract("null, _-> false") + @Contract("null, _-> false; !null, null -> true") private static boolean isUninitializedFieldReferenceExpression(@Nullable GoReferenceExpression fieldReferenceExpression, - @NotNull GoCompositeLit structLiteral) { + @Nullable GoCompositeLit structLiteral) { if (fieldReferenceExpression == null) return false; + if (structLiteral == null) return true; + GoLiteralValue literalValue = structLiteral.getLiteralValue(); PsiElement resolve = fieldReferenceExpression.resolve(); return literalValue != null && isFieldDefinition(resolve) && @@ -238,19 +272,36 @@ 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 needReplaceDeclarationWithShortVar = data.getCompositeLit() == null; + GoCompositeLit compositeLit = + needReplaceDeclarationWithShortVar ? createStructLiteral(data.getStructDefinition(), project) : data.getCompositeLit(); + if (compositeLit == null || needReplaceDeclarationWithShortVar && data.getStructDeclaration() == null) return; + + moveFieldReferenceExpressions(data.getReferenceExpressions(), compositeLit.getLiteralValue(), data.getAssignment()); + if (!needReplaceDeclarationWithShortVar) return; + GoStatement shortVarStatement = + GoElementFactory.createShortVarDeclarationStatement(project, data.getStructDefinition().getText(), compositeLit.getText()); + data.getStructDeclaration().replace(shortVarStatement); } - private static void moveFieldReferenceExpressions(@NotNull Data data) { - GoLiteralValue literalValue = data.getCompositeLit().getLiteralValue(); - if (literalValue == null) return; - for (GoReferenceExpression expression : data.getReferenceExpressions()) { + @Nullable + private static GoCompositeLit createStructLiteral(@NotNull GoVarDefinition definition, @NotNull Project project) { + GoType type = definition.getGoType(null); + return type != null ? GoElementFactory.createCompositeLit(project, type) : null; + } + + private static void moveFieldReferenceExpressions(@NotNull List referenceExpressions, + @Nullable GoLiteralValue literalValue, + @NotNull GoAssignmentStatement parentAssignment) { + if (literalValue == null) return; + 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()); } } @@ -272,25 +323,46 @@ private static class Data { private final GoCompositeLit myCompositeLit; private final GoAssignmentStatement myAssignment; private final List myReferenceExpressions; + private final GoStatement myStructDeclaration; + private final GoVarDefinition myStructDefinition; public Data(@NotNull GoAssignmentStatement assignment, - @NotNull GoCompositeLit compositeLit, - @NotNull List referenceExpressions) { + @Nullable GoCompositeLit compositeLit, + @NotNull List referenceExpressions, + @Nullable GoStatement structDeclaration, + @NotNull GoVarDefinition structDefinition) { myCompositeLit = compositeLit; myAssignment = assignment; myReferenceExpressions = referenceExpressions; + myStructDeclaration = structDeclaration; + myStructDefinition = structDefinition; } + @Nullable public GoCompositeLit getCompositeLit() { return myCompositeLit; } + @NotNull public GoAssignmentStatement getAssignment() { return myAssignment; } + @NotNull public List getReferenceExpressions() { return myReferenceExpressions; } + + @Nullable + public GoStatement getStructDeclaration() { + return myStructDeclaration; + } + + @NotNull + public GoVarDefinition getStructDefinition() { + return myStructDefinition; + } } } + + 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/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..3c7a0123c6 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,7 @@ protected String getBasePath() { public void testJustInitializedVarWrongCaret() { doTestNoFix(); } public void testJustAssignedVarBothParens() { doTestNoFix(); } public void testJustAssignedFieldParens() { doTestNoFix(); } + public void testStructDeclarationMultipleExpressions() { doTestNoFix(); } + public void testStructDeclarationWrongQualifier() { doTestNoFix(); } }