forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplcompletions.jl
2490 lines (2214 loc) · 75.6 KB
/
replcompletions.jl
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
using REPL.REPLCompletions
using Test
using Random
using REPL
@testset "Check symbols previously not shown by REPL.doc_completions()" begin
symbols = ["?","=","[]","[","]","{}","{","}",";","","'","&&","||","julia","Julia","new","@var_str"]
for i in symbols
@test i ∈ string.(REPL.doc_completions(i, Main))
end
end
let ex = quote
module CompletionFoo
using Random
import Test
mutable struct Test_y
yy
end
mutable struct Test_x
xx :: Test_y
end
type_test = Test_x(Test_y(1))
(::Test_y)() = "", ""
unicode_αβγ = Test_y(1)
Base.:(+)(x::Test_x, y::Test_y) = Test_x(Test_y(x.xx.yy + y.yy))
module CompletionFoo2
end
const bar = 1
foo() = bar
macro foobar()
:()
end
macro barfoo(ex)
ex
end
macro error_expanding()
error("cannot expand @error_expanding")
:()
end
macro error_lowering_conditional(a)
if isa(a, Number)
return a
end
throw(AssertionError("Not a Number"))
:()
end
macro error_throwing()
return quote
error("@error_throwing throws an error")
end
end
primitive type NonStruct 8 end
Base.propertynames(::NonStruct) = (:a, :b, :c)
x = reinterpret(NonStruct, 0x00)
# Support non-Dict AbstractDicts, #19441
mutable struct CustomDict{K, V} <: AbstractDict{K, V}
mydict::Dict{K, V}
end
Base.keys(d::CustomDict) = collect(keys(d.mydict))
Base.length(d::CustomDict) = length(d.mydict)
# Support AbstractDict with unknown length, #55931
struct NoLengthDict{K,V} <: AbstractDict{K,V}
dict::Dict{K,V}
NoLengthDict{K,V}() where {K,V} = new(Dict{K,V}())
end
Base.iterate(d::NoLengthDict, s...) = iterate(d.dict, s...)
Base.IteratorSize(::Type{<:NoLengthDict}) = Base.SizeUnknown()
Base.eltype(::Type{NoLengthDict{K,V}}) where {K,V} = Pair{K,V}
Base.setindex!(d::NoLengthDict, v, k) = d.dict[k] = v
test(x::T, y::T) where {T<:Real} = pass
test(x::Real, y::Real) = pass
test(x::AbstractArray{T}, y) where {T<:Real} = pass
test(args...) = pass
test1(x::Type{Float64}) = pass
test2(x::AbstractString) = pass
test2(x::Char) = pass
test2(x::Cmd) = pass
test3(x::AbstractArray{Int}, y::Int) = pass
test3(x::AbstractArray{Float64}, y::Float64) = pass
test4(x::AbstractString, y::AbstractString) = pass
test4(x::AbstractString, y::Regex) = pass
test5(x::Array{Bool,1}) = pass
test5(x::BitArray{1}) = pass
test5(x::Float64) = pass
const a=x->x
test6()=[a, a]
test7() = rand(Bool) ? 1 : 1.0
test8() = Any[1][1]
test9(x::Char) = pass
test9(x::Char, i::Int) = pass
test10(a, x::Int...) = pass
test10(a::Integer, b::Integer, c) = pass
test10(a, y::Bool...) = pass
test10(a, d::Integer, z::Signed...) = pass
test10(s::String...) = pass
test11(a::Integer, b, c) = pass
test11(u, v::Integer, w) = pass
test11(x::Int, y::Int, z) = pass
test11(_, _, s::String) = pass
test!12() = pass
kwtest(; x=1, y=2, w...) = pass
kwtest2(a; x=1, y=2, w...) = pass
kwtest3(a::Number; length, len2, foobar, kwargs...) = pass
kwtest3(a::Real; another!kwarg, len2) = pass
kwtest3(a::Integer; namedarg, foobar, slurp...) = pass
kwtest4(a::AbstractString; _a1b, x23) = pass
kwtest4(a::String; _a1b, xαβγ) = pass
kwtest4(a::SubString; x23, _something) = pass
kwtest5(a::Int, b, x...; somekwarg, somekotherkwarg) = pass
kwtest5(a::Char, b; xyz) = pass
const named = (; len2=3)
const fmsoebelkv = (; len2=3)
array = [1, 1]
varfloat = 0.1
const tuple = (1, 2)
test_y_array=[(@__MODULE__).Test_y(rand()) for i in 1:10]
test_dict = Dict("abc"=>1, "abcd"=>10, :bar=>2, :bar2=>9, Base=>3,
occursin=>4, `ls`=>5, 66=>7, 67=>8, ("q",3)=>11,
"α"=>12, :α=>13)
test_customdict = CustomDict(test_dict)
macro teststr_str(s) end
macro tϵsτstρ_str(s) end
macro testcmd_cmd(s) end
macro tϵsτcmδ_cmd(s) end
var"complicated symbol with spaces" = 5
struct WeirdNames end
Base.propertynames(::WeirdNames) = (Symbol("oh no!"), Symbol("oh yes!"))
# https://github.com/JuliaLang/julia/issues/52551#issuecomment-1858543413
export exported_symbol
exported_symbol(::WeirdNames) = nothing
end # module CompletionFoo
test_repl_comp_dict = CompletionFoo.test_dict
test_repl_comp_customdict = CompletionFoo.test_customdict
test_dict_ℂ = Dict(1=>2)
test_dict_no_length = CompletionFoo.NoLengthDict{Int,Int}()
end
ex.head = :toplevel
Core.eval(Main, ex)
end
function map_completion_text(completions)
c, r, res = completions
return map(completion_text, c), r, res
end
test_complete(s) = map_completion_text(@inferred(completions(s, lastindex(s))))
test_scomplete(s) = map_completion_text(@inferred(shell_completions(s, lastindex(s))))
test_bslashcomplete(s) = map_completion_text(@inferred(bslash_completions(s, lastindex(s)))[2])
test_complete_context(s, m=@__MODULE__; shift::Bool=true) =
map_completion_text(@inferred(completions(s,lastindex(s), m, shift)))
test_complete_foo(s) = test_complete_context(s, Main.CompletionFoo)
test_complete_noshift(s) = map_completion_text(@inferred(completions(s, lastindex(s), Main, false)))
test_methods_list(@nospecialize(f), tt) = map(x -> string(x.method), Base._methods_by_ftype(Base.signature_type(f, tt), 10, Base.get_world_counter()))
module M32377 end
test_complete_32377(s) = map_completion_text(completions(s,lastindex(s), M32377))
macro test_nocompletion(s)
tests = [
:(@test c == String[]),
:(@test res === false)
]
for t in tests
t.args[2] = __source__ # fix the LineNumberNode
end
return Expr(:let, Expr(:(=), :((c, _, res)), :(test_complete($(esc(s))))), Expr(:block, tests...))
end
let s = ""
c, r = test_complete(s)
@test "CompletionFoo" in c
@test isempty(r)
@test s[r] == ""
end
let s = "using REP"
c, r = test_complete_32377(s)
@test count(isequal("REPL"), c) == 1
# issue #30234
@test !Base.isbindingresolved(M32377, :tanh)
# check what happens if REPL is already imported
M32377.eval(:(using REPL))
c, r = test_complete_32377(s)
@test count(isequal("REPL"), c) == 1
end
let s = "Comp"
c, r = test_complete(s)
@test "CompletionFoo" in c
@test r == 1:4
@test s[r] == "Comp"
end
let s = "Main.Comp"
c, r = test_complete(s)
@test "CompletionFoo" in c
@test r == 6:9
@test s[r] == "Comp"
end
let s = "Main.CompletionFoo."
c, r = test_complete(s)
@test "bar" in c
@test r === 20:19
@test s[r] == ""
end
let s = "Main.CompletionFoo.f"
c, r = test_complete(s)
@test "foo" in c
@test r == 20:20
@test s[r] == "f"
@test !("foobar" in c)
end
# test method completions when `!` operator precedes
let
s = "!is"
c, r = test_complete(s)
@test "isa" in c
@test s[r] == "is"
@test !("!" in c)
s = "!!is"
c, r = test_complete(s)
@test "isa" in c
@test s[r] == "is"
@test !("!" in c)
end
# issue #6424
let s = "Main.CompletionFoo.@f"
c, r = test_complete(s)
@test "@foobar" in c
@test r == 20:21
@test s[r] == "@f"
@test !("foo" in c)
end
let s = "Main.CompletionFoo.type_test.x"
c, r = test_complete(s)
@test "xx" in c
@test r == 30:30
@test s[r] == "x"
end
let s = "Main.CompletionFoo.unicode_αβγ.y"
c, r = test_complete(s)
@test "yy" in c
end
let s = "Main.CompletionFoo.bar.no_val_available"
c, r = test_complete(s)
@test length(c)==0
end
#cannot do dot completion on infix operator
let s = "+."
c, r = test_complete(s)
@test length(c)==0
end
# To complete on a variable of a type, the type T of the variable
# must be a concrete type, hence Base.isstructtype(T) returns true,
# for the completion to succeed. That why `xx :: Test_y` of `Test_x`.
let s = "Main.CompletionFoo.type_test.xx.y"
c, r = test_complete(s)
@test "yy" in c
@test r == 33:33
@test s[r] == "y"
end
# issue #6333
let s = "Base.return_types(getin"
c, r = test_complete(s)
@test "getindex" in c
@test r == 19:23
@test s[r] == "getin"
end
# issue #23193: after `using`, identifiers can be prefixed by module names
let s = "using Test, Random"
c, r = test_complete(s)
@test !("RandomDevice" in c)
end
# issue #23226: identifiers must be separated by a comma (not a newline)
let s = "using Base\nusi"
c, r = test_complete(s)
@test "using" in c
end
# issue 23292
let
@test_nowarn test_complete("test7().")
c, r = test_complete("test8().")
@test isempty(c)
end
# issue 46800: (3,2).<TAB> errors in the REPL
let
c, r = test_complete("(3,2).")
@test isempty(c)
end
# inexistent completion inside a string
@test_nocompletion("Base.print(\"lol")
# inexistent completion inside a cmd
@test_nocompletion("run(`lol")
# issue 55856: copy(A').<TAB> errors in the REPL
let
c, r = test_complete("copy(A').")
@test isempty(c)
end
# test latex symbol completions
let s = "\\alpha"
c, r = test_bslashcomplete(s)
@test c[1] == "α"
@test r == 1:lastindex(s)
@test length(c) == 1
end
# test latex symbol completions after unicode #9209
let s = "α\\alpha"
c, r = test_bslashcomplete(s)
@test c[1] == "α"
@test r == 3:sizeof(s)
@test length(c) == 1
end
# test emoji symbol completions
let s = "\\:koala:"
c, r = test_bslashcomplete(s)
@test c[1] == "🐨"
@test r == 1:sizeof(s)
@test length(c) == 1
end
let s = "\\:ko"
c, r = test_bslashcomplete(s)
@test "\\:koala:" in c
end
# test emoji symbol completions after unicode #9209
let s = "α\\:koala:"
c, r = test_bslashcomplete(s)
@test c[1] == "🐨"
@test r == 3:sizeof(s)
@test length(c) == 1
end
# test latex symbol completions in strings should not work when there
# is a backslash in front of `\alpha` because it interferes with path completion on windows
let s = "cd(\"path_to_an_empty_folder_should_not_complete_latex\\\\\\alpha"
c, r, res = test_complete(s)
@test length(c) == 0
end
# test latex symbol completions in strings
let s = "\"C:\\\\ \\alpha"
c, r, res = test_complete(s)
@test c[1] == "α"
@test r == 7:12
@test length(c) == 1
end
# test latex symbol completion in getindex expressions (#24705)
let s = "tuple[\\alpha"
c, r, res = test_complete_foo(s)
@test c[1] == "α"
@test r == 7:12
@test length(c) == 1
end
let s = "\\a"
c, r, res = test_complete(s)
"\\alpha" in c
@test r == 1:2
@test s[r] == "\\a"
end
# `cd("C:\U should not make the repl crash due to escaping see comment #9137
let s = "cd(\"C:\\U"
c, r, res = test_complete(s)
end
# Test method completions
let s = "max("
c, r, res = test_complete(s)
@test !res
@test let found = false
for m in methods(max)
if !found
found = (c[1] == string(m))
end
end
found
end
@test r == 1:3
@test s[r] == "max"
end
# test method completions when `!` operator precedes
let
s = "!("
c, r, res = test_complete(s)
@test !res
@test all(m -> string(m) in c, methods(!))
@test s[r] == s[1:end-1]
s = "!isnothing("
c, r, res = test_complete(s)
@test !res
@test all(m -> string(m) in c, methods(isnothing))
@test s[r] == s[1:end-1]
s = "!!isnothing("
c, r, res = test_complete(s)
@test !res
@test all(m -> string(m) in c, methods(isnothing))
@test s[r] == s[1:end-1]
end
# Test completion of methods with input concrete args and args where typeinference determine their type
let s = "CompletionFoo.test(1, 1, "
c, r, res = test_complete(s)
@test !res
m = test_methods_list(Main.CompletionFoo.test, Tuple{Int, Int, Vararg})
@test c[1] == m[1]
@test c[2] == m[2]
@test length(c) == 2
# In particular, this checks that test(x::Real, y::Real) is not a valid completion
# since it is strictly less specific than test(x::T, y::T) where T
@test r == 1:18
@test s[r] == "CompletionFoo.test"
end
let s = "CompletionFoo.test(CompletionFoo.array,"
c, r, res = test_complete(s)
@test !res
@test c[1] == first(test_methods_list(Main.CompletionFoo.test, Tuple{Array{Int, 1}, Any, Vararg}))
@test length(c) == 2
@test r == 1:18
@test s[r] == "CompletionFoo.test"
end
let s = "CompletionFoo.test(1,1,1,"
c, r, res = test_complete(s)
@test !res
@test c[1] == first(test_methods_list(Main.CompletionFoo.test, Tuple{Any, Any, Any, Vararg}))
@test length(c) == 1
@test r == 1:18
@test s[r] == "CompletionFoo.test"
end
let s = "CompletionFoo.test1(Int,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 0
@test r == 1:19
@test s[r] == "CompletionFoo.test1"
end
let s = "CompletionFoo.test1(Float64,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
@test r == 1:19
@test s[r] == "CompletionFoo.test1"
end
let s = "prevind(\"θ\",1,"
c, r, res = test_complete(s)
@test c[1] == first(test_methods_list(prevind, Tuple{String, Int, Vararg}))
@test r == 1:7
@test s[r] == "prevind"
end
for (T, arg) in [(String,"\")\""),(Char, "')'")]
s = "(1, CompletionFoo.test2($arg,"
c, r, res = test_complete(s)
@test length(c) == 1
@test c[1] == first(test_methods_list(Main.CompletionFoo.test2, Tuple{T, Vararg}))
@test r == 5:23
@test s[r] == "CompletionFoo.test2"
end
let s = "(1, CompletionFoo.test2(`')'`,"
c, r, res = test_complete(s)
@test length(c) == 1
@test c[1] == first(test_methods_list(Main.CompletionFoo.test2, Tuple{Cmd, Vararg}))
end
let s = "CompletionFoo.test3([1, 2] .+ CompletionFoo.varfloat,"
c, r, res = test_complete(s)
@test !res
@test only(c) == first(test_methods_list(Main.CompletionFoo.test3, Tuple{Array{Float64, 1}, Float64, Vararg}))
end
let s = "CompletionFoo.test3([1.,2.], 1.,"
c, r, res = test_complete(s)
@test !res
@test c[1] == first(test_methods_list(Main.CompletionFoo.test3, Tuple{Array{Float64, 1}, Float64, Vararg}))
@test r == 1:19
@test length(c) == 1
@test s[r] == "CompletionFoo.test3"
end
let s = "CompletionFoo.test4(\"e\",r\" \","
c, r, res = test_complete(s)
@test !res
@test c[1] == first(test_methods_list(Main.CompletionFoo.test4, Tuple{String, Regex, Vararg}))
@test r == 1:19
@test length(c) == 1
@test s[r] == "CompletionFoo.test4"
end
# (As discussed in #19829, the Base.REPLCompletions.get_type function isn't
# powerful enough to analyze anonymous functions.)
let s = "CompletionFoo.test5(broadcast((x,y)->x==y, push!(Base.split(\"\",' '),\"\",\"\"), \"\"),"
c, r, res = test_complete(s)
@test !res
@test_broken only(c) == first(test_methods_list(Main.CompletionFoo.test5, Tuple{BitArray{1}, Vararg}))
end
# test partial expression expansion
let s = "CompletionFoo.test5(Bool[x==1 for x=1:4],"
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
@test c[1] == first(test_methods_list(Main.CompletionFoo.test5, Tuple{Array{Bool,1}, Vararg}))
end
let s = "CompletionFoo.test4(CompletionFoo.test_y_array[1]()[1], CompletionFoo.test_y_array[1]()[2], "
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
@test c[1] == first(test_methods_list(Main.CompletionFoo.test4, Tuple{String, String, Vararg}))
end
# Test that string escaping is handled correct
let s = """CompletionFoo.test4("\\"","""
c, r, res = test_complete(s)
@test !res
@test length(c) == 2
end
# Test max method suggestions
let s = "convert("
c, _, res = test_complete_noshift(s)
@test !res
@test only(c) == "convert( too many methods, use SHIFT-TAB to show )"
c2, _, res2 = test_complete(s)
@test !res2
@test any(==(string(first(methods(convert)))), c2)
@test length(c2) > REPL.REPLCompletions.MAX_METHOD_COMPLETIONS
end
let s = "CompletionFoo.test5(AbstractArray[Bool[]][1],"
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
end
let s = "CompletionFoo.test3(@time([1, 2] .+ CompletionFoo.varfloat),"
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
end
# method completions with kwargs
let s = "CompletionFoo.kwtest( "
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
@test occursin("x, y, w...", c[1])
@test (c, r, res) == test_complete("CompletionFoo.kwtest(;")
@test (c, r, res) == test_complete("CompletionFoo.kwtest(; x=1, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest(; kw=1, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest(x=1, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest(x=1; ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest(x=kw=1, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest(; x=kw=1, ")
end
let s = "CompletionFoo.kwtest2(1, x=1,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
@test occursin("a; x, y, w...", c[1])
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(1; x=1, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(1, x=1; ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(1, kw=1, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(1; kw=1, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(1, kw=1; ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(y=3, 1, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(y=3, 1; ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(kw=3, 1, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(kw=3, 1; ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(1; ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest2(1, ")
end
let s = "CompletionFoo.kwtest4(x23=18, x; "
c, r, res = test_complete(s)
@test !res
@test length(c) == 3 # TODO: remove "kwtest4(a::String; _a1b, xαβγ)"
@test any(str->occursin("kwtest4(a::SubString", str), c)
@test any(str->occursin("kwtest4(a::AbstractString", str), c)
@test (c, r, res) == test_complete("CompletionFoo.kwtest4(x23=18, x, ")
@test (c, r, res) == test_complete("CompletionFoo.kwtest4(x23=18, ")
end
# TODO: @test_nocompletion("CompletionFoo.kwtest4(x23=17; ")
# TODO: @test_nocompletion("CompletionFoo.kwtest4.(x23=17; ")
let s = "CompletionFoo.kwtest5(3, somekwarg=6,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
@test occursin("kwtest5(a::$(Int), b, x...; somekwarg, somekotherkwarg)", c[1])
@test (c, r, res) == test_complete("CompletionFoo.kwtest5(3, somekwarg=6, anything, ")
end
# TODO: @test_nocompletion("CompletionFoo.kwtest5(3; somekwarg=6,")
# TODO: @test_nocompletion("CompletionFoo.kwtest5(3;")
# TODO: @test_nocompletion("CompletionFoo.kwtest5(3; somekwarg=6, anything, ")
#################################################################
# method completion with `?` (arbitrary method with given argument types)
let s = "CompletionFoo.?([1,2,3], 2.0)"
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
@test occursin("test(x::AbstractArray{T}, y) where T<:Real", only(c))
# In particular, this checks that test(args...) is not a valid completion
# since it is strictly less specific than test(x::AbstractArray{T}, y)
end
let s = "CompletionFoo.?([1,2,3], 2.0"
c, r, res = test_complete(s)
@test !res
@test any(str->occursin("test(x::AbstractArray{T}, y) where T<:Real", str), c)
@test any(str->occursin("test(args...)", str), c)
@test !any(str->occursin("test3(x::AbstractArray{Int", str), c)
@test !any(str->occursin("test4", str), c)
end
let s = "CompletionFoo.?('c')"
c, r, res = test_complete(s)
@test !res
@test any(str->occursin("test9(x::Char)", str), c)
@test any(str->occursin("test10(a, ", str), c)
@test !any(str->occursin("test9(x::Char, i::Int", str), c)
end
let s = "CompletionFoo.?('c'"
c, r, res = test_complete(s)
@test !res
@test any(str->occursin("test9(x::Char)", str), c)
@test any(str->occursin("test10(a, ", str), c)
@test any(str->occursin("test9(x::Char, i::Int", str), c)
end
let s = "CompletionFoo.?(false, \"a\", 3, "
c, r, res = test_complete(s)
@test !res
@test length(c) == 2
@test any(s->occursin("test(args...)", s), c)
@test any(s->occursin("test11(a::Integer, b, c)", s), c)
end
let s = "CompletionFoo.?(false, \"a\", 3, "
c, r, res = test_complete_noshift(s)
@test !res
@test length(c) == 1
@test occursin("test11(a::Integer, b, c)", only(c))
end
let s = "CompletionFoo.?(\"a\", 3, "
c, r, res = test_complete(s)
@test !res
@test any(str->occursin("test10(a, x::$Int...)", str), c)
@test !any(str->occursin("test10(a, y::Bool...)", str), c)
@test !any(str->occursin("test10(s::String...)", str), c)
end
let s = "CompletionFoo.?()"
c, r, res = test_complete(s)
@test !res
@test any(str->occursin("foo()", str), c)
@test any(str->occursin("kwtest(;", str), c)
@test any(str->occursin("test(args...)", str), c)
end
let s = "CompletionFoo.?()"
c, r, res = test_complete_noshift(s)
@test !res
@test length(c) == 1
@test occursin("test10(s::String...)", only(c))
end
#= TODO: restrict the number of completions when a semicolon is present in ".?(" syntax
let s = "CompletionFoo.?(; y=2, "
c, r, res = test_complete(s)
@test !res
@test length(c) == 4
@test all(x -> occursin("kwtest", x), c)
# We choose to include kwtest2 and kwtest3 although the number of args if wrong.
# This is because the ".?(" syntax with no closing parenthesis does not constrain the
# number of arguments in the methods it suggests.
end
let s = "CompletionFoo.?(3; len2=5, "
c, r, res = test_complete_noshift(s)
@test !res
@test length(c) == 1
@test occursin("kwtest3(a::Integer; namedarg, foobar, slurp...)", only(c))
# the other two kwtest3 methods should not appear because of specificity
end
=#
# For the ".?(" syntax, do not constrain the number of arguments even with a semicolon.
@test test_complete("CompletionFoo.?(; ") ==
test_complete("CompletionFoo.?(")
#TODO: @test test_complete("CompletionFoo.?(Any[]...; ") == test_complete("CompletionFoo.?(Cmd[]..., ") == test_complete("CompletionFoo.?(")
@test test_complete("CompletionFoo.?()") == test_complete("CompletionFoo.?(;)")
#TODO: @test_nocompletion("CompletionFoo.?(3; len2=5; ")
# https://github.com/JuliaLang/julia/issues/52551
@test !isempty(test_complete("?("))
#################################################################
# Test method completion with varargs
let s = "CompletionFoo.test10(z, Integer[]...,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 5
@test all(startswith("test10("), c)
@test allunique(c)
end
let s = "CompletionFoo.test10(3, Integer[]...,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 4
@test all(startswith("test10("), c)
@test allunique(c)
@test !any(str->occursin("test10(s::String...)", str), c)
end
let s = "CompletionFoo.test10(3, 4,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 3
@test any(str->occursin("test10(a, x::$Int...)", str), c)
@test any(str->occursin("test10(a::Integer, b::Integer, c)", str), c)
@test any(str->occursin("test10(a, d::Integer, z::Signed...)", str), c)
end
let s = "CompletionFoo.test10(3, 4, 5,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 3
@test any(str->occursin("test10(a, x::$Int...)", str), c)
@test any(str->occursin("test10(a::Integer, b::Integer, c)", str), c) # show it even though the call would result in an ambiguity error
@test any(str->occursin("test10(a, d::Integer, z::Signed...)", str), c)
# the last one is not eliminated by specificity since the complete call could be
# test10(3, 4, 5, Int8(6)) for instance
end
let s = "CompletionFoo.test10(z, z, 0, "
c, r, res = test_complete(s)
@test !res
@test length(c) == 3
@test any(str->occursin("test10(a, x::$Int...)", str), c)
@test any(str->occursin("test10(a::Integer, b::Integer, c)", str), c) # show it even though the call would result in an ambiguity error
@test any(str->occursin("test10(a, d::Integer, z::Signed...)", str), c)
end
let s = "CompletionFoo.test10(\"a\", Union{Signed,Bool,String}[3][1], "
c, r, res = test_complete(s)
@test !res
@test length(c) == 2
@test all(startswith("test10("), c)
@test allunique(c)
@test !any(str->occursin("test10(a::Integer, b::Integer, c)", str), c)
end
# Test method completion with ambiguity
let s = "CompletionFoo.test11(Integer[false][1], Integer[14][1], "
c, r, res = test_complete(s)
@test !res
@test length(c) == 3
@test all(startswith("test11("), c)
@test allunique(c)
end
let s = "CompletionFoo.test11(Integer[-7][1], Integer[0x6][1], 6,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 2
@test any(str->occursin("test11(a::Integer, b, c)", str), c)
@test any(str->occursin("test11(u, v::Integer, w)", str), c)
@test !any(str->occursin("test11(x::$Int, y::$Int, z)", str), c)
end
let s = "CompletionFoo.test11(3, 4,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 2
@test any(str->occursin("test11(x::$Int, y::$Int, z)", str), c)
@test any(str->occursin("test11(::Any, ::Any, s::String)", str), c)
end
let s = "CompletionFoo.test11(0x8, 5,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 3
@test any(str->occursin("test11(a::Integer, b, c)", str), c)
@test any(str->occursin("test11(u, v::Integer, w)", str), c)
@test any(str->occursin("test11(::Any, ::Any, s::String)", str), c)
end
let s = "CompletionFoo.test11(0x8, 'c',"
c, r, res = test_complete(s)
@test !res
@test length(c) == 2
@test any(str->occursin("test11(a::Integer, b, c)", str), c)
@test any(str->occursin("test11(::Any, ::Any, s::String)", str), c)
end
let s = "CompletionFoo.test11('d', 3,"
c, r, res = test_complete(s)
@test !res
@test length(c) == 2
@test any(str->occursin("test11(u, v::Integer, w)", str), c)
@test any(str->occursin("test11(::Any, ::Any, s::String)", str), c)
end
let s = "CompletionFoo.test!12("
c, r, res = test_complete(s)
@test !res
@test occursin("test!12()", only(c))
end
#= TODO: Test method completion depending on the number of arguments with splatting
@test_nocompletion("CompletionFoo.test3(unknown; ")
@test_nocompletion("CompletionFoo.test3.(unknown; ")
let s = "CompletionFoo.test2(unknown..., somethingelse..., xyz...; " # splat may be empty
c, r, res = test_complete(s)
@test !res
@test length(c) == 3
@test all(str->occursin("test2(", str), c)
@test (c, r, res) == test_complete("CompletionFoo.test2(unknown..., somethingelse..., xyz, ")
@test (c, r, res) == test_complete("CompletionFoo.test2(unknown..., somethingelse..., xyz; ")
end
let s = "CompletionFoo.test('a', args..., 'b';"
c, r, res = test_complete(s)
@test !res
@test length(c) == 1
@test occursin("test(args...)", c[1])
@test (c, r, res) == test_complete("CompletionFoo.test(a, args..., b, c;")
end
let s = "CompletionFoo.test(3, 5, args...,;"
c, r, res = test_complete(s)
@test !res
@test length(c) == 2
@test any(str->occursin("test(x::T, y::T) where T<:Real", str), c)
@test any(str->occursin("test(args...)", str), c)
end
=#
# Test that method calls with ill-formed kwarg syntax are not completed
@test_nocompletion("CompletionFoo.kwtest(; x=2, y=4; kw=3, ")
@test_nocompletion("CompletionFoo.kwtest(x=2; y=4; ")
@test_nocompletion("CompletionFoo.kwtest((x=y)=4, ")
@test_nocompletion("CompletionFoo.kwtest(; (x=y)=4, ")
@test_nocompletion("CompletionFoo.kwtest(; w...=16, ")
@test_nocompletion("CompletionFoo.kwtest(; 2, ")
@test_nocompletion("CompletionFoo.kwtest(; 2=3, ")
@test_nocompletion("CompletionFoo.kwtest3(im; (true ? length : length), ")
@test_nocompletion("CompletionFoo.kwtest.(x=2; y=4; ")
@test_nocompletion("CompletionFoo.kwtest.(; w...=16, ")
# Test of inference based getfield completion
let s = "(1+2im)."
c,r = test_complete(s)
@test length(c) == 2
@test r == (lastindex(s) + 1):lastindex(s)
@test c == ["im", "re"]
end
let s = "((1+2im))."
c, r = test_complete(s)
@test length(c) == 2
@test r == (lastindex(s) + 1):lastindex(s)
@test c == ["im", "re"]
end
let s = "CompletionFoo.test_y_array[1]."
c, r = test_complete(s)
@test length(c) == 1
@test r == (lastindex(s) + 1):lastindex(s)
@test c[1] == "yy"
end
let s = "CompletionFoo.Test_y(rand()).y"
c, r = test_complete(s)
@test length(c) == 1
@test r == lastindex(s):lastindex(s)
@test c[1] == "yy"
end
let s = "CompletionFoo.test6()[1](CompletionFoo.Test_y(rand())).y"
c, r = test_complete(s)
@test length(c) == 1
@test r == lastindex(s):lastindex(s)
@test c[1] == "yy"
end
let s = "CompletionFoo.named."
c, r = test_complete(s)
@test length(c) == 1
@test r == (lastindex(s) + 1):lastindex(s)
@test c[1] == "len2"
end
# Test completion in multi-line comments
let s = "#=\n\\alpha"
c, r, res = test_complete(s)
@test c[1] == "α"
@test r == 4:9
@test length(c) == 1
end
# Test that completion do not work in multi-line comments
let s = "#=\nmax"
c, r, res = test_complete(s)
@test length(c) == 0
end
# Test completion of packages
path = joinpath(tempdir(),randstring())
pushfirst!(LOAD_PATH, path)
try
mkpath(path)
write(joinpath(path, "Project.toml"),
"""
name = "MyProj"
[deps]
MyPack = "09ebe64f-f76c-4f21-bef2-bd9be6c77e76"
""")
c, r, res = test_complete("using MyP")
@test "MyPack" in c