-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsignature-help.jl
More file actions
481 lines (435 loc) · 17.4 KB
/
signature-help.jl
File metadata and controls
481 lines (435 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
using .JS
using .JL
# initialization
# ==============
signature_help_options() = SignatureHelpOptions(;
triggerCharacters = ["(", ",", ";", "\"", "=", " "],
retriggerCharacters = ["."])
const SIGNATURE_HELP_REGISTRATION_ID = "jetls-signature-help"
const SIGNATURE_HELP_REGISTRATION_METHOD = "textDocument/signatureHelp"
const CALL_KINDS = JS.KSet"call macrocall dotcall"
function signature_help_registration()
(; triggerCharacters, retriggerCharacters) = signature_help_options()
return Registration(;
id = SIGNATURE_HELP_REGISTRATION_ID,
method = SIGNATURE_HELP_REGISTRATION_METHOD,
registerOptions = SignatureHelpRegistrationOptions(;
documentSelector = DEFAULT_DOCUMENT_SELECTOR,
triggerCharacters,
retriggerCharacters))
end
# For dynamic registrations during development
# unregister(currently_running, Unregistration(;
# id=SIGNATURE_HELP_REGISTRATION_ID,
# method=SIGNATURE_HELP_REGISTRATION_METHOD))
# register(currently_running, signature_help_registration())
# utils
# =====
"""
Return (args, first_kwarg_i), one SyntaxTree per argument to call. Ignore function
name and K"error" (e.g. missing closing paren)
"""
function flatten_args(call::JL.SyntaxTree)
if kind(call) === K"where"
return flatten_args(call[1])
end
@assert kind(call) in CALL_KINDS
usable = (arg::JL.SyntaxTree) -> kind(arg) != K"error"
orig = filter(usable, JS.children(call)[2:end])
args = JL.SyntaxList(orig.graph)
kw_i = 1
for i in eachindex(orig)
iskw = kind(orig[i]) === K"parameters"
if !iskw
push!(args, orig[i])
kw_i += 1
elseif i === lastindex(orig) && iskw
for p in filter(usable, JS.children(orig[i]))
push!(args, p)
end
end
end
return args, kw_i
end
"""
Get K"Identifier" tree from a kwarg tree (child of K"call" or K"parameters").
`sig`: treat this as a signature rather than a call
a => a
(= a 1) => a
(= (:: a T) 1) => a # only when sig=true
"""
function kwname(a::JL.SyntaxTree; sig=false)
if kind(a) === K"Identifier"
return a
elseif kind(a) === K"=" && kind(a[1]) === K"Identifier"
return a[1]
elseif sig && kind(a) === K"=" && kind(a[1]) === K"::" && kind(a[1][1]) === K"Identifier"
return a[1][1]
elseif kind(a) === K"..."
return nothing
end
JETLS_DEV_MODE && @info "Unknown kwarg form" a
return nothing
end
"""
Best-effort mapping of kwname to position in `args`. args[kw_i] and later are
after the semicolon. False negatives are fine here; false positives would hide
signatures.
If `sig`, then K"=" trees before the semicolon should be interpreted as optional
positional args instead of kwargs.
Keywords should be ignored if `cursor` is within the keyword's name.
"""
function find_kws(args::JL.SyntaxList, kw_i::Int; sig=false, cursor::Int=-1)
out = Dict{String, Int}()
for i in (sig ? (kw_i:lastindex(args)) : eachindex(args))
(kind(args[i]) != K"=") && i < kw_i && continue
n = kwname(args[i]; sig)
if !isnothing(n) && !(JS.first_byte(n) <= cursor <= JS.last_byte(n) + 1)
out[n.name_val] = i
end
end
return out
end
"""
Information from one call's arguments for filtering signatures.
- args: Every valid child of the K"call" and its K"parameters" if present
- kw_i: One plus the number of args not in K"parameters" (semicolon)
- pos_map: Map from position in `args` to (min, max) possible positional arg
e.g. f(a, k=1, b..., c)
--> a => (1, 1), b => (2, nothing), c => (2, nothing)
- pos_args_*: lower and upper bounds on # of positional args
- kw_map: kwname => position in `args`. Excludes any WIP kw (see find_kws)
- kind: Item in `CALL_KINDS`
TODO: types
"""
struct CallArgs
args::JL.SyntaxList
kw_i::Int
pos_map::Dict{Int, Tuple{Int, Union{Int, Nothing}}}
pos_args_lb::Int
pos_args_ub::Union{Int, Nothing}
kw_map::Dict{String, Int}
kind::JS.Kind
end
function CallArgs(st0::JL.SyntaxTree, cursor::Int)
@assert !(-1 in JS.byte_range(st0))
args, kw_i = flatten_args(st0)
pos_map = Dict{Int, Tuple{Int, Union{Int, Nothing}}}()
lb = 0; ub = 0
for i in eachindex(args[1:kw_i-1])
if kind(args[i]) === K"..."
ub = nothing
pos_map[i] = (lb + 1, ub)
elseif kind(args[i]) != K"="
lb += 1
!isnothing(ub) && (ub += 1)
pos_map[i] = (lb, ub)
end
end
kw_map = find_kws(args, kw_i; sig=false, cursor)
CallArgs(args, kw_i, pos_map, lb, ub, kw_map, kind(st0))
end
"""
Return `false` if we can definitely rule out `f(args...|` from being a call to `m`
"""
function compatible_method(m::Method, ca::CallArgs)
# TODO: (later) This should use type information from args (which we already
# have from m's params). For now, just parse the method signature like we
# do in make_siginfo.
@static if VERSION ≥ v"1.13.0-DEV.710"
msig = sprint(show, m; context=(:compact=>true, :print_method_signature_only=>true))
else
mstr = sprint(show, m; context=(:compact=>true))
msig_locinfo = split(mstr, " @ ")
length(msig_locinfo) == 2 || return false
msig = strip(msig_locinfo[1])
end
if ca.kind === K"macrocall" # hack. TODO delete
msig = replace(msig, "__source__::LineNumberNode, __module__::Module, "=>"",
"__source__::LineNumberNode, __module__::Module"=>""; count=1)
end
mnode = JS.parsestmt(JL.SyntaxTree, msig; ignore_errors=true)
params, kwp_i = flatten_args(mnode)
has_var_params = kwp_i > 1 && kind(params[kwp_i - 1]) === K"..."
has_var_kwp = kwp_i <= length(params) && kind(params[end]) === K"..."
kwp_map = find_kws(params, kwp_i; sig=true)
!has_var_params && (ca.pos_args_lb >= kwp_i) && return false
!has_var_kwp && !(keys(ca.kw_map) ⊆ keys(kwp_map)) && return false
return true
end
# LSP objects and handler
# =======================
function make_paraminfo(p::JL.SyntaxTree)
# A parameter's `label` is either a string the client searches for, or
# an inclusive-exclusive range within in the signature.
srcloc = (x::JL.SyntaxTree) -> let r = JS.byte_range(x);
[UInt(r.start-1), UInt(r.stop)]
end
# defaults: whole parameter expression
label = srcloc(p)
documentation = string('`', JS.sourcetext(p), '`')
if JS.is_leaf(p)
documentation = nothing
elseif kind(p) === K"="
@assert JS.numchildren(p) === 2
label = kwname(p; sig=true).name_val
elseif kind(p) === K"::"
if JS.numchildren(p) === 1
documentation = "(unused) " * documentation
else
@assert JS.numchildren(p) === 2
label = srcloc(p[1])
end
elseif kind(p) === K"..."
label = make_paraminfo(p[1]).label
end
# do clients tolerate string labels better?
# if !isa(label, String)
# label = string(p.source.file[label[1]+1:label[2]])
# end
if documentation !== nothing
documentation = MarkupContent(;
kind = MarkupKind.Markdown,
value = documentation)
end
return ParameterInformation(; label, documentation)
end
# active_arg is either an argument index, or :next (available pos. arg), or :none
function make_siginfo(m::Method, ca::CallArgs, active_arg::Union{Int, Symbol};
postprocessor::LSPostProcessor = LSPostProcessor())
# methodshow prints "f(x::T) [unparseable stuff]"
# parse the first part and put the remainder in documentation
@static if VERSION ≥ v"1.13.0-DEV.710"
msig = sprint(show, m; context=(:compact=>true, :print_method_signature_only=>true))
else
mstr = sprint(show, m; context=(:compact=>true))
msig_locinfo = split(mstr, " @ ")
length(msig_locinfo) == 2 || return false
msig = strip(msig_locinfo[1])
end
if ca.kind === K"macrocall"
msig = replace(msig, "__source__::LineNumberNode, __module__::Module, "=>"",
"__source__::LineNumberNode, __module__::Module"=>""; count=1)
end
msig = postprocessor(msig)
mnode = JS.parsestmt(JL.SyntaxTree, msig; ignore_errors=true)
label = String(msig)
documentation = let
mdl = postprocessor(string(Base.parentmodule(m)))
file, line = Base.updated_methodloc(m)
filename = to_full_path(file)
MarkupContent(;
kind = MarkupKind.Markdown,
value = "@ `$(mdl)` " * create_source_location_link(filename2uri(filename); line))
end
# We could show the full docs, but there isn't a way to resolve items lazily
# like completions, so we might be sending many copies. The user may have
# seen this already in the completions UI, too.
# documentation = MarkupContent(;
# kind = MarkupKind.Markdown,
# value = string(Base.Docs.doc(Base.Docs.Binding(m.var"module", m.name))))
params, kwp_i = flatten_args(mnode)
maybe_var_params = kwp_i > 1 && kind(params[kwp_i - 1]) === K"..." ?
kwp_i - 1 : nothing
maybe_var_kwp = kwp_i <= length(params) && kind(params[end]) === K"..." ?
lastindex(params) : nothing
kwp_map = find_kws(params, kwp_i; sig=true)
# Map active arg to active param, or nothing
activeParameter = let i = active_arg
if i === :none
nothing
elseif i === :next # next pos arg if able
kwp_i > ca.kw_i ? ca.kw_i : nothing
elseif i in keys(ca.pos_map)
lb, ub = get(ca.pos_map, i, (1, nothing))
if !isnothing(maybe_var_params) && lb >= maybe_var_params
maybe_var_params
else
lb === ub ? lb : nothing
end
elseif kind(ca.args[i]) === K"..."
# splat after semicolon
maybe_var_kwp
elseif kind(ca.args[i]) === K"=" || i >= ca.kw_i
n = kwname(ca.args[i]).name_val # we don't have a backwards mapping
out = get(kwp_map, n, nothing)
isnothing(out) ? maybe_var_kwp : out
else
JETLS_DEV_MODE && @info "No active arg" i ca.args[i]
nothing
end
end
!isnothing(activeParameter) && (activeParameter -= 1) # shift to 0-based
parameters = map(make_paraminfo, params)
return SignatureInformation(; label, documentation, parameters, activeParameter)
end
const empty_siginfos = SignatureInformation[]
"""
Return the last byte at or before `b` that isn't in whitespace or a comment (or
if `pass_newlines=false`, also isn't a newline).
"""
function prev_nontrivia_byte(ps::JS.ParseStream, b::Int; pass_newlines=false)
ti = get_current_token_idx(ps, min(JS.last_byte(ps), b))
isnothing(ti) && return nothing
skip = (i::Int) -> let tok = ps.tokens[i]; k = kind(tok)
b < tok.next_byte || JS.is_whitespace(k) && (pass_newlines || k != K"NewlineWs")
end
while skip(ti)
ti -= 1
ti < 1 && return nothing
end
return ps.tokens[ti].next_byte - 1
end
function is_relevant_call(call::JL.SyntaxTree)
kind(call) in CALL_KINDS &&
# don't show help for a+b, M', etc., where call[1] isn't the function
!(JS.is_infix_op_call(call) || JS.is_postfix_op_call(call))
end
# If parents of our call are like (macro/function (where (where... (call |) ...))),
# we're actually in a declaration, and shouldn't show signature help.
function call_is_decl(_bas::JL.SyntaxList, i::Int, _basᵢ::JL.SyntaxTree = _bas[i])
kind(_basᵢ) != JS.K"call" && return false
j = i + 1
while j <= lastindex(_bas) && kind(_bas[j]) === JS.K"where"
j += 1
end
return j <= lastindex(_bas) &&
kind(_bas[j]) in JS.KSet"macro function" &&
# in `f(x) = g(x)`, return true in `f`, false in `g`
_bas[j - 1] === _bas[j][1]
end
# Find cases where a macro call is not surrounded by parentheses
# and the current cursor position is on a different line from the `@` macro call
function is_crossline_noparen_macrocall(call::JL.SyntaxTree, cursor_byte::Int)
return noparen_macrocall(call) && let source_file = JS.sourcefile(call)
# Check if cursor is on a different line from the @ symbol
JS.numchildren(call) ≥ 1 &&
JS.source_line(source_file, JS.first_byte(call[1])) ≠ JS.source_line(source_file, cursor_byte)
end
end
"""
Return the nearest call in `st0` containing cursor byte b (if any).
Some adjustment is done if there's trivia before the cursor in an unterminated
call expression, e.g. `foo(#=hi=# |`, `@bar |`. A more accurate description
would be: return the nearest call in `st0` such that stuff inserted at the
cursor would be descendents of it.
"""
function cursor_call(ps::JS.ParseStream, st0::JL.SyntaxTree, b::Int)
# disable signature help if invoked within comment scope
prev_token_idx = get_prev_token_idx(ps, b)
if !isnothing(prev_token_idx) && JS.kind(ps.tokens[prev_token_idx]) === K"Comment"
return nothing
end
bas = byte_ancestors(st0, b)
i = findfirst(is_relevant_call, bas)
if !isnothing(i)
basᵢ = bas[i]
if call_is_decl(bas, i, basᵢ)
return nothing
elseif is_crossline_noparen_macrocall(basᵢ, b)
# Consider cases like:
# @testset begin
# ... | ...
# end
return nothing
elseif any(j::Int->JS.kind(bas[j])===JS.K"do", 1:i)
# bail out if this is actually within a `do` block
return nothing
end
return basᵢ
end
# `i` is nothing. Eat preceding whitespace and check again.
pnb_line = prev_nontrivia_byte(ps, b; pass_newlines=false)
pnb = prev_nontrivia_byte(ps, b; pass_newlines=true)
(isnothing(pnb) || pnb === b) && return nothing
bas = byte_ancestors(st0, pnb)
# If the previous nontrivia byte is part of a call or macrocall, and it is
# missing a closing paren, use that.
i = findfirst(st::JL.SyntaxTree -> is_relevant_call(st) && !noparen_macrocall(st), bas)
if !isnothing(i)
basᵢ = bas[i]
if JS.is_error(JS.children(basᵢ)[end])
return call_is_decl(bas, i, basᵢ) ? nothing : basᵢ
end
end
(isnothing(pnb_line) || pnb_line === b) && return nothing
bas = byte_ancestors(st0, pnb_line)
# If the previous nontrivia byte within this line is part of an
# unparenthesized macrocall, use that.
i = findfirst(noparen_macrocall, bas)
return isnothing(i) ? nothing : bas[i]
end
function cursor_siginfos(mod::Module, fi::FileInfo, b::Int, analyzer::LSAnalyzer;
postprocessor::LSPostProcessor=LSPostProcessor())
st0 = build_tree!(JL.SyntaxTree, fi)
call = cursor_call(fi.parsed_stream, st0, b)
isnothing(call) && return empty_siginfos
after_semicolon = let
params_i = findfirst(st -> kind(st) === K"parameters", JS.children(call))
!isnothing(params_i) && b > JS.first_byte(call[params_i])
end
# TODO: We could be calling a local variable. If it shadows a method, our
# ignoring it is misleading. We need to either know about local variables
# in this scope (maybe by caching completion info) or duplicate some work.
fntyp = resolve_type(analyzer, mod, call[1])
fntyp isa Core.Const || return empty_siginfos
fn = fntyp.val
candidate_methods = methods(fn)
isempty(candidate_methods) && return empty_siginfos
ca = CallArgs(call, b)
# Influence parameter highlighting by selecting the active argument (which
# may be mapped to a parameter in make_siginfo). If cursor is after all
# pos. args and not after semicolon, ask for the next param, which may not
# exist. Otherwise, highlight the param for the arg we're in.
#
# We don't keep commas---do we want the green node here?
active_arg = let no_args = ca.kw_i === 1,
past_pos_args = no_args || b > JS.last_byte(ca.args[ca.kw_i - 1]) + 1
if past_pos_args && !after_semicolon
:next
else
arg_i = findfirst(a -> JS.first_byte(a) <= b <= JS.last_byte(a) + 1, ca.args)
isnothing(arg_i) ? :none : arg_i
end
end
out = SignatureInformation[]
for m in candidate_methods
if compatible_method(m, ca)
siginfo = make_siginfo(m, ca, active_arg; postprocessor)
if siginfo !== nothing
push!(out, siginfo)
end
end
end
return out
end
"""
`textDocument/signatureHelp` is requested when one of the negotiated trigger characters is typed.
Some clients, e.g. Eglot (emacs), requests it more frequently.
"""
function handle_SignatureHelpRequest(server::Server, msg::SignatureHelpRequest)
state = server.state
uri = msg.params.textDocument.uri
fi = get_file_info(state, uri)
if fi === nothing
return send(server,
SignatureHelpResponse(;
id = msg.id,
result = nothing,
error = file_cache_error(uri)))
end
(; mod, analyzer, postprocessor) = get_context_info(state, uri, msg.params.position)
b = xy_to_offset(fi, msg.params.position)
signatures = cursor_siginfos(mod, fi, b, analyzer; postprocessor)
activeSignature = nothing
activeParameter = nothing
return send(server,
SignatureHelpResponse(;
id = msg.id,
result = isempty(signatures) ?
null
: SignatureHelp(;
signatures,
activeSignature,
activeParameter)))
end