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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import LSPTestSupport
import LanguageServerProtocol
import SKTestSupport
import SourceKitLSP
import XCTest
final class SwiftCompletionTests: XCTestCase {
/// Base document text to use for completion tests.
private let text: String = """
struct S {
/// Documentation for `abc`.
var abc: Int
func test(a: Int) {
self.abc
}
func test(_ b: Int) {
self.abc
}
}
"""
// MARK: - Helpers
private var snippetCapabilities = ClientCapabilities(
textDocument: TextDocumentClientCapabilities(
completion: TextDocumentClientCapabilities.Completion(
completionItem: TextDocumentClientCapabilities.Completion.CompletionItem(snippetSupport: true)
)
)
)
// MARK: - Tests
func testCompletionServerFilter() async throws {
try await testCompletionBasic()
}
func testCompletionDefaultFilter() async throws {
try await testCompletionBasic()
}
func testCompletionBasic() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(text, uri: uri)
let selfDot = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 5, utf16index: 9)
)
)
XCTAssertTrue(selfDot.isIncomplete)
XCTAssertGreaterThanOrEqual(selfDot.items.count, 2)
let abc = selfDot.items.first { $0.label == "abc" }
XCTAssertNotNil(abc)
if let abc = abc {
XCTAssertEqual(abc.kind, .property)
XCTAssertEqual(abc.detail, "Int")
XCTAssertEqual(abc.documentation, .markupContent(MarkupContent(kind: .markdown, value: "Documentation for abc.")))
XCTAssertEqual(abc.filterText, "abc")
XCTAssertEqual(
abc.textEdit,
.textEdit(TextEdit(range: Position(line: 5, utf16index: 9)..<Position(line: 5, utf16index: 9), newText: "abc"))
)
XCTAssertEqual(abc.insertText, "abc")
XCTAssertEqual(abc.insertTextFormat, .plain)
}
for col in 10...12 {
let inIdent = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 5, utf16index: col)
)
)
guard let abc = inIdent.items.first(where: { $0.label == "abc" }) else {
XCTFail("No completion item with label 'abc'")
return
}
// If we switch to server-side filtering this will change.
XCTAssertEqual(abc.kind, .property)
XCTAssertEqual(abc.detail, "Int")
XCTAssertEqual(abc.documentation, .markupContent(MarkupContent(kind: .markdown, value: "Documentation for abc.")))
XCTAssertEqual(abc.filterText, "abc")
XCTAssertEqual(
abc.textEdit,
.textEdit(
TextEdit(range: Position(line: 5, utf16index: 9)..<Position(line: 5, utf16index: col), newText: "abc")
)
)
XCTAssertEqual(abc.insertText, "abc")
XCTAssertEqual(abc.insertTextFormat, .plain)
}
let after = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 6, utf16index: 0)
)
)
XCTAssertNotEqual(after, selfDot)
}
func testCompletionSnippetSupport() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
let uri = DocumentURI(for: .swift)
testClient.openDocument(text, uri: uri)
func getTestMethodCompletion(_ position: Position, label: String) async throws -> CompletionItem? {
let selfDot = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: position
)
)
return selfDot.items.first { $0.label == label }
}
var test = try await getTestMethodCompletion(Position(line: 5, utf16index: 9), label: "test(a: Int)")
XCTAssertNotNil(test)
if let test = test {
XCTAssertEqual(test.kind, .method)
XCTAssertEqual(test.detail, "Void")
XCTAssertEqual(test.filterText, "test(a:)")
XCTAssertEqual(
test.textEdit,
.textEdit(
TextEdit(
range: Position(line: 5, utf16index: 9)..<Position(line: 5, utf16index: 9),
newText: "test(a: ${1:Int})"
)
)
)
XCTAssertEqual(test.insertText, "test(a: ${1:Int})")
XCTAssertEqual(test.insertTextFormat, .snippet)
}
test = try await getTestMethodCompletion(Position(line: 9, utf16index: 9), label: "test(b: Int)")
XCTAssertNotNil(test)
if let test = test {
XCTAssertEqual(test.kind, .method)
XCTAssertEqual(test.detail, "Void")
XCTAssertEqual(test.filterText, "test(:)")
XCTAssertEqual(
test.textEdit,
.textEdit(
TextEdit(
range: Position(line: 9, utf16index: 9)..<Position(line: 9, utf16index: 9),
newText: "test(${1:Int})"
)
)
)
XCTAssertEqual(test.insertText, "test(${1:Int})")
XCTAssertEqual(test.insertTextFormat, .snippet)
}
}
func testCompletionNoSnippetSupport() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(text, uri: uri)
func getTestMethodCompletion(_ position: Position, label: String) async throws -> CompletionItem? {
let selfDot = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: position
)
)
return selfDot.items.first { $0.label == label }
}
var test = try await getTestMethodCompletion(Position(line: 5, utf16index: 9), label: "test(a: Int)")
XCTAssertNotNil(test)
if let test = test {
XCTAssertEqual(test.kind, .method)
XCTAssertEqual(test.detail, "Void")
XCTAssertEqual(test.filterText, "test(a:)")
XCTAssertEqual(
test.textEdit,
.textEdit(
TextEdit(range: Position(line: 5, utf16index: 9)..<Position(line: 5, utf16index: 9), newText: "test(a: )")
)
)
XCTAssertEqual(test.insertText, "test(a: )")
XCTAssertEqual(test.insertTextFormat, .plain)
}
test = try await getTestMethodCompletion(Position(line: 9, utf16index: 9), label: "test(b: Int)")
XCTAssertNotNil(test)
if let test = test {
XCTAssertEqual(test.kind, .method)
XCTAssertEqual(test.detail, "Void")
XCTAssertEqual(test.filterText, "test(:)")
// FIXME:
XCTAssertEqual(
test.textEdit,
.textEdit(
TextEdit(range: Position(line: 9, utf16index: 9)..<Position(line: 9, utf16index: 9), newText: "test()")
)
)
XCTAssertEqual(test.insertText, "test()")
XCTAssertEqual(test.insertTextFormat, .plain)
}
}
func testCompletionPositionServerFilter() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument("foo", uri: uri)
for col in 0...3 {
let inOrAfterFoo = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 0, utf16index: col)
)
)
XCTAssertTrue(inOrAfterFoo.isIncomplete)
XCTAssertFalse(inOrAfterFoo.items.isEmpty)
}
let outOfRange1 = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 0, utf16index: 4)
)
)
XCTAssertTrue(outOfRange1.isIncomplete)
let outOfRange2 = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 1, utf16index: 0)
)
)
XCTAssertTrue(outOfRange2.isIncomplete)
}
func testCompletionOptional() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(
"""
struct Foo {
let bar: Int
}
let a: Foo? = Foo(bar: 1)
a.ba
""",
uri: uri
)
for col in 2...4 {
let response = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 4, utf16index: col)
)
)
guard let item = response.items.first(where: { $0.label.contains("bar") }) else {
XCTFail("No completion item with label 'bar'")
return
}
XCTAssertEqual(item.filterText, ".bar")
XCTAssertEqual(
item.textEdit,
.textEdit(
TextEdit(range: Position(line: 4, utf16index: 1)..<Position(line: 4, utf16index: col), newText: "?.bar")
)
)
}
}
func testCompletionOverride() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(
"""
class Base {
func foo() {}
}
class C: Base {
func // don't delete trailing space in this file
}
""",
uri: uri
)
let response = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 4, utf16index: 7)
)
)
guard let item = response.items.first(where: { $0.label == "foo()" }) else {
XCTFail("No completion item with label 'foo()'")
return
}
// FIXME: should be "foo()"
XCTAssertEqual(item.filterText, "func foo()")
XCTAssertEqual(
item.textEdit,
.textEdit(
TextEdit(
range: Position(line: 4, utf16index: 2)..<Position(line: 4, utf16index: 7),
newText: "override func foo() {\n\n}"
)
)
)
}
func testCompletionOverrideInNewLine() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(
"""
class Base {
func foo() {}
}
class C: Base {
func
// don't delete trailing space in this file
}
""",
uri: uri
)
let response = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 5, utf16index: 2)
)
)
guard let item = response.items.first(where: { $0.label == "foo()" }) else {
XCTFail("No completion item with label 'foo()'")
return
}
// If the edit would cross multiple lines, we are currently not replacing any text. It's not technically correct but the best we can do.
XCTAssertEqual(item.filterText, "foo()")
XCTAssertEqual(
item.textEdit,
.textEdit(
TextEdit(
range: Position(line: 5, utf16index: 2)..<Position(line: 5, utf16index: 2),
newText: "override func foo() {\n\n}"
)
)
)
}
func testRefilterAfterIncompleteResults() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(
"""
struct S {
func fooAbc() {}
func fooBcd() {}
func fooCde() {}
func fooDef() {}
func fooGoop() {}
func test() {
self.fcdez
}
}
""",
uri: uri
)
assertEqual(
5,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 10),
context: CompletionContext(triggerKind: .invoked)
)
)
)
)
assertEqual(
3,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 11),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
assertEqual(
2,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 12),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
assertEqual(
1,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 13),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
assertEqual(
0,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 14),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
assertEqual(
2,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 12),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
assertEqual(
1,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 13),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
// Trigger kind changed => OK (200 is maxResults since we're outside the member completion)
assertEqual(
200,
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 0),
context: CompletionContext(triggerKind: .invoked)
)
).items.count
)
}
func testRefilterAfterIncompleteResultsWithEdits() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(
"""
struct S {
func fooAbc() {}
func fooBcd() {}
func fooCde() {}
func fooDef() {}
func fooGoop() {}
func test() {
self.fz
}
}
""",
uri: uri
)
// 'f'
assertEqual(
5,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 10),
context: CompletionContext(triggerKind: .invoked)
)
)
)
)
// 'fz'
assertEqual(
0,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 11),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
testClient.send(
DidChangeTextDocumentNotification(
textDocument: VersionedTextDocumentIdentifier(uri, version: 1),
contentChanges: [
.init(range: Position(line: 7, utf16index: 10)..<Position(line: 7, utf16index: 11), text: "A ")
]
)
)
// 'fA'
assertEqual(
1,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 11),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
// 'fA '
assertEqual(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 12),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
).items,
[]
)
testClient.send(
DidChangeTextDocumentNotification(
textDocument: VersionedTextDocumentIdentifier(uri, version: 1),
contentChanges: [
.init(range: Position(line: 7, utf16index: 10)..<Position(line: 7, utf16index: 11), text: "Ab")
]
)
)
// 'fAb'
assertEqual(
1,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 11),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
testClient.send(
DidChangeTextDocumentNotification(
textDocument: VersionedTextDocumentIdentifier(uri, version: 1),
contentChanges: [
.init(range: Position(line: 7, utf16index: 10)..<Position(line: 7, utf16index: 11), text: "")
]
)
)
// 'fb'
assertEqual(
2,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 11),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
testClient.send(
DidChangeTextDocumentNotification(
textDocument: VersionedTextDocumentIdentifier(uri, version: 1),
contentChanges: [
.init(range: Position(line: 7, utf16index: 11)..<Position(line: 7, utf16index: 11), text: "d")
]
)
)
// 'fbd'
assertEqual(
1,
countFs(
try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 7, utf16index: 12),
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
)
)
}
/// Regression test for https://bugs.swift.org/browse/SR-13561 to make sure the a session
/// close waits for its respective open to finish to prevent a session geting stuck open.
func testSessionCloseWaitsforOpen() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
testClient.openDocument(
"""
struct S {
func forSomethingCrazy() {}
func forSomethingCool() {}
func test() {
self.forSome
}
func print() {}
func anotherOne() {
self.prin
}
}
""",
uri: uri
)
let forSomeComplete = CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 4, utf16index: 12), // forS^
context: CompletionContext(triggerKind: .invoked)
)
let printComplete = CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: Position(line: 8, utf16index: 12), // prin^
context: CompletionContext(triggerKind: .invoked)
)
// Code completion for "self.forSome"
async let forSomeResult = testClient.send(forSomeComplete)
// Code completion for "self.prin", previously could immediately invalidate
// the previous request.
async let printResult = testClient.send(printComplete)
assertEqual(2, countFs(try await forSomeResult))
assertEqual(1, try await printResult.items.count)
// Try code completion for "self.forSome" again to verify that it still works.
let result = try await testClient.send(forSomeComplete)
XCTAssertEqual(2, countFs(result))
}
func testCodeCompleteSwiftPackage() async throws {
let project = try await SwiftPMTestProject(
files: [
"a.swift": """
struct A {
func method(a b: Int) {}
}
""",
"b.swift": """
func test(a: A) {
a.1️⃣
}
""",
]
)
let (uri, positions) = try project.openDocument("b.swift")
let testPosition = positions["1️⃣"]
let results = try await project.testClient.send(
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: testPosition)
)
XCTAssertEqual(
results.items,
[
CompletionItem(
label: "method(a: Int)",
kind: .method,
detail: "Void",
deprecated: false,
sortText: nil,
filterText: "method(a:)",
insertText: "method(a: )",
insertTextFormat: .plain,
textEdit: .textEdit(
TextEdit(
range: Range(testPosition),
newText: "method(a: )"
)
)
),
CompletionItem(
label: "self",
kind: .keyword,
detail: "A",
deprecated: false,
sortText: nil,
filterText: "self",
insertText: "self",
insertTextFormat: .plain,
textEdit: .textEdit(
TextEdit(range: Range(testPosition), newText: "self")
)
),
]
)
}
func testTriggerFromIncompleteAfterStartingStringLiteral() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
func foo(_ x: String) {}
func test() {
foo1️⃣
}
""",
uri: uri
)
// The following is a pattern that VS Code sends. Make sure we don't return an error.
// - Insert `()``, changing the line to `foo()``
// - Invoke code completion after `(`
// - Insert `""`, changing the line to `foo("")`
// - Insert `d` inside the string literal, changing the line to `foo("d")`
// - Ask for completion with the `triggerFromIncompleteCompletions` flag set.
// Since this isn't actually re-filtering but is a completely new code completion session. When we detect this, we
// should just start a new session and return the results.
var position = positions["1️⃣"]
testClient.send(
DidChangeTextDocumentNotification(
textDocument: VersionedTextDocumentIdentifier(uri, version: position.utf16index),
contentChanges: [TextDocumentContentChangeEvent(range: Range(position), text: "()")]
)
)
position.utf16index += 1
let initialCompletionResults = try await testClient.send(
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: position)
)
// Test that we get the "abc" result which makes VS Code think that we are still in the same completion session when doing hte second completion.
XCTAssert(initialCompletionResults.items.contains(where: { $0.label == #""abc""# }))
testClient.send(
DidChangeTextDocumentNotification(
textDocument: VersionedTextDocumentIdentifier(uri, version: position.utf16index),
contentChanges: [TextDocumentContentChangeEvent(range: Range(position), text: "\"\"")]
)
)
position.utf16index += 1
testClient.send(
DidChangeTextDocumentNotification(
textDocument: VersionedTextDocumentIdentifier(uri, version: position.utf16index),
contentChanges: [TextDocumentContentChangeEvent(range: Range(position), text: "d")]
)
)
let secondCompletionResults = try await testClient.send(
CompletionRequest(
textDocument: TextDocumentIdentifier(uri),
position: position,
context: CompletionContext(triggerKind: .triggerFromIncompleteCompletions)
)
)
// We shouldn't be getting code completion results for inside the string literal.
XCTAssert(secondCompletionResults.items.isEmpty)
}
func testNonAsciiCompletionFilter() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
struct Foo {
func 👨👩👦👨👩👦() {}
func test() {
self.1️⃣👨👩👦2️⃣
}
}
""",
uri: uri
)
let completions = try await testClient.send(
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
)
XCTAssertEqual(
completions.items,
[
CompletionItem(
label: "👨👩👦👨👩👦()",
kind: .method,
detail: "Void",
deprecated: false,
filterText: "👨👩👦👨👩👦()",
insertText: "👨👩👦👨👩👦()",
insertTextFormat: .plain,
textEdit: .textEdit(TextEdit(range: positions["1️⃣"]..<positions["2️⃣"], newText: "👨👩👦👨👩👦()"))
)
]
)
}
func testExpandClosurePlaceholder() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
struct MyArray {
func myMap(_ body: (Int) -> Bool) {}
}
func test(x: MyArray) {
x.1️⃣
}
""",
uri: uri
)
let completions = try await testClient.send(
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
XCTAssertEqual(
completions.items.filter { $0.label.contains("myMap") },
[
CompletionItem(
label: "myMap(body: (Int) -> Bool)",
kind: .method,
detail: "Void",
deprecated: false,
sortText: nil,
filterText: "myMap(:)",
insertText: """
myMap { ${1:Int} in
${2:Bool}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: """
myMap { ${1:Int} in
${2:Bool}
}
"""
)
)
)
]
)
}
func testExpandClosurePlaceholderOnOptional() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
struct MyArray {
func myMap(_ body: (Int) -> Bool) {}
}
func test(x: MyArray?) {
x1️⃣.2️⃣
}
""",
uri: uri
)
let completions = try await testClient.send(
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
)
XCTAssertEqual(
completions.items.filter { $0.label.contains("myMap") },
[
CompletionItem(
label: "?.myMap(body: (Int) -> Bool)",
kind: .method,
detail: "Void",
deprecated: false,
sortText: nil,
filterText: ".myMap(:)",
insertText: """
?.myMap { ${1:Int} in
${2:Bool}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: positions["1️⃣"]..<positions["2️⃣"],
newText: """
?.myMap { ${1:Int} in
${2:Bool}
}
"""
)
)
)
]
)
}
func testExpandMultipleClosurePlaceholders() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
struct MyArray {
func myMap(_ body: (Int) -> Bool, _ second: (Int) -> String) {}
}
func test(x: MyArray) {
x.1️⃣
}
""",
uri: uri
)
let completions = try await testClient.send(
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
XCTAssertEqual(
completions.items.filter { $0.label.contains("myMap") },
[
CompletionItem(
label: "myMap(body: (Int) -> Bool, second: (Int) -> String)",
kind: .method,
detail: "Void",
deprecated: false,
sortText: nil,
filterText: "myMap(::)",
insertText: """
myMap { ${1:Int} in
${2:Bool}
} _: { ${3:Int} in
${4:String}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: """
myMap { ${1:Int} in
${2:Bool}
} _: { ${3:Int} in
${4:String}
}
"""
)
)
)
]
)
}
func testExpandMultipleClosurePlaceholdersWithLabel() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
struct MyArray {
func myMap(_ body: (Int) -> Bool, second: (Int) -> String) {}
}
func test(x: MyArray) {
x.1️⃣
}
""",
uri: uri
)
let completions = try await testClient.send(
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
XCTAssertEqual(
completions.items.filter { $0.label.contains("myMap") },
[
CompletionItem(
label: "myMap(body: (Int) -> Bool, second: (Int) -> String)",
kind: .method,
detail: "Void",
deprecated: false,
sortText: nil,
filterText: "myMap(:second:)",
insertText: """
myMap { ${1:Int} in
${2:Bool}
} second: { ${3:Int} in
${4:String}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: """
myMap { ${1:Int} in
${2:Bool}
} second: { ${3:Int} in
${4:String}
}
"""
)
)
)
]
)
}
func testInferIndentationWhenExpandingClosurePlaceholder() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
struct MyArray {
func myMap(_ body: (Int) -> Bool) -> Int {
return 1
}
}
func test(x: MyArray) {
x.1️⃣
}
""",
uri: uri
)
let completions = try await testClient.send(
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
XCTAssertEqual(
completions.items.filter { $0.label.contains("myMap") },
[
CompletionItem(
label: "myMap(body: (Int) -> Bool)",
kind: .method,
detail: "Int",
deprecated: false,
sortText: nil,
filterText: "myMap(:)",
insertText: """
myMap { ${1:Int} in
${2:Bool}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: """
myMap { ${1:Int} in
${2:Bool}
}
"""
)
)
)
]
)
}
}
private func countFs(_ response: CompletionList) -> Int {
return response.items.filter { $0.label.hasPrefix("f") }.count
}
|