|
22 | 22 | import com.goide.util.GoUtil;
|
23 | 23 | import com.intellij.codeInspection.*;
|
24 | 24 | import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
|
25 |
| -import com.intellij.openapi.progress.ProgressManager; |
26 | 25 | import com.intellij.openapi.project.Project;
|
27 | 26 | import com.intellij.openapi.util.InvalidDataException;
|
28 | 27 | import com.intellij.openapi.util.WriteExternalException;
|
29 |
| -import com.intellij.psi.PsiElement; |
30 |
| -import com.intellij.psi.util.PsiTreeUtil; |
31 |
| -import com.intellij.util.containers.ContainerUtil; |
| 28 | +import com.intellij.util.ObjectUtils; |
32 | 29 | import org.jdom.Element;
|
| 30 | +import org.jetbrains.annotations.Contract; |
33 | 31 | import org.jetbrains.annotations.NotNull;
|
34 | 32 | import org.jetbrains.annotations.Nullable;
|
35 | 33 |
|
36 | 34 | import javax.swing.*;
|
37 | 35 | import java.util.List;
|
| 36 | +import java.util.stream.IntStream; |
| 37 | + |
| 38 | +import static com.intellij.openapi.util.Comparing.equal; |
| 39 | +import static com.intellij.openapi.util.NullUtils.hasNull; |
| 40 | +import static com.intellij.util.containers.ContainerUtil.*; |
| 41 | +import static java.util.stream.Collectors.toList; |
38 | 42 |
|
39 | 43 | public class GoStructInitializationInspection extends GoInspectionBase {
|
40 |
| - public static final String REPLACE_WITH_NAMED_STRUCT_FIELD_FIX_NAME = "Replace with named struct field"; |
| 44 | + public static final String REPLACE_WITH_NAMED_STRUCT_FIELD_FIX_NAME = "Replace with named struct fields"; |
| 45 | + private static final GoReplaceWithNamedStructFieldQuickFix QUICK_FIX = new GoReplaceWithNamedStructFieldQuickFix(); |
41 | 46 | public boolean reportLocalStructs;
|
42 | 47 | /**
|
43 |
| - * @deprecated use reportLocalStructs |
| 48 | + * @deprecated use {@link #reportLocalStructs} |
44 | 49 | */
|
45 | 50 | @SuppressWarnings("WeakerAccess") public Boolean reportImportedStructs;
|
46 | 51 |
|
47 | 52 | @NotNull
|
48 | 53 | @Override
|
49 | 54 | protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
|
50 | 55 | return new GoVisitor() {
|
| 56 | + |
51 | 57 | @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); |
| 58 | + public void visitCompositeLit(@NotNull GoCompositeLit compositeLit) { |
| 59 | + GoLiteralValue literalValue = compositeLit.getLiteralValue(); |
| 60 | + GoStructType structType = getStructType(literalValue); |
| 61 | + if (structType == null || !isStructImportedOrLocalAllowed(structType, literalValue)) return; |
| 62 | + |
| 63 | + List<String> elementsNames = getNames(literalValue.getElementList()); |
| 64 | + if (hasNull(elementsNames.toArray()) && areElementsNamesMatchesDefinitions(elementsNames, getFieldDefinitionsNames(structType))) { |
| 65 | + holder.registerProblem(literalValue, "Unnamed field initializations", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, QUICK_FIX); |
60 | 66 | }
|
61 | 67 | }
|
62 | 68 | };
|
63 | 69 | }
|
64 | 70 |
|
65 |
| - @Override |
66 |
| - public JComponent createOptionsPanel() { |
67 |
| - return new SingleCheckboxOptionsPanel("Report for local type definitions as well", this, "reportLocalStructs"); |
| 71 | + @Nullable |
| 72 | + @Contract("null -> null") |
| 73 | + private static GoStructType getStructType(@Nullable GoLiteralValue literal) { |
| 74 | + return literal != null ? ObjectUtils.tryCast(GoPsiImplUtil.getLiteralType(literal.getParent(), false), GoStructType.class) : null; |
68 | 75 | }
|
69 | 76 |
|
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 |
| - } |
| 77 | + private boolean isStructImportedOrLocalAllowed(@NotNull GoStructType structType, @NotNull GoLiteralValue literalValue) { |
| 78 | + return reportLocalStructs || !GoUtil.inSamePackage(structType.getContainingFile(), literalValue.getContainingFile()); |
74 | 79 | }
|
75 | 80 |
|
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 |
| - } |
| 81 | + @NotNull |
| 82 | + private static List<String> getNames(@NotNull List<GoElement> elements) { |
| 83 | + return map(elements, element -> { |
| 84 | + GoKey key = element.getKey(); |
| 85 | + return key != null ? key.getText() : null; |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + private static boolean areElementsNamesMatchesDefinitions(@NotNull List<String> elementsNames, |
| 90 | + @NotNull List<String> fieldDefinitionsNames) { |
| 91 | + return IntStream.range(0, elementsNames.size()) |
| 92 | + .allMatch(index -> elementsNames.get(index) == null || equal(elementsNames.get(index), getByIndex(fieldDefinitionsNames, index))); |
90 | 93 | }
|
91 | 94 |
|
92 | 95 | @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; |
| 96 | + private static String getByIndex(@NotNull List<String> list, int index) { |
| 97 | + return 0 <= index && index < list.size() ? list.get(index) : null; |
| 98 | + } |
| 99 | + |
| 100 | + @NotNull |
| 101 | + private static List<String> getFieldDefinitionsNames(@NotNull GoStructType type) { |
| 102 | + return type.getFieldDeclarationList().stream() |
| 103 | + .flatMap(declaration -> getFieldDefinitionsNames(declaration).stream()) |
| 104 | + .collect(toList()); |
| 105 | + } |
| 106 | + |
| 107 | + @NotNull |
| 108 | + private static List<String> getFieldDefinitionsNames(@NotNull GoFieldDeclaration declaration) { |
| 109 | + GoAnonymousFieldDefinition definition = declaration.getAnonymousFieldDefinition(); |
| 110 | + return definition != null ? list(definition.getName()) : map(declaration.getFieldDefinitionList(), GoNamedElement::getName); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public JComponent createOptionsPanel() { |
| 115 | + return new SingleCheckboxOptionsPanel("Report for local type definitions as well", this, "reportLocalStructs"); |
97 | 116 | }
|
98 | 117 |
|
99 | 118 | private static class GoReplaceWithNamedStructFieldQuickFix extends LocalQuickFixBase {
|
100 |
| - private String myStructField; |
101 | 119 |
|
102 |
| - public GoReplaceWithNamedStructFieldQuickFix(@NotNull String structField) { |
| 120 | + public GoReplaceWithNamedStructFieldQuickFix() { |
103 | 121 | super(REPLACE_WITH_NAMED_STRUCT_FIELD_FIX_NAME);
|
104 |
| - myStructField = structField; |
105 | 122 | }
|
106 | 123 |
|
107 | 124 | @Override
|
108 | 125 | 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 |
| - } |
| 126 | + GoLiteralValue literal = ObjectUtils.tryCast(descriptor.getStartElement(), GoLiteralValue.class); |
| 127 | + GoStructType structType = getStructType(literal); |
| 128 | + List<GoElement> elements = structType != null ? literal.getElementList() : emptyList(); |
| 129 | + List<String> fieldDefinitionNames = structType != null ? getFieldDefinitionsNames(structType) : emptyList(); |
| 130 | + if (!areElementsNamesMatchesDefinitions(getNames(elements), fieldDefinitionNames)) return; |
| 131 | + replaceElementsByNamed(elements, fieldDefinitionNames, project); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static void replaceElementsByNamed(@NotNull List<GoElement> elements, |
| 136 | + @NotNull List<String> fieldDefinitionNames, |
| 137 | + @NotNull Project project) { |
| 138 | + for (int i = 0; i < elements.size(); i++) { |
| 139 | + GoElement element = elements.get(i); |
| 140 | + String fieldDefinitionName = getByIndex(fieldDefinitionNames, i); |
| 141 | + GoValue value = fieldDefinitionName != null && element.getKey() == null ? element.getValue() : null; |
| 142 | + if (value == null) continue; |
| 143 | + |
| 144 | + GoElement namedElement = GoElementFactory.createLiteralValueElement(project, fieldDefinitionName, value.getText()); |
| 145 | + element.replace(namedElement); |
113 | 146 | }
|
114 | 147 | }
|
115 | 148 |
|
|
0 commit comments