17
17
package com .goide .inspections .unresolved ;
18
18
19
19
import com .goide .psi .GoFieldDeclaration ;
20
+ import com .goide .psi .GoReferenceExpression ;
20
21
import com .goide .psi .GoStructType ;
22
+ import com .goide .psi .GoType ;
21
23
import com .goide .psi .impl .GoElementFactory ;
22
24
import com .intellij .codeInspection .LocalQuickFixOnPsiElement ;
23
25
import com .intellij .openapi .project .Project ;
27
29
import com .intellij .util .containers .ContainerUtil ;
28
30
import org .jetbrains .annotations .Nls ;
29
31
import org .jetbrains .annotations .NotNull ;
32
+ import org .jetbrains .annotations .Nullable ;
30
33
31
34
import java .util .List ;
32
35
@@ -35,7 +38,7 @@ public class GoAddStructFieldFix extends LocalQuickFixOnPsiElement {
35
38
private final String myFieldText ;
36
39
private final String myTypeText ;
37
40
38
- protected GoAddStructFieldFix (String fieldText , String typeText , @ NotNull GoStructType element ) {
41
+ protected GoAddStructFieldFix (String fieldText , String typeText , @ NotNull PsiElement element ) {
39
42
super (element );
40
43
myFieldText = fieldText ;
41
44
myTypeText = typeText ;
@@ -49,11 +52,19 @@ public String getText() {
49
52
50
53
@ Override
51
54
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 );
53
56
if (structType == null ) return ;
54
57
List <GoFieldDeclaration > declarations = structType .getFieldDeclarationList ();
55
58
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 ;
57
68
}
58
69
59
70
@ Nls
0 commit comments