Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

wbars
Copy link
Contributor

@wbars wbars commented Nov 1, 2016

Replace single struct declaration with short var declaration, or add to existing struct literal in statement.

@wbars wbars changed the title Move to struct declaration: handle struct declaration case Move to struct init intention: handle struct declaration case Nov 1, 2016
@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 + "{}");
Copy link
Contributor

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) {
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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;
Copy link
Contributor

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,
Copy link
Contributor

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)))

@wbars
Copy link
Contributor Author

wbars commented Nov 3, 2016

Force pushed


if (!data.needReplaceDeclarationWithShortVar()) return;
data.getStructDeclaration()
.replace(GoElementFactory.createShortVarDeclarationStatement(project, data.getStructVarName(), data.getCompositeLit().getText()));
Copy link
Contributor

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())
Copy link
Contributor

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()
Copy link
Contributor

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() {
Copy link
Contributor

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) {
Copy link
Contributor

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

@wbars wbars force-pushed the move-to-struct branch 2 times, most recently from f8a68be to fdb56e7 Compare November 4, 2016 07:32
@wbars
Copy link
Contributor Author

wbars commented Nov 4, 2016

Updated

@wbars wbars force-pushed the move-to-struct branch 4 times, most recently from 57a8f43 to 7640c4f Compare November 6, 2016 09:56
Replace single struct declaration with short var declaration, or add to existing struct literal in statement
@grenki
Copy link
Contributor

grenki commented Nov 8, 2016

@wbars Thank you for this work.

I make few changes as renaming and fix notnull annotations, fix case for field of anonymous field and create another pull reqest. You can see changes here: #2822

@wbars
Copy link
Contributor Author

wbars commented Nov 8, 2016

@grenki Still can't fully beat naming problem, I guess :) Thanks for the review.

@wbars wbars closed this Nov 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants