forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompletionTest.scala
1726 lines (1570 loc) · 53.9 KB
/
CompletionTest.scala
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
package dotty.tools.languageserver
import org.junit.Assert.{assertEquals, assertTrue, assertFalse}
import org.junit.Test
import org.eclipse.lsp4j.CompletionItemKind._
import dotty.tools.languageserver.util.Code._
import dotty.tools.languageserver.util.CodeTester
import dotty.tools.languageserver.util.actions.CodeCompletion
class CompletionTest {
@Test def completion0: Unit = {
code"class Foo { val xyz: Int = 0; def y: Int = xy${m1} }"
.completion(("xyz", Field, "Int"))
}
@Test def completionFromScalaPredef: Unit = {
code"class Foo { def foo: Unit = prin${m1} }"
.completion(
("print", Method, "(x: Any): Unit"),
("printf", Method, "(text: String, xs: Any*): Unit"),
("println", Method, "(x: Any): Unit"),
("println", Method, "(): Unit"),
)
}
@Test def completionFromNewScalaPredef: Unit = {
code"class Foo { val foo = summ${m1} }"
.completion(("summon", Method, "[T](using x: T): x.type"))
}
@Test def completionFromScalaPackage: Unit = {
code"class Foo { val foo: Conv${m1} }"
.completion(("Conversion", Class, "Conversion"))
}
@Test def implicitSearchCrash: Unit =
code"""
|object Test:
| trait Foo:
| def test(): String
| given Int = ???
| given (using ev: Int): Conversion[String, Foo] = ???
|
| val test = {
| "".tes$m1
| 1
| }"""
.completion(("test", Method, "(): String"))
@Test def completionFromScalaPackageObject: Unit = {
code"class Foo { val foo: BigD${m1} }"
.completion(
("BigDecimal", Field, "BigDecimal"),
("BigDecimal", Method, "=> scala.math.BigDecimal.type"),
)
}
@Test def completionFromSyntheticPackageObject: Unit = {
code"class Foo { val foo: IArr${m1} }"
.completion(
("IArray", Module, "IArray"),
("IArray", Field, "IArray"),
)
}
@Test def completionFromJavaDefaults: Unit = {
code"class Foo { val foo: Runn${m1} }"
.completion(
("Runnable", Class, "Runnable"),
("Runnable", Module, "Runnable"),
)
}
@Test def completionWithImplicitConversion: Unit = {
withSources(
code"object Foo { implicit class WithBaz(bar: Bar) { def baz = 0 } }",
code"class Bar",
code"object Main { import Foo._; val bar: Bar = new Bar; bar.b${m1} }"
).completion(("baz", Method, "=> Int"))
}
// TODO: Also add tests with concrete classes, where the completion will
// include the constructor proxy companion
@Test def importCompleteClassWithPrefix: Unit = {
withSources(
code"""object Foo { abstract class MyClass }""",
code"""import Foo.My${m1}"""
).completion(("MyClass", Class, "Foo.MyClass"))
}
@Test def importCompleteClassNoPrefix: Unit = {
withSources(
code"""object Foo { abstract class MyClass }""",
code"""import Foo.${m1}"""
).completion(completionItems => {
val results = CodeCompletion.simplifyResults(completionItems)
val myClass = ("MyClass", Class, "Foo.MyClass")
assertTrue(results.contains(("MyClass", Class, "Foo.MyClass")))
// Verify that apart from `MyClass`, we only have the methods that exists on `Foo`
assertTrue((results - myClass).forall { case (_, kind, _) => kind == Method })
// Verify that we don't have things coming from an implicit conversion, such as ensuring
assertFalse(results.exists { case (name, _, _) => name == "ensuring" })
})
}
@Test def importCompleteFromPackage: Unit = {
withSources(
code"""|package a
|abstract class MyClass""",
code"""|package b
|import a.My${m1}"""
).completion(("MyClass", Class, "a.MyClass"))
}
@Test def importCompleteFromClass: Unit = {
withSources(
code"""abstract class Foo { val x: Int = 0 }""",
code"""import Foo.${m1}"""
).noCompletions()
}
@Test def importCompleteIncludesSynthetic: Unit = {
code"""|case class MyCaseClass(foobar: Int)
|object O {
| val x = MyCaseClass(0)
| import x.c${m1}
|}"""
.completion(
("copy", Method, "(foobar: Int): MyCaseClass"),
("canEqual", Method, "(that: Any): Boolean"),
)
}
@Test def importCompleteIncludeModule: Unit = {
withSources(
code"""object O { object MyObject }""",
code"""import O.My${m1}"""
).completion(("MyObject", Module, "O.MyObject"))
}
@Test def importCompleteWithClassAndCompanion: Unit = {
withSources(
code"""|package pkg0
|class Foo
|object Foo""",
code"""|package pgk1
|import pkg0.F${m1}"""
).completion(
("Foo", Class, "pkg0.Foo"),
("Foo", Module, "pkg0.Foo"),
)
}
@Test def importCompleteIncludePackage: Unit = {
withSources(
code"""|package foo.bar
|abstract classFizz""",
code"""import foo.b${m1}"""
).completion(("bar", Module, "foo.bar"))
}
@Test def importCompleteIncludeMembers: Unit = {
withSources(
code"""|object MyObject {
| val myVal = 0
| def myDef = 0
| var myVar = 0
| object myObject
| abstract class myClass
| trait myTrait
|}""",
code"""import MyObject.my${m1}"""
).completion(
("myVal", Field, "Int"),
("myDef", Method, "=> Int"),
("myVar", Variable, "Int"),
("myObject", Module, "MyObject.myObject"),
("myClass", Class, "MyObject.myClass"),
("myTrait", Class, "MyObject.myTrait"),
)
}
@Test def importJavaClass: Unit = {
code"""import java.io.FileDesc${m1}"""
.completion(
("FileDescriptor", Class, "java.io.FileDescriptor"),
("FileDescriptor", Module, "java.io.FileDescriptor"),
)
}
@Test def importJavaStaticMethod: Unit = {
code"""import java.lang.System.lineSep${m1}"""
.completion(("lineSeparator", Method, "(): String"))
}
@Test def importJavaStaticField: Unit = {
code"""import java.lang.System.ou${m1}"""
.completion(("out", Field, "java.io.PrintStream"))
}
@Test def importFromExplicitAndSyntheticPackageObject: Unit = {
withSources(
code"package foo.bar; trait XXXX1",
code"package foo; package object bar { trait XXXX2 }",
code"object Main { import foo.bar.XX${m1} }"
).completion(
("XXXX1", Class, "foo.bar.XXXX1"),
("XXXX2", Class, "foo.bar.XXXX2"),
)
}
@Test def completeJavaModuleClass: Unit = {
code"""|object O {
| val out = java.io.FileDesc${m1}
|}"""
.completion(("FileDescriptor", Module, "java.io.FileDescriptor"))
}
@Test def importRename: Unit = {
code"""import java.io.{FileDesc${m1} => Foo}"""
.completion(
("FileDescriptor", Class, "java.io.FileDescriptor"),
("FileDescriptor", Module, "java.io.FileDescriptor"),
)
}
@Test def noImportRename: Unit = {
code"""import java.io.{FileDescriptor => Fo$m1}"""
.noCompletions()
}
@Test def importGivenByType: Unit = {
code"""|trait Foo
|object Bar
|import Bar.{given Fo$m1}"""
.completion(("Foo", Class, "Foo"))
}
@Test def markDeprecatedSymbols: Unit = {
code"""|object Foo {
| @deprecated
| val bar = 0
|}
|import Foo.ba${m1}"""
.completion(results => {
assertEquals(1, results.size)
val result = results.head
assertEquals("bar", result.getLabel)
assertTrue("bar was not deprecated", result.getDeprecated)
})
}
@Test def i4397: Unit = {
code"""class Foo {
| .${m1}
|}"""
.noCompletions()
}
@Test def completeNoPrefix: Unit = {
code"""class Foo { def foo = 0 }
|object Bar {
| val foo = new Foo
| foo.${m1}
|}"""
.completion(results => assertTrue(results.nonEmpty))
}
@Test def completeErrorKnowsKind: Unit = {
code"""object Bar {
| abstract class Zig
| val Zag: Int = 0
| val b = 3 + Bar.${m1}
|}"""
.completion(completionItems => {
val results = CodeCompletion.simplifyResults(completionItems)
assertTrue(results.contains(("Zag", Field, "Int")))
assertFalse(results.exists((label, _, _) => label == "Zig"))
})
}
@Test def typeCompletionShowsTerm: Unit = {
code"""class Bar
|object Foo {
| val bar = new Bar
| def baz = new Bar
| object bat
| val bizz: ba${m1}
|}"""
.completion(
("bar", Field, "Bar"),
("bat", Module, "Foo.bat"),
)
}
@Test def completionOnRenamedImport: Unit = {
code"""import java.io.{FileDescriptor => AwesomeStuff}
trait Foo { val x: Awesom${m1}}"""
.completion(
("AwesomeStuff", Class, "java.io.FileDescriptor"),
("AwesomeStuff", Module, "java.io.FileDescriptor"),
)
}
@Test def completionOnRenamedImport2: Unit = {
code"""|import java.util.{HashMap => MyImportedSymbol}
|trait Foo {
| import java.io.{FileDescriptor => MyImportedSymbol}
| val x: MyImp${m1}
|}"""
.completion(
("MyImportedSymbol", Class, "java.io.FileDescriptor"),
("MyImportedSymbol", Module, "java.io.FileDescriptor"),
)
}
@Test def completionRenamedAndOriginalNames: Unit = {
code"""import java.util.HashMap
|trait Foo {
| import java.util.{HashMap => HashMap2}
| val x: Hash${m1}
|}"""
.completion(
("HashMap", Class, "java.util.HashMap"),
("HashMap", Module, "java.util.HashMap"),
("HashMap2", Class, "java.util.HashMap"),
("HashMap2", Module, "java.util.HashMap"),
)
}
@Test def completionRenamedThrice: Unit = {
code"""import java.util.{HashMap => MyHashMap}
|import java.util.{HashMap => MyHashMap2}
|trait Foo {
| import java.util.{HashMap => MyHashMap3}
| val x: MyHash${m1}
|}"""
.completion(
("MyHashMap", Class, "java.util.HashMap"),
("MyHashMap", Module, "java.util.HashMap"),
("MyHashMap2", Class, "java.util.HashMap"),
("MyHashMap2", Module, "java.util.HashMap"),
("MyHashMap3", Class, "java.util.HashMap"),
("MyHashMap3", Module, "java.util.HashMap"),
)
}
@Test def completeFromWildcardImports: Unit = {
code"""object Foo {
| val fooFloat: Float = 1.0
| val fooLong: Long = 0L
| given fooInt: Int = 0
| given fooString: String = ""
|}
|object Test1 { import Foo.{fooFloat => _, _}; foo${m1} }
|object Test2 { import Foo.given; foo${m2} }
|object Test3 { import Foo.{given String}; foo${m3} }
|object Test4 { import Foo.{_, given String}; foo${m4} }
|object Test5 { import Foo.{fooFloat, given}; foo${m5} }
|object Test6 { import Foo.{fooInt => _, fooString => fooStr, given}; foo${m6} }
|object Test7 { import Foo.{fooLong => fooInt, given Int}; foo${m7} }
"""
.completion(m1, ("fooLong", Field, "Long"))
.completion(m2, ("fooInt", Field, "Int"), ("fooString", Field, "String"))
.completion(m3, ("fooString", Field, "String"))
.completion(m4, ("fooLong", Field, "Long"), ("fooFloat", Field, "Float"), ("fooString", Field, "String"))
.completion(m5, ("fooFloat", Field, "Float"), ("fooInt", Field, "Int"), ("fooString", Field, "String"))
.completion(m6, ("fooStr", Field, "String"))
.completion(m7, ("fooInt", Field, "Long"))
}
@Test def dontCompleteFromAmbiguousImportsFromSameSite: Unit = {
code"""object Foo {
| val i = 0
| val j = 1
|}
|object Test {
| import Foo.{i => xxxx, j => xxxx}
| val x = xx${m1}
|}"""
.noCompletions()
}
@Test def collectNamesImportedInNestedScopes: Unit = {
code"""object Foo {
| val xxxx1 = 1
|}
|object Bar {
| val xxxx2 = 2
|}
|object Baz {
| val xxxx3 = 3
|}
|object Test {
| import Foo.xxxx1
| locally {
| import Bar.xxxx2
| locally {
| import Baz.xxxx3
| val x = xx${m1}
| }
| }
|}"""
.completion(
("xxxx1", Field, "Int"),
("xxxx2", Field, "Int"),
("xxxx3", Field, "Int"),
)
}
@Test def completeEnclosingObject: Unit = {
code"""object Test {
| def x = Tes${m1}
|}"""
.completion(("Test", Module, "Test"))
}
@Test def completeBothDefinitionsForEqualNestingLevels: Unit = {
code"""trait Foo {
| def xxxx(i: Int): Int = i
|}
|trait Bar {
| def xxxx(s: String): String = s
|}
|object Test extends Foo, Bar {
| val x = xx${m1}
|}"""
.completion(
("xxxx", Method, "(s: String): String"),
("xxxx", Method, "(i: Int): Int"),
)
}
@Test def dontCompleteFromAmbiguousImportsForEqualNestingLevels: Unit = {
code"""object Foo {
| def xxxx(i: Int): Int = i
|}
|object Bar {
| def xxxx(s: String): String = s
|}
|object Test {
| import Foo.xxxx
| import Bar.xxxx
| val x = xx${m1}
|}"""
.noCompletions()
}
@Test def completeFromSameImportsForEqualNestingLevels: Unit = {
code"""object Foo {
| def xxxx(i: Int): Int = i
|}
|object Test {
| import Foo.xxxx
| import Foo.xxxx
| import Foo.xxxx
| val x = xx${m1}
|}"""
.completion(("xxxx", Method, "(i: Int): Int"))
}
@Test def preferLocalDefinitionToImportForEqualNestingLevels: Unit = {
code"""object Foo {
| val xxxx = 1
|}
|object Test {
| def xxxx(s: String): String = s
| import Foo.xxxx
| val x = xx${m1}
|}"""
.completion(("xxxx", Method, "(s: String): String"))
}
@Test def preferMoreDeeplyNestedDefinition: Unit = {
code"""object Test {
| def xxxx(i: Int): Int = i
| object Inner {
| def xxxx(s: String): String = s
| val x = xx${m1}
| }
|}"""
.completion(("xxxx", Method, "(s: String): String"))
}
@Test def preferMoreDeeplyNestedImport: Unit = {
code"""object Foo {
| def xxxx(i: Int): Int = i
|}
|object Bar {
| def xxxx(s: String): String = s
|}
|object Test {
| import Foo.xxxx
| locally {
| import Bar.xxxx
| val x: String = xx${m1}
| }
|}"""
.completion(("xxxx", Method, "(s: String): String"))
}
@Test def preferMoreDeeplyNestedLocalDefinitionToImport: Unit = {
code"""object Foo {
| def xxxx(i: Int): Int = i
|}
|object Test {
| import Foo.xxxx
| object Inner {
| def xxxx(s: String): String = s
| val x: String = xx${m1}
| }
|}"""
.completion(("xxxx", Method, "(s: String): String"))
}
@Test def dontCompleteLocalDefinitionShadowedByImport: Unit = {
code"""object XXXX {
| val xxxx = 1
|}
|object Test {
| locally {
| val xxxx = ""
| locally {
| import XXXX.xxxx // import conflicts with val from outer scope
| val y = xx${m1}
| }
| }
|}"""
.noCompletions()
}
@Test def completeFromLocalDefinitionIgnoringLessDeeplyNestedAmbiguities: Unit = {
code"""object XXXX {
| val xxxx = 1
|}
|object Test {
| locally {
| val xxxx = ""
| locally {
| import XXXX.xxxx // import conflicts with val from outer scope
| locally {
| val xxxx = 'a' // shadows both the import and the val from outer scope
| val y = xx${m1}
| }
| }
| }
|}"""
.completion(("xxxx", Field, "Char"))
}
@Test def completionClassAndMethod: Unit = {
code"""object Foo {
| class bar
| def bar(i: Int) = 0
|}
|import Foo.b${m1}"""
.completion(
("bar", Class, "Foo.bar"),
("bar", Method, "(i: Int): Int"),
)
}
@Test def completionTypeAndLazyValue: Unit = {
code"""object Foo {
| type bar = Int
| lazy val bar = 3
|}
|import Foo.b${m1}"""
.completion(
("bar", Field, "Foo.bar"),
("bar", Field, "Int"),
)
}
@Test def keepTrackOfTermsAndTypesSeparately: Unit = {
code"""object XXXX {
| object YYYY
| type YYYY = YYYY.type
|}
|object Test {
| import XXXX._
| val YYYY = Int
| val ZZZZ = YY${m1}
| type ZZZZ = YY${m2}
|}"""
.completion(m1, ("YYYY", Field, "Int"))
.completion(m2, ("YYYY", Field, "XXXX.YYYY"), ("YYYY", Field, "Int"))
}
@Test def completeRespectingAccessModifiers: Unit = {
code"""trait Foo {
| def xxxx1 = ""
| protected def xxxx2 = ""
| private def xxxx3 = ""
|}
|object Test1 extends Foo {
| xx${m1}
|}
|object Test2 {
| val foo = new Foo {}
| foo.xx${m2}
|}"""
.completion(m1, ("xxxx1", Method, "=> String"), ("xxxx2", Method, "=> String"))
.completion(m2, ("xxxx1", Method, "=> String"))
}
@Test def completeFromPackageObjectWithInheritance: Unit = {
code"""package test
|trait Foo[A] { def xxxx(a: A) = a }
|package object foo extends Foo[Int] {}
|object Test {
| foo.xx${m1}
|}"""
.completion(("xxxx", Method, "(a: Int): Int"))
}
@Test def completePrimaryConstructorParameter: Unit = {
val expected = Set(("abc", Field, "Int"))
code"""class Foo(abc: Int) {
| ab${m1}
| def method1: Int = {
| ab${m2}
| 42
| }
| def method2: Int = {
| val smth = ab${m3}
| 42
| }
|}"""
.completion(m1, expected)
.completion(m2, expected)
.completion(m3, expected)
}
@Test def completeExtensionReceiver: Unit = {
code"""extension (string: String) def xxxx = str${m1}"""
.completion(("string", Field, "String"))
}
@Test def completeExtensionMethodWithoutParameter: Unit = {
code"""object Foo
|extension (foo: Foo.type) def xxxx = 1
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def completeExtensionMethodWithParameter: Unit = {
code"""object Foo
|extension (foo: Foo.type) def xxxx(i: Int) = i
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "(i: Int): Int"))
}
@Test def completeExtensionMethodWithTypeParameter: Unit = {
code"""object Foo
|extension (foo: Foo.type) def xxxx[A]: Int = 1
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "[A]: Int"))
}
@Test def completeExtensionMethodWithParameterAndTypeParameter: Unit = {
code"""object Foo
|extension (foo: Foo.type) def xxxx[A](a: A) = a
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "[A](a: A): A"))
}
@Test def completeExtensionMethodFromExtensionWithTypeParameter: Unit = {
code"""extension [A](a: A) def xxxx: A = a
|object Main { "abc".xx${m1} }"""
.completion(("xxxx", Method, "=> String"))
}
@Test def completeExtensionMethodWithResultTypeDependantOnReceiver: Unit = {
code"""trait Foo { type Out; def get: Out}
|object Bar extends Foo { type Out = String; def get: Out = "abc"}
|extension (foo: Foo) def xxxx: foo.Out = foo.get
|object Main { Bar.xx${m1} }"""
.completion(("xxxx", Method, "=> String"))
}
@Test def completeExtensionMethodFromExtenionWithPrefixUsingSection: Unit = {
code"""object Foo
|trait Bar
|trait Baz
|given Bar with {}
|given Baz with {}
|extension (using Bar, Baz)(foo: Foo.type) def xxxx = 1
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def completeExtensionMethodFromExtenionWithMultiplePrefixUsingSections: Unit = {
code"""object Foo
|trait Bar
|trait Baz
|given Bar with {}
|given Baz with {}
|extension (using Bar)(using Baz)(foo: Foo.type) def xxxx = 1
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def dontCompleteExtensionMethodFromExtenionWithMissingImplicitFromPrefixUsingSection: Unit = {
code"""object Foo
|trait Bar
|trait Baz
|given Baz with {}
|extension (using Bar, Baz)(foo: Foo.type) def xxxx = 1
|object Main { Foo.xx${m1} }"""
.noCompletions()
}
@Test def completeExtensionMethodForReceiverOfTypeDependentOnLeadingImplicits: Unit = {
code"""
|trait Foo:
| type Out <: Bar
|
|given Foo with
| type Out = Baz
|
|trait Bar:
| type Out
|
|trait Baz extends Bar
|
|given Baz with
| type Out = Quux
|
|class Quux
|
|object Quux:
| extension (using foo: Foo)(using fooOut: foo.Out)(fooOutOut: fooOut.Out) def xxxx = "abc"
|
|object Main { (new Quux).xx${m1} }"""
.completion(("xxxx", Method, "=> String"))
}
@Test def completeExtensionMethodWithResultTypeDependentOnLeadingImplicit: Unit = {
code"""object Foo
|trait Bar { type Out; def get: Out }
|given Bar with { type Out = 123; def get: Out = 123 }
|extension (using bar: Bar)(foo: Foo.type) def xxxx: bar.Out = bar.get
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "=> (123 : Int)"))
}
@Test def completeExtensionMethodFromExtenionWithPostfixUsingSection: Unit = {
code"""object Foo
|trait Bar
|trait Baz
|given Bar with {}
|given Baz with {}
|extension (foo: Foo.type)(using Bar, Baz) def xxxx = 1
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "(using x$2: Bar, x$3: Baz): Int"))
}
@Test def completeExtensionMethodFromExtenionWithMultiplePostfixUsingSections: Unit = {
code"""object Foo
|trait Bar
|trait Baz
|given Bar with {}
|given Baz with {}
|extension (foo: Foo.type)(using Bar)(using Baz) def xxxx = 1
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "(using x$2: Bar)(using x$3: Baz): Int"))
}
@Test def completeExtensionMethodWithTypeParameterFromExtenionWithTypeParametersAndPrefixAndPostfixUsingSections: Unit = {
code"""trait Bar
|trait Baz
|given Bar with {}
|given Baz with {}
|extension [A](using bar: Bar)(a: A)(using baz: Baz) def xxxx[B]: Either[A, B] = Left(a)
|object Main { 123.xx${m1} }"""
.completion(("xxxx", Method, "(using baz: Baz)[B]: Either[Int, B]"))
}
@Test def completeExtensionMethodWithTypeBounds: Unit = {
code"""trait Foo
|trait Bar extends Foo
|given Bar with {}
|extension [A >: Bar](a: A) def xxxx[B <: a.type]: Either[A, B] = Left(a)
|val foo = new Foo {}
|object Main { foo.xx${m1} }"""
.completion(("xxxx", Method, "[B <: (foo : Foo)]: Either[Foo, B]"))
}
@Test def completeInheritedExtensionMethod: Unit = {
code"""object Foo
|trait FooOps {
| extension (foo: Foo.type) def xxxx = 1
|}
|object Main extends FooOps { Foo.xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def completeExtensionMethodWithoutLosingTypeParametersFromGivenInstance: Unit = {
code"""trait ListOps[A] {
| extension (xs: List[A]) def xxxx = xs
|}
|given ListOps[Int] with {}
|object Main { List(1, 2, 3).xx${m1} }"""
.completion(("xxxx", Method, "=> List[Int]"))
}
@Test def completeRenamedExtensionMethod: Unit = {
code"""object Foo
|object FooOps {
| extension (foo: Foo.type) def xxxx = 1
|}
|import FooOps.{xxxx => yyyy}
|object Main { Foo.yy${m1} }"""
.completion(("yyyy", Method, "=> Int"))
}
@Test def completeExtensionMethodFromGivenInstanceDefinedInScope: Unit = {
code"""object Foo
|trait FooOps
|given FooOps with {
| extension (foo: Foo.type) def xxxx = 1
|}
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def completeExtensionMethodFromImportedGivenInstance: Unit = {
code"""object Foo
|trait FooOps
|object Bar {
| given FooOps with {
| extension (foo: Foo.type) def xxxx = 1
| }
|}
|import Bar.given
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def completeExtensionMethodFromImplicitScope: Unit = {
code"""case class Foo(i: Int)
|object Foo {
| extension (foo: Foo) def xxxx = foo.i
|}
|object Main { Foo(123).xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def completeExtensionMethodFromGivenInImplicitScope: Unit = {
code"""trait Bar
|case class Foo(i: Int)
|object Foo {
| given Bar with {
| extension (foo: Foo) def xxxx = foo.i
| }
|}
|object Main { Foo(123).xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def completeExtensionMethodOnResultOfImplicitConversion: Unit = {
code"""import scala.language.implicitConversions
|case class Foo(i: Int)
|extension (foo: Foo) def xxxx = foo.i
|given Conversion[Int, Foo] = Foo(_)
|object Main { 123.xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def dontCompleteExtensionMethodWithMismatchedName: Unit = {
code"""object Foo
|extension (foo: Foo.type) def xxxx = 1
|object Main { Foo.yy${m1} }"""
.noCompletions()
}
@Test def preferNormalMethodToExtensionMethod: Unit = {
code"""object Foo {
| def xxxx = "abcd"
|}
|object FooOps {
| extension (foo: Foo.type) def xxxx = 1
|}
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "=> String"))
}
@Test def preferExtensionMethodFromExplicitScope: Unit = {
code"""object Foo
|extension (foo: Foo.type) def xxxx = 1
|object FooOps {
| extension (foo: Foo.type) def xxxx = "abcd"
|}
|object Main { Foo.xx${m1} }"""
.completion(("xxxx", Method, "=> Int"))
}
@Test def dontCompleteExtensionMethodWithMismatchedReceiverType: Unit = {
code"""extension (i: Int) def xxxx = i
|object Main { "abc".xx${m1} }"""
.noCompletions()
}
@Test def i13365: Unit = {
code"""import scala.quoted._
|
|object Test {
| def test(using Quotes)(str: String) = {
| import quotes.reflect._
| val msg = Expr(str)
| val printHello = '{ print("sdsd") }
| val tree = printHello.asTerm
| tree.sh${m1}
| }
|}"""
.completion(("show",Method, "(using x$2: x$1.reflect.Printer[x$1.reflect.Tree]): String"))
}
@Test def syntheticThis: Unit = {
code"""class Y() {
| def bar: Unit =
| val argument: Int = ???
| arg${m1}
|
| def arg: String = ???
|}
|"""
.completion(
("arg", Method, "=> String"),
("argument", Field, "Int")
)
}
@Test def concatMethodWithImplicits: Unit = {
code"""object A {
| Array.concat${m1}
|}"""
.completion(("concat", Method, "[T](xss: Array[T]*)(implicit evidence$11: scala.reflect.ClassTag[T]): Array[T]"))
}
@Test def i12465_hkt: Unit =
code"""???.asInstanceOf[scala.collection.Seq].${m1}"""
.noCompletions()
@Test def i12465_hkt_alias: Unit =
code"""???.asInstanceOf[Seq].${m1}"""
.noCompletions()
@Test def i13624_annotType: Unit =
val expected1 = Set(("MyAnnotation", Class, "MyAnnotation"))
val expected2 = Set(("MyAnnotation", Class, "Foo.MyAnnotation"))
code"""object Foo{
| class MyAnnotation extends annotation.StaticAnnotation
|}
|class MyAnnotation extends annotation.StaticAnnotation
|class Annotation2(a: String) extends annotation.StaticAnnotation
|val x = 1: @MyAnnot${m1}
|type X = Int @MyAnnot${m2}
|val y = 1: @Foo.MyAnnot${m3}
|val z = 1: @Foo.MyAnnotation @MyAnno${m4}
|type Y = Int @MyAnnotation @Foo.MyAnnota${m5}
|val w = 1: @Annotation2("abc": @Foo.MyAnnot${m6})
|"""
.completion(m1, expected1)
.completion(m2, expected1)
.completion(m3, expected2)
.completion(m4, expected1)
.completion(m5, expected2)
.completion(m6, expected2)
@Test def i13624_annotation : Unit =
code"""@annotation.implicitNot${m1}
|@annotation.implicitNotFound @mai${m2}"""
.completion(m1, ("implicitNotFound", Class, "scala.annotation.implicitNotFound"))
.completion(m2, ("main", Class, "main"))
@Test def i13623_annotation : Unit =
code"""import annot${m1}"""
.completion(("annotation", Module, "scala.annotation"))
@Test def importAnnotationAfterImport : Unit =
code"""import java.lang.annotation; import annot${m1}"""
.completion(("annotation", Module, "java.lang.annotation"))
@Test def completeTemplateConstrArgType: Unit = {
code"""import scala.concurrent.Future
|class Foo(x: Fut${m1})"""
.completion(
("Future", Class, "scala.concurrent.Future"),
("Future", Module, "scala.concurrent.Future")
)
}