|
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;
|
| 26 | +import com.intellij.openapi.util.Comparing; |
27 | 27 | import com.intellij.openapi.util.InvalidDataException;
|
28 | 28 | import com.intellij.openapi.util.WriteExternalException;
|
29 | 29 | import com.intellij.psi.PsiElement;
|
30 | 30 | import com.intellij.psi.util.PsiTreeUtil;
|
31 |
| -import com.intellij.util.containers.ContainerUtil; |
| 31 | +import com.intellij.util.ObjectUtils; |
32 | 32 | import org.jdom.Element;
|
| 33 | +import org.jetbrains.annotations.Contract; |
33 | 34 | import org.jetbrains.annotations.NotNull;
|
34 | 35 | import org.jetbrains.annotations.Nullable;
|
35 | 36 |
|
36 | 37 | import javax.swing.*;
|
37 | 38 | import java.util.List;
|
38 | 39 |
|
| 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 | + |
39 | 44 | 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(); |
41 | 47 | public boolean reportLocalStructs;
|
42 | 48 | /**
|
43 |
| - * @deprecated use reportLocalStructs |
| 49 | + * @deprecated use {@link #reportLocalStructs} |
44 | 50 | */
|
45 | 51 | @SuppressWarnings("WeakerAccess") public Boolean reportImportedStructs;
|
46 | 52 |
|
47 | 53 | @NotNull
|
48 | 54 | @Override
|
49 | 55 | protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
|
50 | 56 | return new GoVisitor() {
|
| 57 | + |
51 | 58 | @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); |
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 | + @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; |
68 | 79 | }
|
69 | 80 |
|
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; |
74 | 86 | }
|
75 | 87 |
|
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; |
90 | 96 | }
|
91 | 97 |
|
92 | 98 | @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"); |
97 | 171 | }
|
98 | 172 |
|
99 | 173 | private static class GoReplaceWithNamedStructFieldQuickFix extends LocalQuickFixBase {
|
100 |
| - private String myStructField; |
101 | 174 |
|
102 |
| - public GoReplaceWithNamedStructFieldQuickFix(@NotNull String structField) { |
| 175 | + public GoReplaceWithNamedStructFieldQuickFix() { |
103 | 176 | super(REPLACE_WITH_NAMED_STRUCT_FIELD_FIX_NAME);
|
104 |
| - myStructField = structField; |
105 | 177 | }
|
106 | 178 |
|
107 | 179 | @Override
|
108 | 180 | 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())); |
113 | 199 | }
|
114 | 200 | }
|
115 | 201 |
|
|
0 commit comments