Skip to content

Commit 9f63fbf

Browse files
committed
Review marks
1 parent 006beaa commit 9f63fbf

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/com/goide/inspections/unresolved/GoAddStructFieldFix.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package com.goide.inspections.unresolved;
1818

1919
import com.goide.psi.GoFieldDeclaration;
20+
import com.goide.psi.GoReferenceExpression;
2021
import com.goide.psi.GoStructType;
22+
import com.goide.psi.GoType;
2123
import com.goide.psi.impl.GoElementFactory;
2224
import com.intellij.codeInspection.LocalQuickFixOnPsiElement;
2325
import com.intellij.openapi.project.Project;
@@ -27,6 +29,7 @@
2729
import com.intellij.util.containers.ContainerUtil;
2830
import org.jetbrains.annotations.Nls;
2931
import org.jetbrains.annotations.NotNull;
32+
import org.jetbrains.annotations.Nullable;
3033

3134
import java.util.List;
3235

@@ -35,7 +38,7 @@ public class GoAddStructFieldFix extends LocalQuickFixOnPsiElement {
3538
private final String myFieldText;
3639
private final String myTypeText;
3740

38-
protected GoAddStructFieldFix(String fieldText, String typeText, @NotNull GoStructType element) {
41+
protected GoAddStructFieldFix(String fieldText, String typeText, @NotNull PsiElement element) {
3942
super(element);
4043
myFieldText = fieldText;
4144
myTypeText = typeText;
@@ -49,11 +52,19 @@ public String getText() {
4952

5053
@Override
5154
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
52-
GoStructType structType = ObjectUtils.tryCast(startElement, GoStructType.class);
55+
GoStructType structType = resolveStructType(startElement);
5356
if (structType == null) return;
5457
List<GoFieldDeclaration> declarations = structType.getFieldDeclarationList();
5558
PsiElement anchor = !declarations.isEmpty() ? ContainerUtil.getLastItem(declarations) : structType.getLbrace();
56-
if (anchor != null) structType.addAfter(GoElementFactory.createFieldDeclaration(project, myFieldText, myTypeText), anchor);
59+
structType.addAfter(GoElementFactory.createFieldDeclaration(project, myFieldText, myTypeText), anchor);
60+
}
61+
62+
@Nullable
63+
private static GoStructType resolveStructType(@NotNull PsiElement startElement) {
64+
GoReferenceExpression referenceExpression = ObjectUtils.tryCast(startElement, GoReferenceExpression.class);
65+
GoReferenceExpression qualifier = referenceExpression != null ? referenceExpression.getQualifier() : null;
66+
GoType type = qualifier != null ? qualifier.getGoType(null) : null;
67+
return type != null ? ObjectUtils.tryCast(type.getUnderlyingType(), GoStructType.class) : null;
5768
}
5869

5970
@Nls

src/com/goide/inspections/unresolved/GoUnresolvedReferenceInspection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ else if (reference.resolve() == null) {
7979
GoType type = qualifier != null ? qualifier.getGoType(null) : null;
8080
GoStructType structType = type != null ? ObjectUtils.tryCast(type.getUnderlyingType(), GoStructType.class) : null;
8181
if (!"_".equals(reference.getCanonicalText()) && structType != null) {
82-
fixes = new LocalQuickFix[]{new GoAddStructFieldFix(reference.getCanonicalText(), getTypeName(o), structType)};
82+
fixes = new LocalQuickFix[]{new GoAddStructFieldFix(reference.getCanonicalText(), getTypeName(o), o)};
8383
}
8484
else if (isProhibited(o, qualifier)) {
8585
fixes = createImportPackageFixes(o, reference, holder.isOnTheFly());

0 commit comments

Comments
 (0)