@@ -39,18 +39,23 @@ func SignatureHelp(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle
39
39
if err != nil {
40
40
return nil , 0 , err
41
41
}
42
- // Find a call expression surrounding the query position.
43
- var callExpr * ast.CallExpr
42
+ // Find a call/SelectorExpr expression surrounding the query position.
43
+ var callNode ast.Node
44
44
path , _ := astutil .PathEnclosingInterval (pgf .File , pos , pos )
45
45
if path == nil {
46
46
return nil , 0 , fmt .Errorf ("cannot find node enclosing position" )
47
47
}
48
48
FindCall:
49
49
for _ , node := range path {
50
50
switch node := node .(type ) {
51
+ case * ast.SelectorExpr :
52
+ if pos >= node .Pos () && pos <= node .End () {
53
+ callNode = node
54
+ break FindCall
55
+ }
51
56
case * ast.CallExpr :
52
57
if pos >= node .Lparen && pos <= node .Rparen {
53
- callExpr = node
58
+ callNode = node
54
59
break FindCall
55
60
}
56
61
case * ast.FuncLit , * ast.FuncType :
@@ -67,23 +72,36 @@ FindCall:
67
72
// you've already dismissed the help!).
68
73
return nil , 0 , nil
69
74
}
75
+ default :
70
76
}
71
77
72
78
}
73
- if callExpr == nil || callExpr .Fun == nil {
79
+
80
+ if callNode == nil {
74
81
return nil , 0 , nil
75
82
}
76
83
77
84
info := pkg .TypesInfo ()
78
85
86
+ var expr ast.Expr
87
+ switch node := callNode .(type ) {
88
+ case * ast.SelectorExpr :
89
+ expr = node
90
+ case * ast.CallExpr :
91
+ if node .Fun == nil {
92
+ return nil , 0 , nil
93
+ }
94
+ expr = node .Fun
95
+ }
96
+
79
97
// Get the type information for the function being called.
80
98
var sig * types.Signature
81
- if tv , ok := info .Types [callExpr . Fun ]; ! ok {
82
- return nil , 0 , fmt .Errorf ("cannot get type for Fun %[1]T (%[1]v)" , callExpr . Fun )
99
+ if tv , ok := info .Types [expr ]; ! ok {
100
+ return nil , 0 , fmt .Errorf ("cannot get type for Fun %[1]T (%[1]v)" , expr )
83
101
} else if tv .IsType () {
84
102
return nil , 0 , nil // a conversion, not a call
85
103
} else if sig , ok = tv .Type .Underlying ().(* types.Signature ); ! ok {
86
- return nil , 0 , fmt .Errorf ("call operand is not a func or type: %[1]T (%[1]v)" , callExpr . Fun )
104
+ return nil , 0 , fmt .Errorf ("call operand is not a func or type: %[1]T (%[1]v)" , expr )
87
105
}
88
106
// Inv: sig != nil
89
107
@@ -93,16 +111,26 @@ FindCall:
93
111
// There is no object in certain cases such as calling a function returned by
94
112
// a function (e.g. "foo()()").
95
113
var obj types.Object
96
- switch t := callExpr . Fun .(type ) {
114
+ switch t := expr .(type ) {
97
115
case * ast.Ident :
98
116
obj = info .ObjectOf (t )
99
117
case * ast.SelectorExpr :
100
118
obj = info .ObjectOf (t .Sel )
101
119
}
120
+
121
+ activeParam := 0
122
+ switch node := callNode .(type ) {
123
+ case * ast.SelectorExpr :
124
+ case * ast.CallExpr :
125
+ // only return activeParam when CallExpr
126
+ // because we don't modify arguments when get function signature only
127
+ activeParam = activeParameter (node , sig .Params ().Len (), sig .Variadic (), pos )
128
+ }
129
+
102
130
if obj != nil && isBuiltin (obj ) {
103
131
// function?
104
132
if obj , ok := obj .(* types.Builtin ); ok {
105
- return builtinSignature (ctx , snapshot , callExpr , obj .Name (), pos )
133
+ return builtinSignature (ctx , snapshot , activeParam , obj .Name (), pos )
106
134
}
107
135
108
136
// method (only error.Error)?
@@ -116,8 +144,6 @@ FindCall:
116
144
return nil , 0 , bug .Errorf ("call to unexpected built-in %v (%T)" , obj , obj )
117
145
}
118
146
119
- activeParam := activeParameter (callExpr , sig .Params ().Len (), sig .Variadic (), pos )
120
-
121
147
var (
122
148
name string
123
149
comment * ast.CommentGroup
@@ -148,7 +174,7 @@ FindCall:
148
174
}, activeParam , nil
149
175
}
150
176
151
- func builtinSignature (ctx context.Context , snapshot * cache.Snapshot , callExpr * ast. CallExpr , name string , pos token.Pos ) (* protocol.SignatureInformation , int , error ) {
177
+ func builtinSignature (ctx context.Context , snapshot * cache.Snapshot , activeParam int , name string , pos token.Pos ) (* protocol.SignatureInformation , int , error ) {
152
178
sig , err := NewBuiltinSignature (ctx , snapshot , name )
153
179
if err != nil {
154
180
return nil , 0 , err
@@ -157,7 +183,7 @@ func builtinSignature(ctx context.Context, snapshot *cache.Snapshot, callExpr *a
157
183
for _ , p := range sig .params {
158
184
paramInfo = append (paramInfo , protocol.ParameterInformation {Label : p })
159
185
}
160
- activeParam := activeParameter ( callExpr , len ( sig . params ), sig . variadic , pos )
186
+
161
187
return & protocol.SignatureInformation {
162
188
Label : sig .name + sig .Format (),
163
189
Documentation : stringToSigInfoDocumentation (sig .doc , snapshot .Options ()),
0 commit comments