1717package com .goide .inspections .unresolved ;
1818
1919import com .goide .psi .GoFieldDeclaration ;
20+ import com .goide .psi .GoReferenceExpression ;
2021import com .goide .psi .GoStructType ;
22+ import com .goide .psi .GoType ;
2123import com .goide .psi .impl .GoElementFactory ;
2224import com .intellij .codeInspection .LocalQuickFixOnPsiElement ;
2325import com .intellij .openapi .project .Project ;
2729import com .intellij .util .containers .ContainerUtil ;
2830import org .jetbrains .annotations .Nls ;
2931import org .jetbrains .annotations .NotNull ;
32+ import org .jetbrains .annotations .Nullable ;
3033
3134import 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
0 commit comments