forked from go-lang-plugin-org/go-lang-idea-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework GoStructInitializationInspection
fixes go-lang-plugin-org#2819
- Loading branch information
Showing
27 changed files
with
441 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
129 changes: 0 additions & 129 deletions
129
src/com/goide/inspections/GoStructInitializationInspection.java
This file was deleted.
Oops, something went wrong.
165 changes: 165 additions & 0 deletions
165
src/com/goide/inspections/GoStructInitializationWithUnnamedFieldInspection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright 2013-2016 Sergey Ignatov, Alexander Zolotov, Florin Patan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.goide.inspections; | ||
|
||
import com.goide.psi.*; | ||
import com.goide.psi.impl.GoElementFactory; | ||
import com.goide.psi.impl.GoPsiImplUtil; | ||
import com.goide.util.GoUtil; | ||
import com.intellij.codeInspection.*; | ||
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.util.InvalidDataException; | ||
import com.intellij.openapi.util.WriteExternalException; | ||
import com.intellij.util.ObjectUtils; | ||
import org.jdom.Element; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.swing.*; | ||
import java.util.List; | ||
|
||
import static com.intellij.openapi.util.Comparing.equal; | ||
import static com.intellij.util.containers.ContainerUtil.*; | ||
import static java.util.stream.Collectors.toList; | ||
import static java.util.stream.IntStream.range; | ||
|
||
public class GoStructInitializationWithUnnamedFieldInspection extends GoInspectionBase { | ||
public static final String REPLACE_WITH_NAMED_STRUCT_FIELD_FIX_NAME = "Replace with named struct fields"; | ||
private static final GoReplaceWithNamedStructFieldQuickFix QUICK_FIX = new GoReplaceWithNamedStructFieldQuickFix(); | ||
public boolean reportLocalStructs; | ||
/** | ||
* @deprecated use {@link #reportLocalStructs} | ||
*/ | ||
@SuppressWarnings("WeakerAccess") public Boolean reportImportedStructs; | ||
|
||
@NotNull | ||
@Override | ||
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) { | ||
return new GoVisitor() { | ||
|
||
@Override | ||
public void visitCompositeLit(@NotNull GoCompositeLit compositeLit) { | ||
GoLiteralValue literalValue = compositeLit.getLiteralValue(); | ||
GoStructType structType = getStructType(literalValue); | ||
if (structType == null || !isStructPackageLocationValid(literalValue, structType)) return; | ||
|
||
List<GoElement> elements = literalValue.getElementList(); | ||
if (hasUnnamedElement(elements) && areElementsNamesMatchesDefinitions(getNames(elements), getFieldDefinitionsNames(structType))) { | ||
holder.registerProblem(literalValue, "Unnamed field initializations", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, QUICK_FIX); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Nullable | ||
@Contract("null -> null") | ||
private static GoStructType getStructType(@Nullable GoLiteralValue literal) { | ||
return literal != null ? ObjectUtils.tryCast(GoPsiImplUtil.getLiteralType(literal.getParent(), false), GoStructType.class) : null; | ||
} | ||
|
||
private boolean isStructPackageLocationValid(GoLiteralValue literalValue, GoStructType structType) { | ||
return reportLocalStructs || !GoUtil.inSamePackage(literalValue.getContainingFile(), structType.getContainingFile()); | ||
} | ||
|
||
private static boolean hasUnnamedElement(@NotNull List<GoElement> elements) { | ||
return exists(elements, element -> element.getKey() == null); | ||
} | ||
|
||
@NotNull | ||
private static List<String> getNames(@NotNull List<GoElement> elements) { | ||
return map(elements, element -> { | ||
GoKey key = element.getKey(); | ||
return key != null ? key.getText() : null; | ||
}); | ||
} | ||
|
||
private static boolean areElementsNamesMatchesDefinitions(@NotNull List<String> elementsNames, | ||
@NotNull List<String> fieldDefinitionsNames) { | ||
return range(0, elementsNames.size()) | ||
.allMatch(index -> elementsNames.get(index) == null || equal(elementsNames.get(index), getByIndex(fieldDefinitionsNames, index))); | ||
} | ||
|
||
@Nullable | ||
private static String getByIndex(@NotNull List<String> list, int index) { | ||
return 0 <= index && index < list.size() ? list.get(index) : null; | ||
} | ||
|
||
@NotNull | ||
private static List<String> getFieldDefinitionsNames(@NotNull GoStructType type) { | ||
return type.getFieldDeclarationList().stream() | ||
.flatMap(declaration -> getFieldDefinitionsNames(declaration).stream()) | ||
.collect(toList()); | ||
} | ||
|
||
@NotNull | ||
private static List<String> getFieldDefinitionsNames(@NotNull GoFieldDeclaration declaration) { | ||
GoAnonymousFieldDefinition definition = declaration.getAnonymousFieldDefinition(); | ||
return definition != null ? list(definition.getName()) : map(declaration.getFieldDefinitionList(), GoNamedElement::getName); | ||
} | ||
|
||
@Override | ||
public JComponent createOptionsPanel() { | ||
return new SingleCheckboxOptionsPanel("Report for local type definitions as well", this, "reportLocalStructs"); | ||
} | ||
|
||
private static class GoReplaceWithNamedStructFieldQuickFix extends LocalQuickFixBase { | ||
|
||
public GoReplaceWithNamedStructFieldQuickFix() { | ||
super(REPLACE_WITH_NAMED_STRUCT_FIELD_FIX_NAME); | ||
} | ||
|
||
@Override | ||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { | ||
GoLiteralValue literal = ObjectUtils.tryCast(descriptor.getStartElement(), GoLiteralValue.class); | ||
GoStructType structType = getStructType(literal); | ||
List<GoElement> elements = structType != null ? literal.getElementList() : emptyList(); | ||
List<String> fieldDefinitionNames = structType != null ? getFieldDefinitionsNames(structType) : emptyList(); | ||
if (!areElementsNamesMatchesDefinitions(getNames(elements), fieldDefinitionNames)) return; | ||
replaceElementsByNamed(elements, fieldDefinitionNames, project); | ||
} | ||
} | ||
|
||
private static void replaceElementsByNamed(@NotNull List<GoElement> elements, | ||
@NotNull List<String> fieldDefinitionNames, | ||
@NotNull Project project) { | ||
for (int i = 0; i < elements.size(); i++) { | ||
GoElement element = elements.get(i); | ||
String fieldDefinitionName = getByIndex(fieldDefinitionNames, i); | ||
GoValue value = fieldDefinitionName != null && element.getKey() == null ? element.getValue() : null; | ||
if (value == null) continue; | ||
|
||
GoElement namedElement = GoElementFactory.createLiteralValueElement(project, fieldDefinitionName, value.getText()); | ||
element.replace(namedElement); | ||
} | ||
} | ||
|
||
@Override | ||
public void readSettings(@NotNull Element node) throws InvalidDataException { | ||
super.readSettings(node); | ||
if (reportImportedStructs != null) { | ||
reportLocalStructs = reportImportedStructs; | ||
} | ||
} | ||
|
||
@Override | ||
public void writeSettings(@NotNull Element node) throws WriteExternalException { | ||
reportImportedStructs = null; | ||
super.writeSettings(node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
testData/inspections/struct-initialization-with-unnamed-field/anonField-after.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package foo | ||
|
||
type S struct { | ||
X string | ||
string | ||
Y int | ||
} | ||
func main() { | ||
var s S | ||
s = S{X: "X", string: "a", Y: 1} | ||
} |
11 changes: 11 additions & 0 deletions
11
testData/inspections/struct-initialization-with-unnamed-field/anonField.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package foo | ||
|
||
type S struct { | ||
X string | ||
string | ||
Y int | ||
} | ||
func main() { | ||
var s S | ||
s = S<weak_warning descr="Unnamed field initializations">{<caret>"X", "a", Y: 1}</weak_warning> | ||
} |
8 changes: 8 additions & 0 deletions
8
...pections/struct-initialization-with-unnamed-field/exceedElementsCountOnlyUnnamed-after.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package foo | ||
|
||
type S struct { | ||
X, Y int | ||
} | ||
func main() { | ||
s := S{X: 1, Y: 0, 2} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ta/inspections/struct-initialization-with-unnamed-field/exceedElementsCountOnlyUnnamed.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package foo | ||
|
||
type S struct { | ||
X, Y int | ||
} | ||
func main() { | ||
s := S<weak_warning descr="Unnamed field initializations">{<caret>1, 0, 2}</weak_warning> | ||
} |
9 changes: 9 additions & 0 deletions
9
...Data/inspections/struct-initialization-with-unnamed-field/exceedElementsCountWithNamed.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package foo | ||
|
||
type S struct { | ||
X, Y int | ||
} | ||
func main() { | ||
s := S{<caret>1, 0, X: 2} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
testData/inspections/struct-initialization-with-unnamed-field/innerAnonStruct-after.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package foo | ||
|
||
func main() { | ||
type B struct { | ||
Y int | ||
} | ||
|
||
type S struct { | ||
X int | ||
B | ||
Z int | ||
} | ||
|
||
s := S{X: 1, B: B{Y: 2}, Z: 3} | ||
print(s.B.Y) | ||
} |
Oops, something went wrong.