Skip to content

Commit

Permalink
Rework GoStructInitializationInspection
Browse files Browse the repository at this point in the history
fixes #2819
  • Loading branch information
wbars committed Nov 13, 2016
1 parent eb669fc commit adbba13
Show file tree
Hide file tree
Showing 27 changed files with 441 additions and 233 deletions.
2 changes: 1 addition & 1 deletion resources/META-INF/gogland.xml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
implementationClass="com.goide.inspections.GoExportedOwnDeclarationInspection"/>
<localInspection language="go" displayName="Struct initialization without field names" groupPath="Go"
groupName="Code style issues" enabledByDefault="true" level="WEAK WARNING"
implementationClass="com.goide.inspections.GoStructInitializationInspection"/>
implementationClass="com.goide.inspections.GoStructInitializationWithUnnamedFieldInspection"/>
<localInspection language="go" displayName="Receiver has generic name" groupPath="Go"
groupName="Code style issues" enabledByDefault="true" level="WEAK WARNING"
implementationClass="com.goide.inspections.GoReceiverNamesInspection"/>
Expand Down
129 changes: 0 additions & 129 deletions src/com/goide/inspections/GoStructInitializationInspection.java

This file was deleted.

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);
}
}
2 changes: 1 addition & 1 deletion src/com/goide/psi/impl/GoElementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public static GoType createType(@NotNull Project project, @NotNull String text)
return PsiTreeUtil.findChildOfType(file, GoType.class);
}

public static PsiElement createLiteralValueElement(@NotNull Project project, @NotNull String key, @NotNull String value) {
public static GoElement createLiteralValueElement(@NotNull Project project, @NotNull String key, @NotNull String value) {
GoFile file = createFileFromText(project, "package a; var _ = struct { a string } { " + key + ": " + value + " }");
return PsiTreeUtil.findChildOfType(file, GoElement.class);
}
Expand Down
9 changes: 0 additions & 9 deletions testData/inspections/go-struct-initialization/quickFix.go

This file was deleted.

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

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

0 comments on commit adbba13

Please sign in to comment.