Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/com/goide/inspections/unresolved/GoAddStructFieldFix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2013-2017 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.unresolved;

import com.goide.psi.GoFieldDeclaration;
import com.goide.psi.GoStructType;
import com.goide.psi.impl.GoElementFactory;
import com.intellij.codeInspection.LocalQuickFixOnPsiElement;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class GoAddStructFieldFix extends LocalQuickFixOnPsiElement {
public static final String QUICK_FIX_NAME = "Add missing field";
private final String myFieldText;
private final String myTypeText;

protected GoAddStructFieldFix(String fieldText, String typeText, @NotNull GoStructType element) {
super(element);
myFieldText = fieldText;
myTypeText = typeText;
}

@NotNull
@Override
public String getText() {
return QUICK_FIX_NAME;
}

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
GoStructType structType = ObjectUtils.tryCast(startElement, GoStructType.class);
if (structType == null) return;
List<GoFieldDeclaration> declarations = structType.getFieldDeclarationList();
PsiElement anchor = !declarations.isEmpty() ? ContainerUtil.getLastItem(declarations) : structType.getLbrace();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= ContainerUtil.getLastItem(declarations, structType.getLbrace)

if (anchor != null) structType.addAfter(GoElementFactory.createFieldDeclaration(project, myFieldText, myTypeText), anchor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When can it happen? Can you test this?

}

@Nls
@NotNull
@Override
public String getFamilyName() {
return QUICK_FIX_NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.goide.codeInsight.imports.GoImportPackageQuickFix;
import com.goide.inspections.GoInspectionBase;
import com.goide.psi.*;
import com.goide.psi.impl.GoPsiImplUtil;
import com.goide.psi.impl.GoReference;
import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector;
import com.intellij.codeInspection.LocalInspectionToolSession;
Expand All @@ -33,6 +34,7 @@
import com.intellij.psi.formatter.FormatterUtil;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -74,7 +76,12 @@ public void visitReferenceExpression(@NotNull GoReferenceExpression o) {
}
else if (reference.resolve() == null) {
LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY;
if (isProhibited(o, qualifier)) {
GoType type = qualifier != null ? qualifier.getGoType(null) : null;
GoStructType structType = type != null ? ObjectUtils.tryCast(type.getUnderlyingType(), GoStructType.class) : null;
if (!"_".equals(reference.getCanonicalText()) && structType != null) {
fixes = new LocalQuickFix[]{new GoAddStructFieldFix(reference.getCanonicalText(), getTypeName(o), structType)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, psi element that is passed to QuickFix is an inspection holder. In this case you pass there the element that should be changed, not the unresolved element. It's bad, because the struct type or unresolved reference could completely changed on the moment of quickfix invocation and should not be modified,

}
else if (isProhibited(o, qualifier)) {
fixes = createImportPackageFixes(o, reference, holder.isOnTheFly());
}
else if (holder.isOnTheFly()) {
Expand Down Expand Up @@ -159,6 +166,14 @@ else if (holder.isOnTheFly()) {
};
}

private static String getTypeName(GoReferenceExpression referenceExpression) {
GoAssignmentStatement assignment = PsiTreeUtil.getParentOfType(referenceExpression, GoAssignmentStatement.class);
if (assignment == null) return "interface {}";
GoExpression expression = GoPsiImplUtil.getRightExpression(assignment, referenceExpression);
GoType type = expression != null ? expression.getGoType(null) : null;
return type != null ? type.getText() : "interface {}";
}

@NotNull
private static LocalQuickFix[] createImportPackageFixes(@NotNull PsiElement target, @NotNull PsiReference reference, boolean onTheFly) {
if (onTheFly) {
Expand Down
9 changes: 7 additions & 2 deletions src/com/goide/psi/impl/GoElementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static GoIfStatement createIfStatement(@NotNull Project project,
public static GoImportDeclaration createEmptyImportDeclaration(@NotNull Project project) {
return PsiTreeUtil.findChildOfType(createFileFromText(project, "package main\nimport (\n\n)"), GoImportDeclaration.class);
}

@NotNull
public static GoImportDeclaration createImportDeclaration(@NotNull Project project, @NotNull String importString,
@Nullable String alias, boolean withParens) {
Expand Down Expand Up @@ -191,7 +191,7 @@ public static GoGoStatement createGoStatement(@NotNull Project project, @NotNull

@NotNull
public static GoForStatement createForStatement(@NotNull Project project, @NotNull String text) {
GoFile file = createFileFromText(project, "package a; func a() {\n for {\n" + text + "\n}\n}");
GoFile file = createFileFromText(project, "package a; func a() {\n for {\n" + text + "\n}\n}");
return PsiTreeUtil.findChildOfType(file, GoForStatement.class);
}

Expand Down Expand Up @@ -266,4 +266,9 @@ public static GoTypeDeclaration createTypeDeclaration(@NotNull Project project,
GoFile file = createFileFromText(project, "package a; type " + name + " " + type.getText());
return PsiTreeUtil.findChildOfType(file, GoTypeDeclaration.class);
}

public static GoFieldDeclaration createFieldDeclaration(@NotNull Project project, @NotNull String fieldText, @NotNull String typeText) {
GoFile file = createFileFromText(project, "package a; var _ = struct { " + fieldText + " " + typeText + " }");
return PsiTreeUtil.findChildOfType(file, GoFieldDeclaration.class);
}
}
11 changes: 11 additions & 0 deletions testData/quickfixes/add-struct-field/blank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

type S struct {
bb interface{}
cc interface{}
}

func main() {
s := S{}
s.<error descr="Unresolved reference '_'">_<caret></error> = "aa"
}
12 changes: 12 additions & 0 deletions testData/quickfixes/add-struct-field/simple-after.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

type S struct {
bb interface{}
cc interface{}
aa string
}

func main() {
s := S{}
s.aa = "aa"
}
11 changes: 11 additions & 0 deletions testData/quickfixes/add-struct-field/simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

type S struct {
bb interface{}
cc interface{}
}

func main() {
s := S{}
s.aa<caret> = "aa"
}
12 changes: 12 additions & 0 deletions testData/quickfixes/add-struct-field/withoutAssignment-after.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

type S struct {
bb interface{}
cc interface{}
aa interface{}
}

func main() {
s := S{}
s.aa<caret>
}
11 changes: 11 additions & 0 deletions testData/quickfixes/add-struct-field/withoutAssignment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

type S struct {
bb interface{}
cc interface{}
}

func main() {
s := S{}
s.aa<caret>
}
10 changes: 10 additions & 0 deletions testData/quickfixes/add-struct-field/withoutElements-after.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

type S struct {
aa string
}

func main() {
s := S{}
s.aa<caret> = "aa"
}
10 changes: 10 additions & 0 deletions testData/quickfixes/add-struct-field/withoutElements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

type S struct {

}

func main() {
s := S{}
s.aa<caret> = "aa"
}
44 changes: 44 additions & 0 deletions tests/com/goide/quickfix/GoAddStructFieldQuickFixTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2013-2017 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.quickfix;

import com.goide.SdkAware;
import com.goide.inspections.unresolved.GoUnresolvedReferenceInspection;
import org.jetbrains.annotations.NotNull;

@SdkAware
public class GoAddStructFieldQuickFixTest extends GoQuickFixTestBase {

private static final String ADD_STRUCT_FIELD = "Add missing field";

@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(GoUnresolvedReferenceInspection.class);
}

@NotNull
@Override
protected String getBasePath() {
return "quickfixes/add-struct-field";
}

public void testSimple() { doTest(ADD_STRUCT_FIELD); }
public void testWithoutElements() { doTest(ADD_STRUCT_FIELD); }
public void testWithoutAssignment() { doTest(ADD_STRUCT_FIELD); }
public void testBlank() { doTestNoFix(ADD_STRUCT_FIELD, true); }
}