Skip to content

Update for new ExpandEditorPlaceholdersToLiteralClosures API #2170

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
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/SourceKitLSP/Swift/CodeCompletionSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,14 @@ class CodeCompletionSession {
exprToExpand = insertText
}

// Note we don't need special handling for macro expansions since
// their insertion text doesn't include the '#', so are parsed as
// function calls here.
var parser = Parser(exprToExpand)
let expr = ExprSyntax.parse(from: &parser)
guard let call = OutermostFunctionCallFinder.findOutermostFunctionCall(in: expr),
let expandedCall = ExpandEditorPlaceholdersToLiteralClosures.refactor(
syntax: call,
syntax: Syntax(call),
in: ExpandEditorPlaceholdersToLiteralClosures.Context(
format: .custom(
ClosureCompletionFormat(indentationWidth: indentationWidth),
Expand Down
67 changes: 67 additions & 0 deletions Tests/SourceKitLSPTests/SwiftCompletionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,73 @@ final class SwiftCompletionTests: XCTestCase {
)
}

func testExpandMacroClosurePlaceholder() async throws {
try await SkipUnless.sourcekitdSupportsPlugin()

let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
let uri = DocumentURI(for: .swift)
let positions = testClient.openDocument(
"""
@freestanding(expression)
macro myMacroExpr(fn: (Int) -> String) = #externalMacro(module: "", type: "")

@freestanding(declaration)
macro myMacroDecl(fn1: (Int) -> String, fn2: () -> Void) = #externalMacro(module: "", type: "")

func test() {
#1️⃣
}
""",
uri: uri
)
let completions = try await testClient.send(
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
XCTAssertEqual(
completions.items.clearingUnstableValues.filter { $0.label.contains("myMacro") },
[
CompletionItem(
label: "myMacroDecl(fn1: (Int) -> String, fn2: () -> Void)",
kind: .value,
detail: "Declaration Macro",
deprecated: false,
filterText: "myMacroDecl(fn1:fn2:)",
insertText: #"""
myMacroDecl(fn1: ${1:{ ${2:Int} in ${3:String} \}}, fn2: ${4:{ ${5:Void} \}})
"""#,
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: #"""
myMacroDecl(fn1: ${1:{ ${2:Int} in ${3:String} \}}, fn2: ${4:{ ${5:Void} \}})
"""#
)
)
),
CompletionItem(
label: "myMacroExpr(fn: (Int) -> String)",
kind: .value,
detail: "Void",
deprecated: false,
filterText: "myMacroExpr(fn:)",
insertText: #"""
myMacroExpr(fn: ${1:{ ${2:Int} in ${3:String} \}})
"""#,
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: #"""
myMacroExpr(fn: ${1:{ ${2:Int} in ${3:String} \}})
"""#
)
)
),
]
)
}

func testCompletionScoring() async throws {
try await SkipUnless.sourcekitdSupportsPlugin()

Expand Down