-
Notifications
You must be signed in to change notification settings - Fork 569
Move to struct init intention: handle struct declaration case #2816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you need this : struct " + typeText + " {};
?
GoStatement previousStatement = PsiTreeUtil.getPrevSiblingOfType(assignment, GoStatement.class); | ||
if (previousStatement instanceof GoSimpleStatement) { | ||
return getStructLiteral(fieldReferenceExpression, (GoSimpleStatement)previousStatement); | ||
} | ||
if (previousStatement instanceof GoAssignmentStatement) { | ||
return getStructLiteral(fieldReferenceExpression, (GoAssignmentStatement)previousStatement); | ||
} | ||
if (previousStatement instanceof GoStatementImpl) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you know that definition
is GoVarDefinition
use == or equals
@@ -193,6 +224,19 @@ private static GoCompositeLit getStructLiteral(@NotNull GoReferenceExpression fi | |||
return ObjectUtils.tryCast(compositeLit, GoCompositeLit.class); | |||
} | |||
|
|||
@Nullable | |||
private static GoCompositeLit getStructLiteral(@NotNull GoStatementImpl statement, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't create struct literal at this place. Because method name is getData
and it's called in isAviable
@@ -239,6 +286,10 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement | |||
Data data = getData(element); | |||
if (data == null) return; | |||
moveFieldReferenceExpressions(data); | |||
|
|||
if (!data.needReplaceDeclarationWithShortVar()) return; | |||
data.getStructDeclaration() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NPE can be there
@@ -292,5 +352,19 @@ public GoAssignmentStatement getAssignment() { | |||
public List<GoReferenceExpression> getReferenceExpressions() { | |||
return myReferenceExpressions; | |||
} | |||
|
|||
public GoStatement getStructDeclaration() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
annotate with @NotNull all getters please
@NotNull List<GoReferenceExpression> referenceExpressions, | ||
@Nullable GoStatement structDeclaration, | ||
@Nullable String structVarName, | ||
boolean needReplaceDeclarationWithShortVar) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.