-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_signature_help.jl
More file actions
472 lines (423 loc) · 18.2 KB
/
test_signature_help.jl
File metadata and controls
472 lines (423 loc) · 18.2 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
module test_signature_help
using Test
using JETLS
using JETLS: JL, JS
using JETLS.URIs2
using JETLS.Analyzer: LSAnalyzer
# siginfos(mod, code) -> siginfos
# nsigs(mod, code) -> n
function siginfos(mod::Module, code::AbstractString; kwargs...)
clean_code, positions = JETLS.get_text_and_positions(code; kwargs...)
@assert length(positions) == 1 "siginfos requires exactly one cursor marker"
position = only(positions)
fi = JETLS.FileInfo(0, clean_code, @__FILE__)
b = JETLS.xy_to_offset(fi, position)
return JETLS.cursor_siginfos(mod, fi, b, LSAnalyzer())
end
n_si(args...) = length(siginfos(args...))
module M_sanity
i_exist(a,b,c) = 0
struct StrctExist1; s; end # just 1 construct (w/o the conversion method)
struct StrctExist2; s::String; end # just 1 construct (w/o the conversion method)
struct StrctExist3
s
StrctExist3(@nospecialize s) = new(s)
end
end
@testset "sanity" begin
@test 1 == n_si(M_sanity, "i_exist(│)")
@test 1 == n_si(M_sanity, "i_exist(1,2,3│)")
@test 1 == n_si(M_sanity, "i_exist(│1,2,3)")
@test 0 == n_si(M_sanity, "i_do_not_exist(│)")
@test 0 == n_si(M_sanity, "│")
@test 0 == n_si(M_sanity, "(│)")
@test 0 == n_si(M_sanity, "()│")
@test 1 == n_si(M_sanity, "StrctExist1(│)")
@test 0 == n_si(M_sanity, "StrctExist1(1,2│)")
@test 2 == n_si(M_sanity, "StrctExist2(│)")
@test 0 == n_si(M_sanity, "StrctExist2(1,2│)")
@test 1 == n_si(M_sanity, "StrctExist3(│)")
@test 0 == n_si(M_sanity, "StrctExist3(1,2│)")
end
module M_macros
macro m(x, y=1); x; end
macro v(x...); x; end
end
@testset "simple macros" begin
@test 2 == n_si(M_macros, "@m(│)")
@test 2 == n_si(M_macros, "@m│")
@test 2 == n_si(M_macros, "@m │")
@test 2 == n_si(M_macros, "begin\n @m │\nend")
@test 0 == n_si(M_macros, "begin\n @m 1\n│end")
@test 0 == n_si(M_macros, "begin\n @m 1\n│\nend")
@test 2 == n_si(M_macros, "@m(1,│)")
@test 2 == n_si(M_macros, "@m 1│")
@test 1 == n_si(M_macros, "@m(1,2,│)")
@test 1 == n_si(M_macros, "@m 1 2│")
@test 0 == n_si(M_macros, "@m(1,2,3,│)")
@test 0 == n_si(M_macros, "@m 1 2 3│")
@test 1 == n_si(M_macros, "@v│")
@test 1 == n_si(M_macros, "@v 1 2 3 4│")
end
module M_dotcall
f(x) = 0
end
@testset "dotcall" begin
@test 1 == n_si(M_dotcall, "f.(│)")
@test 1 == n_si(M_dotcall, "f.(x│)")
end
module M_edgecases
kwname(var"end") = var"end"
end
@testset "Edge cases" begin
@test 1 == n_si(M_edgecases, "kwname(│)")
let si = only(siginfos(M_edgecases, "kwname(│)"))
@test si.label == "kwname(var\"end\")"
end
end
module M_noshow_def
f(x) = x
g(x) = x
macro m(x); x; end
end
@testset "don't show help in method definitions" begin
snippets = [
"function f(│); end",
"function f(│) where T; end",
"function f(│) where T where T; end",
"f(│) = 1",
"f(│) where T = 1",
"│f(x) = g(x)",
"f(x│) = g(x)",
"f(x)│ = g(x)",
"f(x::T│) where T = g(x)",
]
for s in snippets
@test 0 == n_si(M_noshow_def, s)
end
@test 1 == n_si(M_noshow_def, "f(x) = g(│)")
@test 1 == n_si(M_noshow_def, "f(x) = g(x │)")
@test 1 == n_si(M_noshow_def, "f(x) where T where U = g(x│)")
@test 1 == n_si(M_noshow_def, "f(x) where T where U = @m(x│)")
end
module M_filterp
f4() = 0
f4(a) = 0
f4(a,b) = 0
f4(a,b,c) = 0
f4(a,b,c,d) = 0
f1v() = 0
f1v(a) = 0
f1v(a, args...) = 0
end
@testset "filter by number of positional args" begin
@test 5 == n_si(M_filterp, "f4(│)")
@test 4 == n_si(M_filterp, "f4(1│)")
@test 4 == n_si(M_filterp, "f4(1,│)")
@test 4 == n_si(M_filterp, "f4(1, │)")
@test 3 == n_si(M_filterp, "f4(1,2│)")
@test 2 == n_si(M_filterp, "f4(1,2,3│)")
@test 1 == n_si(M_filterp, "f4(1,2,3,4,│)")
@test 1 == n_si(M_filterp, "f4(│1,2,3,4,)")
@test 1 == n_si(M_filterp, "f4(1,2,3,4; │)")
# splat should be assumed empty for filtering purposes
@test 1 == n_si(M_filterp, "f4(1,2,3,4,x...│)")
@test 1 == n_si(M_filterp, "f4(x...,1,2,3,4,│)")
@test 3 == n_si(M_filterp, "f1v(│)")
@test 2 == n_si(M_filterp, "f1v(1,│)")
@test 1 == n_si(M_filterp, "f1v(1,2│)")
@test 1 == n_si(M_filterp, "f1v(1,2,3│)")
@test 1 == n_si(M_filterp, "f1v(1,2,3,foo...│)")
end
module M_filterk
f(;kw1, kw2=2, kw3::Int=3) = 0
f(x; kw2, kw3, kw4, kw5, kw6) = 0
end
@testset "filter by names of kwargs" begin
@test 2 == n_si(M_filterk, "f(│)")
# pre-semicolon
@test 0 == n_si(M_filterk, "f(1, kw1│)") # positional until we type "="
@test 1 == n_si(M_filterk, "f(kw1=1│)")
# post-semicolon
@test 1 == n_si(M_filterk, "f(│;kw1)")
@test 1 == n_si(M_filterk, "f(;kw1,│)")
@test 1 == n_si(M_filterk, "f(;kw1=│)")
@test 1 == n_si(M_filterk, "f(;kw1=1│)")
# mix
@test 1 == n_si(M_filterk, "f(kw2=2,kw3=3;│)")
@test 1 == n_si(M_filterk, "f(kw2=2; kw3=3│)")
@test 0 == n_si(M_filterk, "f(kw2=2; kw6=6│)")
# When nothing before semicolon, filter methods with required positional args
# (regardless of whether cursor is editing a kwarg name)
@test 1 == n_si(M_filterk, "f(;kw1│)")
@test 1 == n_si(M_filterk, "f(;kw1│=1)")
@test 1 == n_si(M_filterk, "f(;kw│1)")
@test 1 == n_si(M_filterk, "f(;│kw1)")
@test 1 == n_si(M_filterk, "f(;kw1=1, kw1│)")
end
module M_pos_vs_kw
f(a::Int, b::Int) = 0
f(; a, b) = 0
g(x::Int, y::Int) = 0
g(x::Int; kw=nothing) = 0
h(; kw) = 0
h(x::Int; kw) = 0
i(x, y) = 0
i(; kw) = 0
end
@testset "filter positional-only methods when semicolon is present" begin
# Without semicolon, both methods are shown
@test 2 == n_si(M_pos_vs_kw, "f(│)")
# With semicolon but no kwarg yet, only keyword-accepting methods should match
@test 1 == n_si(M_pos_vs_kw, "f(;│)")
@test 1 == n_si(M_pos_vs_kw, "g(42;│)") # Should exclude `g(::Int, ::Int)`
# With semicolon and kwarg, only keyword-accepting methods should match
@test 1 == n_si(M_pos_vs_kw, "f(;a,│)")
@test 1 == n_si(M_pos_vs_kw, "f(;a=1│)")
# Filter out methods with required positional args when semicolon is present
@test 2 == n_si(M_pos_vs_kw, "h(│)") # Without semicolon, both shown
@test 1 == n_si(M_pos_vs_kw, "h(;│)") # h(x::Int; kw) has required pos arg, filtered
@test 1 == n_si(M_pos_vs_kw, "h(;kw│)") # Same filtering applies
# Splat args should not filter out methods with required positional args
# because splat could expand to enough args
@test 2 == n_si(M_pos_vs_kw, "i(│)")
@test 1 == n_si(M_pos_vs_kw, "i(;│)") # Without splat, i(x, y) is filtered
@test 2 == n_si(M_pos_vs_kw, "i(args...;│)") # With splat, i(x, y) should match
@test 1 == n_si(M_pos_vs_kw, "i(a, b...;│)") # i(; kw) filtered (already has pos arg)
end
module M_highlight
f(a0, a1, a2, va3...; kw4=0, kw5=0, kws6...) = 0
f1(x, xs...) = 0
kwfunc(; kw0, kw1, kws2...) = nothing
end
@testset "Active param highlighting" begin
function active_parameter(mod::Module, code::AbstractString; kwargs...)
si = siginfos(mod, code; kwargs...)
Int(@something only(si).activeParameter return nothing)
end
@test 0 == active_parameter(M_highlight, "f(│)")
@test 0 == active_parameter(M_highlight, "f(0│)")
@test 1 == active_parameter(M_highlight, "f(0,│)")
@test 1 == active_parameter(M_highlight, "f(0, │)")
# in vararg
@test 3 == active_parameter(M_highlight, "f(0, 1, 2, 3│)")
@test 3 == active_parameter(M_highlight, "f(0, 1, 2, 3, 3│)")
@test 3 == active_parameter(M_highlight, "f(0, 1, 2, 3, x...│)")
@test 3 == active_parameter(M_highlight, "f(0, 1, 2, x...│)")
@test 1 == active_parameter(M_highlight, "f1(0,│)")
@test 1 == active_parameter(M_highlight, "f1(0, 1│)")
@test 1 == active_parameter(M_highlight, "f1(0, 1,│)")
@test 1 == active_parameter(M_highlight, "f1(0, 1, 2│)")
@test 1 == active_parameter(M_highlight, "f1(0, 1, 2,│)")
@test 1 == active_parameter(M_highlight, "f1(0, 1, 2, 3│)")
@test 1 == active_parameter(M_highlight, "f1(0, 1, 2, 3,│)")
@test 0 == active_parameter(M_highlight, "kwfunc(; │)")
@test 1 == active_parameter(M_highlight, "kwfunc(; kw0,│)")
@test 1 == active_parameter(M_highlight, "kwfunc(; kw0=0,│)")
@test 2 == active_parameter(M_highlight, "kwfunc(; kw0=0,kw1,│)")
@test 2 == active_parameter(M_highlight, "kwfunc(; kw0=0,kw1=1,│)")
@test 2 == active_parameter(M_highlight, "kwfunc(; kw0=0,kw1=1,kw2,│)")
@test 2 == active_parameter(M_highlight, "kwfunc(; kw0=0,kw1=1,kw2=2,│)")
@test 2 == active_parameter(M_highlight, "kwfunc(; kw0=0,kw1=1,kws2...,│)")
# splat contains 0 or more args; use what we know
@test nothing === active_parameter(M_highlight, "f(x...│, 0, 1, 2, 3, x...)")
@test nothing === active_parameter(M_highlight, "f(x..., 0, 1, 2│, 3, x...)")
@test 3 == active_parameter(M_highlight, "f(x..., 0, 1, 2, 3│, x...)")
@test 3 == active_parameter(M_highlight, "f(x..., 0, 1, 2, 3, x...│)")
@test 3 == active_parameter(M_highlight, "f(x..., 0, 1, 2, │x...)")
# various kwarg
@test 4 == active_parameter(M_highlight, "f(0, 1, 2, 3; kw4│)")
@test 4 == active_parameter(M_highlight, "f(0, 1, 2, 3; kw4=0│)")
@test 4 == active_parameter(M_highlight, "f(│kw4=0, 0, 1, 2, 3)")
@test 0 == active_parameter(M_highlight, "f(kw4=0, 0│, 1, 2, 3)")
# # any old kwarg can go in `kws6...`
@test 6 == active_parameter(M_highlight, "f(0, 1, 2, 3; kwfake│)")
@test 6 == active_parameter(M_highlight, "f(0, 1, 2, 3; kwfake=1│)")
@test 6 == active_parameter(M_highlight, "f(kwfake=1│, 0, 1, 2, 3)")
# # splat after semicolon
@test 6 == active_parameter(M_highlight, "f(0, 1, 2, 3; kwfake...│)")
# unrecognized kwarg forms should not crash and return something
@test siginfos(M_highlight, "f(0, 1, 2; a.b=1│)") isa Vector
end
module M_nested
inner(args...) = 0
outer(args...) = 0
end
@testset "nested" begin
active_si(code) = only(siginfos(M_nested, code)).label
@test startswith(active_si("outer(0,1,inner(│))"), "inner")
@test startswith(active_si("outer(0,1,inner()│)"), "outer")
@test startswith(active_si("outer(0,1,│inner())"), "inner") # either is fine really
@test startswith(active_si("outer(0,1│,inner())"), "outer")
@test startswith(active_si("outer(0,1,inner(),│)"), "outer")
@test startswith(active_si("function outer(); inner(│); end"), "inner")
# see through infix and postfix ops, which are parsed as calls
@test startswith(active_si("outer(0,1,2+3│)"), "outer")
@test startswith(active_si("outer(0,1,│2+3)"), "outer")
@test startswith(active_si("outer(0,1,2:│3)"), "outer")
@test startswith(active_si("outer(0,1,3│')"), "outer")
end
# This depends somewhat on what JuliaSyntax does using `ignore_errors=true`,
# which I don't think is specified, but it would be good to know if these common
# cases break.
module M_invalid
f1(a; k=1) = 0
macro m(x); x; end
end
@testset "tolerate extra whitespace and invalid syntax" begin
# unclosed paren: ignore whitespace
@test 1 == n_si(M_invalid, "f1(│")
@test 1 == n_si(M_invalid, "f1( #=comment=# │")
@test 1 == n_si(M_invalid, "f1( \n │")
@test 1 == n_si(M_invalid, "@m(│")
@test 1 == n_si(M_invalid, "@m( #=comment=# │")
@test 1 == n_si(M_invalid, "@m( \n │")
# don't ignore whitespace with closed call
@test 0 == n_si(M_invalid, "f1( \n ) │")
@test 0 == n_si(M_invalid, "@m( \n ) │")
# ignore space but not newlines with no-paren macro
@test 1 == n_si(M_invalid, "@m │")
@test 0 == n_si(M_invalid, "@m\n│")
@test 0 == n_si(M_invalid, "@m \n │")
# no-paren macro signature support should not be triggered after closed string macrocall
@test 0 == n_si(M_invalid, "r\"xxx\"│")
# no-paren macro signature support should not be triggered when the scope surrounding the cursor is a block
@test 0 == n_si(M_invalid, """@m begin
│
end""")
@test 1 == n_si(M_invalid, """@m begin
f1(│)
end""")
# signature help should not be triggered when the scope surrounding the cursor is a do block
@test 0 == n_si(M_invalid, """identity() do x
│
end""")
@test 1 == n_si(M_invalid, """identity() do x
f1(│)
end""")
@test 1 == n_si(M_invalid, "f1(,,,,,,,,,│)")
@test 1 == n_si(M_invalid, "f1(a b c│)")
@test 1 == n_si(M_invalid, "f1(k=│)")
@test 1 == n_si(M_invalid, "f1(k= \n │)")
@test 0 == n_si(M_invalid, "f1(fake= \n │)")
end
module M_argtype_filtering
const gx1 = 42
const gx2 = 43
func(x::Int) = x
func(x::Int, y::Int) = x + y
func(x::Float64) = x
func(x::Float64, y::Float64) = x + y
kwfunc(x::Int, y::Int; kw1=nothing, kw2=nothing) = x, kw1
kwfunc(x::Float64, y::Float64; kw1=nothing, kw2=nothing) = x, kw1
end
@testset "Argument type based filtering" begin
@test 2 == n_si(M_argtype_filtering, "func(1,│)")
@test 1 == n_si(M_argtype_filtering, "func(1,2,│)")
@test 2 == n_si(M_argtype_filtering, "func(gx1,│)")
@test 1 == n_si(M_argtype_filtering, "func(gx1,gx2,│)")
@test 1 == n_si(M_argtype_filtering, "kwfunc(1,│)")
@test 1 == n_si(M_argtype_filtering, "kwfunc(1,2,│)")
@test 1 == n_si(M_argtype_filtering, "kwfunc(1,kw1=nothing,│)")
@test 1 == n_si(M_argtype_filtering, "kwfunc(1,kw1=nothing,2,│)")
@test 1 == n_si(M_argtype_filtering, "kwfunc(1,2;│)")
@test 1 == n_si(M_argtype_filtering, "kwfunc(1,2; kw1=nothing,│)")
@test_broken 2 == n_si(M_argtype_filtering, "let x = 1; func(x,│); end")
end
include("setup.jl")
function with_signature_help_request(tester, text::AbstractString; kwargs...)
clean_code, positions = JETLS.get_text_and_positions(text; kwargs...)
withscript(clean_code) do script_path
uri = filepath2uri(script_path)
withserver() do (; writereadmsg, id_counter)
# run the full analysis first
(; raw_res) = writereadmsg(make_DidOpenTextDocumentNotification(uri, clean_code))
@test raw_res isa PublishDiagnosticsNotification
@test raw_res.params.uri == uri
for (i, pos) in enumerate(positions)
(; raw_res) = writereadmsg(SignatureHelpRequest(;
id = id_counter[] += 1,
params = SignatureHelpParams(;
textDocument = TextDocumentIdentifier(; uri),
position = pos)))
tester(i, raw_res.result, uri, script_path)
end
end
end
end
@testset "signature help request/response cycle" begin
let text = """
foo(xxx) = :xxx
foo(xxx, yyy) = :xxx_yyy
foo(nothing,│)
"""
cnt = 0
with_signature_help_request(text) do _, result, _, script_path
@test length(result.signatures) == 2
@test any(result.signatures) do siginfo
siginfo.label == "foo(xxx)" &&
# this also tests that JETLS doesn't show the nonsensical `var"..."`
# string caused by JET's internal details
occursin("@ `Main` [$(script_path):1]($(filepath2uri(script_path))#L1)",
(siginfo.documentation::MarkupContent).value)
end
@test any(result.signatures) do siginfo
siginfo.label == "foo(xxx, yyy)" &&
# this also tests that JETLS doesn't show the nonsensical `var"..."`
# string caused by JET's internal details
occursin("@ `Main` [$(script_path):2]($(filepath2uri(script_path))#L2)",
(siginfo.documentation::MarkupContent).value)
end
cnt += 1
end
@test cnt == 1
end
# Test with DidChangeTextDocumentNotification
let script_code = """
foo(xxx) = :xxx
foo(xxx, yyy) = :xxx_yyy
"""
withscript(script_code) do script_path
uri = filepath2uri(script_path)
withserver() do (; writereadmsg, id_counter)
# run the full analysis first
(; raw_res) = writereadmsg(make_DidOpenTextDocumentNotification(uri, script_code))
@test raw_res isa PublishDiagnosticsNotification
@test raw_res.params.uri == uri
edited_code = """
foo(xxx) = :xxx
foo(xxx, yyy) = :xxx_yyy
foo(nothing,) # <- cursor set at `,`
"""
writereadmsg(
make_DidChangeTextDocumentNotification(uri, edited_code, #=version=#2);
read = 0)
sleep(2.0) # sleep to propagate the document change to the cache (requried for multithreading env)
let id = id_counter[] += 1
(; raw_res) = writereadmsg(SignatureHelpRequest(;
id,
params = SignatureHelpParams(;
textDocument = TextDocumentIdentifier(; uri),
position = Position(; line=2, character=12))))
@test raw_res isa SignatureHelpResponse
@test length(raw_res.result.signatures) == 2
@test any(raw_res.result.signatures) do siginfo
siginfo.label == "foo(xxx)" &&
# this also tests that JETLS doesn't show the nonsensical `var"..."`
# string caused by JET's internal details
occursin("@ `Main` [$(script_path):1]($(filepath2uri(script_path))#L1)",
(siginfo.documentation::MarkupContent).value)
end
@test any(raw_res.result.signatures) do siginfo
siginfo.label == "foo(xxx, yyy)" &&
# this also tests that JETLS doesn't show the nonsensical `var"..."`
# string caused by JET's internal details
occursin("@ `Main` [$(script_path):2]($(filepath2uri(script_path))#L2)",
(siginfo.documentation::MarkupContent).value)
end
end
end
end
end
end
end # module test_signature_help