Skip to content

Commit 87177eb

Browse files
committed
Rework GoStructInitializationInspection
fixes #2819
1 parent eb669fc commit 87177eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+642
-95
lines changed

src/com/goide/completion/GoStructLiteralCompletion.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
import com.goide.psi.*;
2020
import com.goide.psi.impl.GoPsiImplUtil;
2121
import com.intellij.psi.PsiElement;
22-
import com.intellij.util.ObjectUtils;
2322
import com.intellij.util.containers.ContainerUtil;
24-
import org.jetbrains.annotations.Contract;
2523
import org.jetbrains.annotations.NotNull;
2624
import org.jetbrains.annotations.Nullable;
2725

@@ -61,8 +59,8 @@ enum Variants {
6159

6260
@NotNull
6361
static Variants allowedVariants(@Nullable GoReferenceExpression structFieldReference) {
64-
GoValue value = parent(structFieldReference, GoValue.class);
65-
GoElement element = parent(value, GoElement.class);
62+
GoValue value = GoPsiTreeUtil.getDirectParentOfType(structFieldReference, GoValue.class);
63+
GoElement element = GoPsiTreeUtil.getDirectParentOfType(value, GoElement.class);
6664
if (element != null && element.getKey() != null) {
6765
return Variants.NONE;
6866
}
@@ -75,7 +73,7 @@ static Variants allowedVariants(@Nullable GoReferenceExpression structFieldRefer
7573
boolean hasValueInitializers = false;
7674
boolean hasFieldValueInitializers = false;
7775

78-
GoLiteralValue literalValue = parent(element, GoLiteralValue.class);
76+
GoLiteralValue literalValue = GoPsiTreeUtil.getDirectParentOfType(element, GoLiteralValue.class);
7977
List<GoElement> fieldInitializers = literalValue != null ? literalValue.getElementList() : Collections.emptyList();
8078
for (GoElement initializer : fieldInitializers) {
8179
if (initializer == element) {
@@ -105,9 +103,4 @@ static Set<String> alreadyAssignedFields(@Nullable GoLiteralValue literal) {
105103
return identifier != null ? identifier.getText() : null;
106104
});
107105
}
108-
109-
@Contract("null,_->null")
110-
private static <T> T parent(@Nullable PsiElement of, @NotNull Class<T> parentClass) {
111-
return ObjectUtils.tryCast(of != null ? of.getParent() : null, parentClass);
112-
}
113106
}

src/com/goide/inspections/GoStructInitializationInspection.java

+131-45
Original file line numberDiff line numberDiff line change
@@ -22,94 +22,180 @@
2222
import com.goide.util.GoUtil;
2323
import com.intellij.codeInspection.*;
2424
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
25-
import com.intellij.openapi.progress.ProgressManager;
2625
import com.intellij.openapi.project.Project;
26+
import com.intellij.openapi.util.Comparing;
2727
import com.intellij.openapi.util.InvalidDataException;
2828
import com.intellij.openapi.util.WriteExternalException;
2929
import com.intellij.psi.PsiElement;
3030
import com.intellij.psi.util.PsiTreeUtil;
31-
import com.intellij.util.containers.ContainerUtil;
31+
import com.intellij.util.ObjectUtils;
3232
import org.jdom.Element;
33+
import org.jetbrains.annotations.Contract;
3334
import org.jetbrains.annotations.NotNull;
3435
import org.jetbrains.annotations.Nullable;
3536

3637
import javax.swing.*;
3738
import java.util.List;
3839

40+
import static com.intellij.util.containers.ContainerUtil.*;
41+
import static java.util.stream.Collectors.toList;
42+
import static java.util.stream.IntStream.range;
43+
3944
public class GoStructInitializationInspection extends GoInspectionBase {
40-
public static final String REPLACE_WITH_NAMED_STRUCT_FIELD_FIX_NAME = "Replace with named struct field";
45+
public static final String REPLACE_WITH_NAMED_STRUCT_FIELD_FIX_NAME = "Replace with named struct fields";
46+
private static final GoReplaceWithNamedStructFieldQuickFix QUICK_FIX = new GoReplaceWithNamedStructFieldQuickFix();
4147
public boolean reportLocalStructs;
4248
/**
43-
* @deprecated use reportLocalStructs
49+
* @deprecated use {@link #reportLocalStructs}
4450
*/
4551
@SuppressWarnings("WeakerAccess") public Boolean reportImportedStructs;
4652

4753
@NotNull
4854
@Override
4955
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
5056
return new GoVisitor() {
57+
5158
@Override
52-
public void visitLiteralValue(@NotNull GoLiteralValue o) {
53-
if (PsiTreeUtil.getParentOfType(o, GoReturnStatement.class, GoShortVarDeclaration.class, GoAssignmentStatement.class) == null) {
54-
return;
55-
}
56-
PsiElement parent = o.getParent();
57-
GoType refType = GoPsiImplUtil.getLiteralType(parent, false);
58-
if (refType instanceof GoStructType) {
59-
processStructType(holder, o, (GoStructType)refType);
60-
}
59+
public void visitLiteralValue(@NotNull GoLiteralValue literalValue) {
60+
GoStructType structType = getLiteralStructType(literalValue);
61+
if (structType == null || !isStructImportedOrLocalAllowed(structType, literalValue)) return;
62+
63+
List<GoElement> elements = literalValue.getElementList();
64+
List<String> keys = getKeys(elements);
65+
if (!areKeysMatchesDefinitions(keys, getFieldDefinitionsNames(structType))) return;
66+
registerProblemsForElementsWithoutKeys(elements, keys, holder);
6167
}
6268
};
6369
}
6470

65-
@Override
66-
public JComponent createOptionsPanel() {
67-
return new SingleCheckboxOptionsPanel("Report for local type definitions as well", this, "reportLocalStructs");
71+
@Contract("null -> null")
72+
private static GoStructType getLiteralStructType(@Nullable GoLiteralValue literalValue) {
73+
GoCompositeLit parentLit = GoPsiTreeUtil.getDirectParentOfType(literalValue, GoCompositeLit.class);
74+
if (parentLit != null && !isStructLit(parentLit)) return null;
75+
76+
GoStructType litType = ObjectUtils.tryCast(GoPsiImplUtil.getLiteralType(literalValue, parentLit == null), GoStructType.class);
77+
String definitionName = getFieldDefinitionName(GoPsiTreeUtil.getDirectParentOfType(literalValue, GoValue.class));
78+
return definitionName != null && litType != null ? getFieldDefinitionType(litType, definitionName) : litType;
6879
}
6980

70-
private void processStructType(@NotNull ProblemsHolder holder, @NotNull GoLiteralValue element, @NotNull GoStructType structType) {
71-
if (reportLocalStructs || !GoUtil.inSamePackage(structType.getContainingFile(), element.getContainingFile())) {
72-
processLiteralValue(holder, element, structType.getFieldDeclarationList());
73-
}
81+
@Nullable
82+
private static String getFieldDefinitionName(@Nullable GoValue value) {
83+
GoKey key = PsiTreeUtil.getChildOfType(GoPsiTreeUtil.getDirectParentOfType(value, GoElement.class), GoKey.class);
84+
GoFieldName fieldName = key != null ? key.getFieldName() : null;
85+
return fieldName != null ? fieldName.getText() : null;
7486
}
7587

76-
private static void processLiteralValue(@NotNull ProblemsHolder holder,
77-
@NotNull GoLiteralValue o,
78-
@NotNull List<GoFieldDeclaration> fields) {
79-
List<GoElement> vals = o.getElementList();
80-
for (int elemId = 0; elemId < vals.size(); elemId++) {
81-
ProgressManager.checkCanceled();
82-
GoElement element = vals.get(elemId);
83-
if (element.getKey() == null && elemId < fields.size()) {
84-
String structFieldName = getFieldName(fields.get(elemId));
85-
LocalQuickFix[] fixes = structFieldName != null ? new LocalQuickFix[]{new GoReplaceWithNamedStructFieldQuickFix(structFieldName)}
86-
: LocalQuickFix.EMPTY_ARRAY;
87-
holder.registerProblem(element, "Unnamed field initialization", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, fixes);
88-
}
89-
}
88+
@Nullable
89+
private static GoStructType getFieldDefinitionType(@NotNull GoStructType structType, @NotNull String definitionName) {
90+
GoFieldDefinition fieldDefinition = getDefinition(structType, definitionName);
91+
if (fieldDefinition != null) return ObjectUtils.tryCast(getUnderlyingType(fieldDefinition), GoStructType.class);
92+
93+
GoAnonymousFieldDefinition anonymousDefinition = getAnonDefinition(structType, definitionName);
94+
return anonymousDefinition != null ? ObjectUtils
95+
.tryCast(GoPsiImplUtil.getUnderlyingType(anonymousDefinition.getType()), GoStructType.class) : null;
9096
}
9197

9298
@Nullable
93-
private static String getFieldName(@NotNull GoFieldDeclaration declaration) {
94-
List<GoFieldDefinition> list = declaration.getFieldDefinitionList();
95-
GoFieldDefinition fieldDefinition = ContainerUtil.getFirstItem(list);
96-
return fieldDefinition != null ? fieldDefinition.getIdentifier().getText() : null;
99+
private static GoType getUnderlyingType(@NotNull GoFieldDefinition fieldDefinition) {
100+
GoType type = fieldDefinition.getGoType(null);
101+
return type != null ? GoPsiImplUtil.getUnderlyingType(type) : null;
102+
}
103+
104+
@Nullable
105+
private static GoAnonymousFieldDefinition getAnonDefinition(@NotNull GoStructType type, @NotNull String definitionName) {
106+
return type.getFieldDeclarationList().stream()
107+
.map(GoFieldDeclaration::getAnonymousFieldDefinition)
108+
.filter(definition -> definition != null && Comparing.equal(definitionName, definition.getName()))
109+
.findAny().orElse(null);
110+
}
111+
112+
@Nullable
113+
private static GoFieldDefinition getDefinition(@NotNull GoStructType type, @NotNull String definitionName) {
114+
return type.getFieldDeclarationList().stream().flatMap(declaration -> declaration.getFieldDefinitionList().stream())
115+
.filter(definition -> Comparing.equal(definition.getName(), definitionName))
116+
.findAny().orElse(null);
117+
}
118+
119+
120+
private static boolean isStructLit(@NotNull GoCompositeLit parentLit) {
121+
GoType type = parentLit.getGoType(null);
122+
return type != null && GoPsiImplUtil.getUnderlyingType(type) instanceof GoStructType;
123+
}
124+
125+
private boolean isStructImportedOrLocalAllowed(@NotNull GoStructType structType, @NotNull GoLiteralValue literalValue) {
126+
return reportLocalStructs || !GoUtil.inSamePackage(structType.getContainingFile(), literalValue.getContainingFile());
127+
}
128+
129+
@NotNull
130+
private static List<String> getKeys(@NotNull List<GoElement> elements) {
131+
return map(elements, element -> {
132+
GoKey key = element.getKey();
133+
return key != null ? key.getText() : null;
134+
});
135+
}
136+
137+
private static boolean areKeysMatchesDefinitions(@NotNull List<String> keys, @NotNull List<String> fieldDefinitionsNames) {
138+
return range(0, keys.size()).allMatch(i -> isNullOrEqual(keys.get(i), GoPsiImplUtil.getByIndex(fieldDefinitionsNames, i)));
139+
}
140+
141+
@Contract("null, _ -> true")
142+
private static boolean isNullOrEqual(@Nullable Object o, @Nullable Object objectToCompare) {
143+
return o == null || Comparing.equal(o, objectToCompare);
144+
}
145+
146+
@NotNull
147+
private static List<String> getFieldDefinitionsNames(@Nullable GoStructType type) {
148+
return type != null ? type.getFieldDeclarationList().stream()
149+
.flatMap(declaration -> getFieldDefinitionsNames(declaration).stream())
150+
.collect(toList()) : emptyList();
151+
}
152+
153+
@NotNull
154+
private static List<String> getFieldDefinitionsNames(@NotNull GoFieldDeclaration declaration) {
155+
GoAnonymousFieldDefinition definition = declaration.getAnonymousFieldDefinition();
156+
return definition != null ? list(definition.getName()) : map(declaration.getFieldDefinitionList(), GoNamedElement::getName);
157+
}
158+
159+
private static void registerProblemsForElementsWithoutKeys(@NotNull List<GoElement> elements,
160+
@NotNull List<String> keys,
161+
@NotNull ProblemsHolder holder) {
162+
for (int i = 0; i < elements.size(); i++) {
163+
if (GoPsiImplUtil.getByIndex(keys, i) != null) continue;
164+
holder.registerProblem(elements.get(i), "Unnamed field initializations", ProblemHighlightType.WEAK_WARNING, QUICK_FIX);
165+
}
166+
}
167+
168+
@Override
169+
public JComponent createOptionsPanel() {
170+
return new SingleCheckboxOptionsPanel("Report for local type definitions as well", this, "reportLocalStructs");
97171
}
98172

99173
private static class GoReplaceWithNamedStructFieldQuickFix extends LocalQuickFixBase {
100-
private String myStructField;
101174

102-
public GoReplaceWithNamedStructFieldQuickFix(@NotNull String structField) {
175+
public GoReplaceWithNamedStructFieldQuickFix() {
103176
super(REPLACE_WITH_NAMED_STRUCT_FIELD_FIX_NAME);
104-
myStructField = structField;
105177
}
106178

107179
@Override
108180
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
109-
PsiElement startElement = descriptor.getStartElement();
110-
if (startElement instanceof GoElement) {
111-
startElement.replace(GoElementFactory.createLiteralValueElement(project, myStructField, startElement.getText()));
112-
}
181+
PsiElement element = ObjectUtils.tryCast(descriptor.getStartElement(), GoElement.class);
182+
GoLiteralValue literal = element != null && element.isValid() ? PsiTreeUtil.getParentOfType(element, GoLiteralValue.class) : null;
183+
184+
List<GoElement> elements = literal != null ? literal.getElementList() : emptyList();
185+
List<String> fieldDefinitionNames = getFieldDefinitionsNames(getLiteralStructType(literal));
186+
if (!areKeysMatchesDefinitions(getKeys(elements), fieldDefinitionNames)) return;
187+
addKeysToElements(project, elements, fieldDefinitionNames);
188+
}
189+
}
190+
191+
private static void addKeysToElements(@NotNull Project project,
192+
@NotNull List<GoElement> elements,
193+
@NotNull List<String> fieldDefinitionNames) {
194+
for (int i = 0; i < elements.size(); i++) {
195+
GoElement element = elements.get(i);
196+
String fieldDefinitionName = GoPsiImplUtil.getByIndex(fieldDefinitionNames, i);
197+
GoValue value = fieldDefinitionName != null && element.getKey() == null ? element.getValue() : null;
198+
if (value != null) element.replace(GoElementFactory.createLiteralValueElement(project, fieldDefinitionName, value.getText()));
113199
}
114200
}
115201

src/com/goide/psi/GoPsiTreeUtil.java

+7
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import com.intellij.psi.stubs.StubElement;
2727
import com.intellij.psi.util.PsiTreeUtil;
2828
import com.intellij.psi.util.PsiUtilCore;
29+
import com.intellij.util.ObjectUtils;
2930
import com.intellij.util.SmartList;
3031
import com.intellij.util.containers.ContainerUtil;
32+
import org.jetbrains.annotations.Contract;
3133
import org.jetbrains.annotations.NotNull;
3234
import org.jetbrains.annotations.Nullable;
3335

@@ -155,5 +157,10 @@ private static PsiElement findNotWhiteSpaceElementAtOffset(@NotNull GoFile file,
155157
}
156158
return element;
157159
}
160+
161+
@Contract("null,_->null")
162+
public static <T> T getDirectParentOfType(@Nullable PsiElement element, @NotNull Class<T> aClass) {
163+
return element != null ? ObjectUtils.tryCast(element.getParent(), aClass) : null;
164+
}
158165
}
159166

src/com/goide/psi/impl/GoElementFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public static GoType createType(@NotNull Project project, @NotNull String text)
256256
return PsiTreeUtil.findChildOfType(file, GoType.class);
257257
}
258258

259-
public static PsiElement createLiteralValueElement(@NotNull Project project, @NotNull String key, @NotNull String value) {
259+
public static GoElement createLiteralValueElement(@NotNull Project project, @NotNull String key, @NotNull String value) {
260260
GoFile file = createFileFromText(project, "package a; var _ = struct { a string } { " + key + ": " + value + " }");
261261
return PsiTreeUtil.findChildOfType(file, GoElement.class);
262262
}

src/com/goide/psi/impl/GoPsiImplUtil.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,12 @@ public static GoType getLiteralType(@Nullable PsiElement context, boolean consid
569569
@Nullable
570570
public static GoValue getParentGoValue(@NotNull PsiElement element) {
571571
PsiElement place = element;
572-
while ((place = PsiTreeUtil.getParentOfType(place, GoLiteralValue.class)) != null) {
572+
do {
573573
if (place.getParent() instanceof GoValue) {
574574
return (GoValue)place.getParent();
575575
}
576576
}
577+
while ((place = PsiTreeUtil.getParentOfType(place, GoLiteralValue.class)) != null);
577578
return null;
578579
}
579580

@@ -1468,7 +1469,8 @@ public static GoExpression getValue(@NotNull GoConstDefinition definition) {
14681469
return getByIndex(((GoConstSpec)parent).getExpressionList(), index);
14691470
}
14701471

1471-
private static <T> T getByIndex(@NotNull List<T> list, int index) {
1472+
@Nullable
1473+
public static <T> T getByIndex(@NotNull List<T> list, int index) {
14721474
return 0 <= index && index < list.size() ? list.get(index) : null;
14731475
}
14741476

testData/inspections/go-struct-initialization/quickFix.go

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package foo
2+
3+
type S struct {
4+
X string
5+
string
6+
Y int
7+
}
8+
func main() {
9+
var s S
10+
s = S{X: "X", string: "a", Y: 1}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package foo
2+
3+
type S struct {
4+
X string
5+
string
6+
Y int
7+
}
8+
func main() {
9+
var s S
10+
s = S{<caret><weak_warning descr="Unnamed field initializations">"X"</weak_warning>, <weak_warning descr="Unnamed field initializations">"a"</weak_warning>, Y: 1}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package foo
2+
3+
type S struct {
4+
X, Y int
5+
}
6+
func main() {
7+
s := S{X: 1, Y: 0, 2}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package foo
2+
3+
type S struct {
4+
X, Y int
5+
}
6+
func main() {
7+
s := S{<weak_warning descr="Unnamed field initializations"><caret>1</weak_warning>, <weak_warning descr="Unnamed field initializations">0</weak_warning>, <weak_warning descr="Unnamed field initializations">2</weak_warning>}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package foo
2+
3+
type S struct {
4+
X, Y int
5+
}
6+
func main() {
7+
s := S{<caret>1, 0, X: 2}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package foo
2+
3+
type S struct {
4+
t int
5+
}
6+
7+
func main() {
8+
var _ = []S{ {t: 1} }
9+
}

0 commit comments

Comments
 (0)