-
Notifications
You must be signed in to change notification settings - Fork 327
[#2118] Implement keyword snippets for control-flow keywords (if/for/while/etc.) #2387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+377
−6
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
Tests/SourceKitLSPTests/SwiftCompletetionSnippetTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| @_spi(SourceKitLSP) import LanguageServerProtocol | ||
DPrakashh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import SKLogging | ||
| import SKTestSupport | ||
| import SourceKitLSP | ||
| import SwiftExtensions | ||
| import XCTest | ||
|
|
||
| final class SwiftCompletionSnippetTests: SourceKitLSPTestCase { | ||
| private var snippetCapabilities = ClientCapabilities( | ||
| textDocument: TextDocumentClientCapabilities( | ||
| completion: TextDocumentClientCapabilities.Completion( | ||
| completionItem: TextDocumentClientCapabilities.Completion.CompletionItem(snippetSupport: true) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| func testKeywordIfProvidesSnippet() async throws { | ||
| try await SkipUnless.sourcekitdSupportsPlugin() | ||
|
|
||
| let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities) | ||
| let uri = DocumentURI(for: .swift) | ||
| let positions = testClient.openDocument( | ||
| """ | ||
| func test() { | ||
| 1️⃣ | ||
| } | ||
| """, | ||
| uri: uri | ||
| ) | ||
|
|
||
| let completions = try await testClient.send( | ||
| CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"]) | ||
| ) | ||
|
|
||
| guard let ifItem = completions.items.first(where: { $0.label == "if" }) else { | ||
| XCTFail("No completion item with label 'if'") | ||
| return | ||
| } | ||
DPrakashh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| XCTAssertEqual(ifItem.kind, .keyword) | ||
| XCTAssertEqual(ifItem.insertTextFormat, .snippet) | ||
|
|
||
| guard let insertText = ifItem.insertText else { | ||
| XCTFail("Completion item for 'if' has no insertText") | ||
| return | ||
| } | ||
| XCTAssertTrue(insertText.contains("${1:condition}")) | ||
| XCTAssertTrue(insertText.contains("${0:")) | ||
| } | ||
|
|
||
| func testKeywordForProvidesSnippet() async throws { | ||
| try await SkipUnless.sourcekitdSupportsPlugin() | ||
|
|
||
| let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities) | ||
| let uri = DocumentURI(for: .swift) | ||
| let positions = testClient.openDocument( | ||
| """ | ||
| func test() { | ||
| 1️⃣ | ||
| } | ||
| """, | ||
| uri: uri | ||
| ) | ||
|
|
||
| let completions = try await testClient.send( | ||
| CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"]) | ||
| ) | ||
|
|
||
| guard let forItem = completions.items.first(where: { $0.label == "for" }) else { | ||
| XCTFail("No completion item with label 'for'") | ||
| return | ||
| } | ||
|
|
||
| XCTAssertEqual(forItem.kind, .keyword) | ||
| XCTAssertEqual(forItem.insertTextFormat, .snippet) | ||
|
|
||
| guard let insertText = forItem.insertText else { | ||
| XCTFail("Completion item for 'for' has no insertText") | ||
| return | ||
| } | ||
| XCTAssertTrue(insertText.contains("${1:item}")) | ||
| XCTAssertTrue(insertText.contains("${2:sequence}")) | ||
| } | ||
|
|
||
| func testKeywordWhileProvidesSnippet() async throws { | ||
| try await SkipUnless.sourcekitdSupportsPlugin() | ||
|
|
||
| let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities) | ||
| let uri = DocumentURI(for: .swift) | ||
| let positions = testClient.openDocument( | ||
| """ | ||
| func test() { | ||
| 1️⃣ | ||
| } | ||
| """, | ||
| uri: uri | ||
| ) | ||
|
|
||
| let completions = try await testClient.send( | ||
| CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"]) | ||
| ) | ||
|
|
||
| guard let whileItem = completions.items.first(where: { $0.label == "while" }) else { | ||
| XCTFail("No completion item with label 'while'") | ||
| return | ||
| } | ||
|
|
||
| XCTAssertEqual(whileItem.kind, .keyword) | ||
| XCTAssertEqual(whileItem.insertTextFormat, .snippet) | ||
|
|
||
| guard let insertText = whileItem.insertText else { | ||
| XCTFail("Completion item for 'while' has no insertText") | ||
| return | ||
| } | ||
| XCTAssertTrue(insertText.contains("${1:condition}")) | ||
| } | ||
|
|
||
| func testKeywordGuardProvidesSnippet() async throws { | ||
| try await SkipUnless.sourcekitdSupportsPlugin() | ||
|
|
||
| let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities) | ||
| let uri = DocumentURI(for: .swift) | ||
| let positions = testClient.openDocument( | ||
| """ | ||
| func test() { | ||
| 1️⃣ | ||
| } | ||
| """, | ||
| uri: uri | ||
| ) | ||
|
|
||
| let completions = try await testClient.send( | ||
| CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"]) | ||
| ) | ||
|
|
||
| guard let guardItem = completions.items.first(where: { $0.label == "guard" }) else { | ||
| XCTFail("No completion item with label 'guard'") | ||
| return | ||
| } | ||
|
|
||
| XCTAssertEqual(guardItem.kind, .keyword) | ||
| XCTAssertEqual(guardItem.insertTextFormat, .snippet) | ||
|
|
||
| guard let insertText = guardItem.insertText else { | ||
| XCTFail("Completion item for 'guard' has no insertText") | ||
| return | ||
| } | ||
| XCTAssertTrue(insertText.contains("${1:condition}")) | ||
| XCTAssertTrue(insertText.contains("else")) | ||
| } | ||
|
|
||
| func testKeywordSwitchProvidesSnippet() async throws { | ||
| try await SkipUnless.sourcekitdSupportsPlugin() | ||
|
|
||
| let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities) | ||
| let uri = DocumentURI(for: .swift) | ||
| let positions = testClient.openDocument( | ||
| """ | ||
| func test() { | ||
| 1️⃣ | ||
| } | ||
| """, | ||
| uri: uri | ||
| ) | ||
|
|
||
| let completions = try await testClient.send( | ||
| CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"]) | ||
| ) | ||
|
|
||
| guard let switchItem = completions.items.first(where: { $0.label == "switch" }) else { | ||
| XCTFail("No completion item with label 'switch'") | ||
| return | ||
| } | ||
|
|
||
| XCTAssertEqual(switchItem.kind, .keyword) | ||
| XCTAssertEqual(switchItem.insertTextFormat, .snippet) | ||
|
|
||
| guard let insertText = switchItem.insertText else { | ||
| XCTFail("Completion item for 'switch' has no insertText") | ||
| return | ||
| } | ||
| XCTAssertTrue(insertText.contains("${1:value}")) | ||
| XCTAssertTrue(insertText.contains("case")) | ||
| } | ||
|
|
||
| func testKeywordRepeatProvidesSnippet() async throws { | ||
| try await SkipUnless.sourcekitdSupportsPlugin() | ||
|
|
||
| let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities) | ||
| let uri = DocumentURI(for: .swift) | ||
| let positions = testClient.openDocument( | ||
| """ | ||
| func test() { | ||
| 1️⃣ | ||
| } | ||
| """, | ||
| uri: uri | ||
| ) | ||
|
|
||
| let completions = try await testClient.send( | ||
| CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"]) | ||
| ) | ||
|
|
||
| guard let repeatItem = completions.items.first(where: { $0.label == "repeat" }) else { | ||
| XCTFail("No completion item with label 'repeat'") | ||
| return | ||
| } | ||
|
|
||
| XCTAssertEqual(repeatItem.kind, .keyword) | ||
| XCTAssertEqual(repeatItem.insertTextFormat, .snippet) | ||
|
|
||
| guard let insertText = repeatItem.insertText else { | ||
| XCTFail("Completion item for 'repeat' has no insertText") | ||
| return | ||
| } | ||
| XCTAssertTrue(insertText.contains("while")) | ||
| } | ||
|
|
||
| func testKeywordWithoutSnippetSupport() async throws { | ||
| try await SkipUnless.sourcekitdSupportsPlugin() | ||
|
|
||
| // Client without snippet support should get plain keywords | ||
| let testClient = try await TestSourceKitLSPClient() | ||
| let uri = DocumentURI(for: .swift) | ||
| let positions = testClient.openDocument( | ||
| """ | ||
| func test() { | ||
| 1️⃣ | ||
| } | ||
| """, | ||
| uri: uri | ||
| ) | ||
|
|
||
| let completions = try await testClient.send( | ||
| CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"]) | ||
| ) | ||
|
|
||
| guard let ifItem = completions.items.first(where: { $0.label == "if" }) else { | ||
| XCTFail("No completion item with label 'if'") | ||
| return | ||
| } | ||
|
|
||
| XCTAssertEqual(ifItem.kind, .keyword) | ||
| XCTAssertEqual(ifItem.insertTextFormat, .plain) | ||
| XCTAssertEqual(ifItem.insertText, "if") | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.