Skip to content
← Back

src/syntax/process/signature_help.ghul

1
namespace Syntax.Process is
2
3
use Logging
4
use Source
5
6
use Semantic.Types.Type
7
8
use Ghul.Pipes
9
10
// One overload presented to the IDE's signature-help popup: a label
11
// for the whole call and a description per parameter. Built either
12
// from an overload-resolved FUNCTION_GROUP member or, when the callee
13
// is a value of function type (a let-bound lambda, a function-typed
14
// parameter, a directly-invoked anonymous function), from the
15
// function type itself.
16
class SIGNATURE is
17
description: string public
18
parameter_descriptions: Collections.List[string] public
19
20
init(description: string, parameter_descriptions: Collections.List[string]) is
21
self.description = description
22
self.parameter_descriptions = parameter_descriptions
23
si
24
si
25
26
class SIGNATURE_HELP_RESULT is
27
signatures: Collections.List[SIGNATURE] public
28
best_signature_index: int public
29
current_parameter_index: int public
30
31
// The parameter of the best signature that a call written with
32
// its argument pack spread out fills from every written argument
33
// at that position and after, and negative where it takes none.
34
spread_parameter_index: int public
35
36
init(
37
signatures: Collections.List[SIGNATURE],
38
best_signature_index: int
39
) is
40
self.signatures = signatures
41
self.best_signature_index = best_signature_index
42
self.current_parameter_index = 0
43
self.spread_parameter_index = -1
44
si
45
si
46
47
class SIGNATURE_HELP: ScopedVisitor is
48
_overload_resolver: Semantic.OVERLOAD_RESOLVER
49
50
_target_line: int
51
_target_column: int
52
53
_results: SIGNATURE_HELP_RESULT?
54
55
init(
56
logger: Logger,
57
symbol_table: Semantic.SYMBOL_TABLE,
58
namespaces: Semantic.NAMESPACES,
59
overload_resolver: Semantic.OVERLOAD_RESOLVER
60
)
61
is
62
super.init(logger, symbol_table, namespaces)
63
64
_overload_resolver = overload_resolver
65
si
66
67
find_signatures(root: Trees.Node, target_line: int, target_column: int) -> SIGNATURE_HELP_RESULT? is
68
_results = null
69
70
_target_line = target_line
71
_target_column = target_column
72
73
root.walk(self)
74
75
return _results
76
si
77
78
visit(call: Trees.Expressions.CALL) is
79
if !_results? then
80
if !call.arguments.location.contains(_target_line, _target_column) /\
81
(!call.location.contains(_target_line, _target_column) \/
82
call.function.location.contains(_target_line, _target_column))
83
then
84
return
85
fi
86
87
_results = help_for(call)
88
89
if let results = _results then
90
// A `|>` call carries its subject as arguments[0], but the
91
// subject sits to the left of the call's own location and
92
// is never the argument being edited. Locate the cursor
93
// among the written arguments alone, then shift past the
94
// parameter the subject occupies.
95
if call.is_thread_first /\ call.arguments.count > 0 then
96
results.current_parameter_index =
97
get_current_parameter_index(
98
call.location,
99
Collections.LIST[Trees.Expressions.Expression](call.arguments |> skip(1)),
100
call.arguments.has_trailing_comma
101
) + 1
102
else
103
results.current_parameter_index =
104
get_current_parameter_index(
105
call.location,
106
Collections.LIST[Trees.Expressions.Expression](call.arguments),
107
call.arguments.has_trailing_comma
108
)
109
fi
110
111
// The written arguments at and after the spread all fill
112
// the one parameter the pack stands for.
113
if results.spread_parameter_index >= 0 /\ results.current_parameter_index > results.spread_parameter_index then
114
results.current_parameter_index = results.spread_parameter_index
115
fi
116
fi
117
fi
118
si
119
120
visit(index: Trees.Expressions.INDEX) is
121
if !_results? then
122
let brackets_location =
123
LOCATION(
124
index.location.file_name,
125
index.left.location.end_line,
126
index.left.location.end_column,
127
index.location.end_line,
128
index.location.end_column
129
)
130
131
if !brackets_location.contains(_target_line, _target_column) \/
132
index.left.location.contains(_target_line, _target_column)
133
then
134
return
135
fi
136
137
_results = help_for(index)
138
fi
139
si
140
141
// Slot the cursor falls in, counting from the start of the call.
142
// Each slot runs from the end of the argument before it to the end
143
// of its own argument, so the comma introducing an argument belongs
144
// to that argument. A trailing comma adds one more slot, empty of
145
// any argument, running to the end of the call: that is where the
146
// cursor sits with the closing bracket already in place.
147
get_current_parameter_index(
148
full_location: LOCATION,
149
arguments: Collections.LIST[Trees.Expressions.Expression],
150
has_trailing_comma: bool
151
) -> int is
152
let slots =
153
if has_trailing_comma then
154
arguments.count + 1
155
else
156
arguments.count
157
fi
158
159
if slots <= 1 then
160
return 0
161
fi
162
163
let last = slots - 1
164
165
for i in 0..slots do
166
let location =
167
LOCATION(
168
full_location.file_name,
169
if i == 0 then full_location.start_line else arguments[i-1].location.end_line fi,
170
if i == 0 then full_location.start_column else arguments[i-1].location.end_column fi,
171
if i == last then full_location.end_line else arguments[i].location.end_line fi,
172
if i == last then full_location.end_column else arguments[i].location.end_column fi
173
)
174
175
if location.contains(_target_line, _target_column) then
176
return i
177
fi
178
od
179
180
return 0
181
si
182
183
help_for(call: Trees.Expressions.CALL) -> SIGNATURE_HELP_RESULT? is
184
if _results? then
185
return null
186
fi
187
188
let callee = call.function.value
189
190
if !callee? then
191
return null
192
fi
193
194
let function_group = function_group_of(callee)
195
196
if function_group? then
197
return help_for_function_group(function_group, argument_types_of(call.arguments))
198
fi
199
200
if callee.type? /\ callee.type.is_function then
201
return help_for_function_type(callee.type)
202
fi
203
204
return null
205
si
206
207
// Signature help for `x[i]` / `x[i] = v`: resolve the indexer
208
// as the read accessor (`get_Item`) on the left's type. The
209
// assign-side `set_Item` shares its leading parameters, so the
210
// read form's signature gives a useful surface for the index
211
// arguments the user is typing.
212
help_for(index: Trees.Expressions.INDEX) -> SIGNATURE_HELP_RESULT? is
213
if _results? then
214
return null
215
fi
216
217
let left_value = index.left.value
218
219
if !left_value? \/ !left_value.type? then
220
return null
221
fi
222
223
let type = left_value.type
224
225
if type.is_error then
226
return null
227
fi
228
229
let named_type = cast Semantic.Types.NAMED?(type)
230
231
if !named_type? then
232
return null
233
fi
234
235
let symbol = named_type.find_member(Semantic.Symbols.INDEXER_NAMES.read)
236
237
if !symbol? \/ !symbol.is_function_group then
238
return null
239
fi
240
241
let argument_types = Collections.LIST[Type]()
242
243
if let value = index.index?.value /\ value.type? then
244
argument_types.add(value.type)
245
else
246
argument_types.add(Semantic.Types.ERROR())
247
fi
248
249
return help_for_function_group(
250
cast Semantic.Symbols.FUNCTION_GROUP?(symbol)!,
251
argument_types
252
)
253
si
254
255
// The callee's IR value resolves to a named function only when
256
// it is a direct load of a FUNCTION_GROUP symbol. A load of a
257
// variable / parameter / field of function type does not — its
258
// signature comes from the function type instead.
259
function_group_of(callee: IR.Values.Value) -> Semantic.Symbols.FUNCTION_GROUP? is
260
let load = cast IR.Values.Load.SYMBOL?(callee)
261
262
if !load? then
263
return null
264
fi
265
266
return cast Semantic.Symbols.FUNCTION_GROUP?(load.symbol)
267
si
268
269
argument_types_of(arguments: Trees.Expressions.LIST) -> Collections.LIST[Type] is
270
let argument_types = Collections.LIST[Type]()
271
272
for a in arguments do
273
if let value = a?.value /\ value.type? then
274
argument_types.add(value.type)
275
else
276
argument_types.add(Semantic.Types.ERROR())
277
fi
278
od
279
280
return argument_types
281
si
282
283
help_for_function_group(
284
function_group: Semantic.Symbols.FUNCTION_GROUP,
285
argument_types: Collections.LIST[Type]
286
) -> SIGNATURE_HELP_RESULT? is
287
let overload_results = _overload_resolver.find_matches(function_group, argument_types)
288
289
if !overload_results? \/ overload_results.results.count == 0 then
290
return null
291
fi
292
293
let signatures = Collections.LIST[SIGNATURE]()
294
295
for f in overload_results.results do
296
signatures.add(signature_for_function(f))
297
od
298
299
let result = SIGNATURE_HELP_RESULT(signatures, overload_results.best_result_index)
300
301
if overload_results.best_result_index >= 0 /\ overload_results.best_result_index < overload_results.results.count then
302
result.spread_parameter_index = overload_results.results[overload_results.best_result_index].spread_argument_index
303
fi
304
305
return result
306
si
307
308
signature_for_function(function: Semantic.Symbols.Function) -> SIGNATURE is
309
let parameter_descriptions = Collections.LIST[string]()
310
311
for i in 0..function.arguments.count do
312
parameter_descriptions.add(function.get_short_argument_description(i))
313
od
314
315
return SIGNATURE(function.short_description, parameter_descriptions)
316
si
317
318
// Signature for a callee whose type is a function type. Function
319
// types carry no parameter names, so the label and parameter
320
// descriptions are the formal types alone: `(int, string) -> bool`.
321
help_for_function_type(function_type: Type) -> SIGNATURE_HELP_RESULT? is
322
let arguments = function_type.arguments
323
324
let formal_count =
325
if function_type.is_action then
326
arguments.count
327
else
328
arguments.count - 1
329
fi
330
331
if formal_count < 0 then
332
return null
333
fi
334
335
let parameter_descriptions = Collections.LIST[string]()
336
337
for i in 0..formal_count do
338
parameter_descriptions.add(arguments[i].short_description)
339
od
340
341
let label = System.Text.StringBuilder()
342
343
label.append('(')
344
parameter_descriptions |> append_to(label, ", ")
345
label.append(") -> ")
346
347
if function_type.is_action then
348
label.append("void")
349
else
350
label.append(arguments[arguments.count - 1].short_description)
351
fi
352
353
return SIGNATURE_HELP_RESULT(
354
Collections.LIST[SIGNATURE]([SIGNATURE(label.to_string(), parameter_descriptions)]),
355
0
356
)
357
si
358
si
359
si