Move to struct init intention: handle struct declaration case#2816
Move to struct init intention: handle struct declaration case#2816wbars wants to merge 1 commit into
Conversation
| @NotNull | ||
| public static GoCompositeLit createCompositeLit(@NotNull Project project, @NotNull GoType type) { | ||
| String typeText = type.getText(); | ||
| GoFile file = createFileFromText(project, "package a; struct " + typeText + " {}; var _ = " + typeText + "{}"); |
There was a problem hiding this comment.
Why you need this : struct " + typeText + " {}; ?
| if (previousStatement instanceof GoAssignmentStatement) { | ||
| return getStructLiteral(fieldReferenceExpression, (GoAssignmentStatement)previousStatement); | ||
| } | ||
| if (previousStatement instanceof GoStatementImpl) { |
There was a problem hiding this comment.
Use GoStatement interface instead GoStatementImpl
| if (varSpec == null) return null; | ||
| GoType structType = isUnassigned(varSpec) ? definition.getGoType(null) : null; | ||
| return structType != null | ||
| ? GoElementFactory.createCompositeLit(project, structType) |
There was a problem hiding this comment.
You can take project from statement
| if (varSpec == null) return null; | ||
| GoType structType = isUnassigned(varSpec) ? definition.getGoType(null) : null; | ||
| return structType != null | ||
| ? GoElementFactory.createCompositeLit(project, structType) |
There was a problem hiding this comment.
You can take project from statement
| List<GoVarSpec> varSpecs = declaration != null ? declaration.getVarSpecList() : emptyList(); | ||
| GoVarSpec singleVarSpec = varSpecs.size() == 1 ? getFirstItem(varSpecs) : null; | ||
| List<GoVarDefinition> varDefinitions = singleVarSpec != null ? singleVarSpec.getVarDefinitionList() : emptyList(); | ||
| return varDefinitions.size() == 1 && isResolvedTo(definition, getFirstItem(varDefinitions)) ? singleVarSpec : null; |
There was a problem hiding this comment.
If you know that definition is GoVarDefinition use == or equals
| } | ||
|
|
||
| @Nullable | ||
| private static GoCompositeLit getStructLiteral(@NotNull GoStatementImpl statement, |
There was a problem hiding this comment.
It's getOrCreateStructLiteral and better create composite literal more clear.
you check was struct literal created again in getData : isUnassigned(getSingleVarSpecByDefinition(previousStatement, structDefinition)))
|
Force pushed |
|
|
||
| if (!data.needReplaceDeclarationWithShortVar()) return; | ||
| data.getStructDeclaration() | ||
| .replace(GoElementFactory.createShortVarDeclarationStatement(project, data.getStructVarName(), data.getCompositeLit().getText())); |
There was a problem hiding this comment.
extract variable for more readability, please
| boolean needReplaceDeclarationWithShortVar = isUnassigned(getSingleVarSpecByDefinition(previousStatement, structDefinition)); | ||
|
|
||
| GoCompositeLit compositeLit = needReplaceDeclarationWithShortVar | ||
| ? createStructLiteral(structDefinition, previousStatement.getProject()) |
There was a problem hiding this comment.
You shouldn't create struct literal at this place. Because method name is getData and it's called in isAviable
| moveFieldReferenceExpressions(data); | ||
|
|
||
| if (!data.needReplaceDeclarationWithShortVar()) return; | ||
| data.getStructDeclaration() |
| return myReferenceExpressions; | ||
| } | ||
|
|
||
| public GoStatement getStructDeclaration() { |
| @NotNull List<GoReferenceExpression> referenceExpressions, | ||
| @Nullable GoStatement structDeclaration, | ||
| @Nullable String structVarName, | ||
| boolean needReplaceDeclarationWithShortVar) { |
There was a problem hiding this comment.
I think you can get rid of needReplaceDeclarationWithShortVar field by giving null compositeLit
f8a68be to
fdb56e7
Compare
|
Updated |
57a8f43 to
7640c4f
Compare
Replace single struct declaration with short var declaration, or add to existing struct literal in statement
|
@grenki Still can't fully beat naming problem, I guess :) Thanks for the review. |
Replace single struct declaration with short var declaration, or add to existing struct literal in statement.