Skip to content

Commit e6df9e7

Browse files
committed
Update for new ExpandEditorPlaceholdersToLiteralClosures API
This refactoring now takes a `Syntax` parameter. We don't actually need to change anything else to handle placeholder expansion for macro expansion completions since they get parsed as function calls.
1 parent 98fe45e commit e6df9e7

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

Sources/SourceKitLSP/Swift/CodeCompletionSession.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,14 @@ class CodeCompletionSession {
405405
exprToExpand = insertText
406406
}
407407

408+
// Note we don't need special handling for macro expansions since
409+
// their insertion text doesn't include the '#', so are parsed as
410+
// function calls here.
408411
var parser = Parser(exprToExpand)
409412
let expr = ExprSyntax.parse(from: &parser)
410413
guard let call = OutermostFunctionCallFinder.findOutermostFunctionCall(in: expr),
411414
let expandedCall = ExpandEditorPlaceholdersToLiteralClosures.refactor(
412-
syntax: call,
415+
syntax: Syntax(call),
413416
in: ExpandEditorPlaceholdersToLiteralClosures.Context(
414417
format: .custom(
415418
ClosureCompletionFormat(indentationWidth: indentationWidth),

Tests/SourceKitLSPTests/SwiftCompletionTests.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,73 @@ final class SwiftCompletionTests: XCTestCase {
10581058
)
10591059
}
10601060

1061+
func testExpandMacroClosurePlaceholder() async throws {
1062+
try await SkipUnless.sourcekitdSupportsPlugin()
1063+
1064+
let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
1065+
let uri = DocumentURI(for: .swift)
1066+
let positions = testClient.openDocument(
1067+
"""
1068+
@freestanding(expression)
1069+
macro myMacroExpr(fn: (Int) -> String) = #externalMacro(module: "", type: "")
1070+
1071+
@freestanding(declaration)
1072+
macro myMacroDecl(fn1: (Int) -> String, fn2: () -> Void) = #externalMacro(module: "", type: "")
1073+
1074+
func test() {
1075+
#1️⃣
1076+
}
1077+
""",
1078+
uri: uri
1079+
)
1080+
let completions = try await testClient.send(
1081+
CompletionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
1082+
)
1083+
XCTAssertEqual(
1084+
completions.items.clearingUnstableValues.filter { $0.label.contains("myMacro") },
1085+
[
1086+
CompletionItem(
1087+
label: "myMacroDecl(fn1: (Int) -> String, fn2: () -> Void)",
1088+
kind: .value,
1089+
detail: "Declaration Macro",
1090+
deprecated: false,
1091+
filterText: "myMacroDecl(fn1:fn2:)",
1092+
insertText: #"""
1093+
myMacroDecl(fn1: ${1:{ ${2:Int} in ${3:String} \}}, fn2: ${4:{ ${5:Void} \}})
1094+
"""#,
1095+
insertTextFormat: .snippet,
1096+
textEdit: .textEdit(
1097+
TextEdit(
1098+
range: Range(positions["1️⃣"]),
1099+
newText: #"""
1100+
myMacroDecl(fn1: ${1:{ ${2:Int} in ${3:String} \}}, fn2: ${4:{ ${5:Void} \}})
1101+
"""#
1102+
)
1103+
)
1104+
),
1105+
CompletionItem(
1106+
label: "myMacroExpr(fn: (Int) -> String)",
1107+
kind: .value,
1108+
detail: "Void",
1109+
deprecated: false,
1110+
filterText: "myMacroExpr(fn:)",
1111+
insertText: #"""
1112+
myMacroExpr(fn: ${1:{ ${2:Int} in ${3:String} \}})
1113+
"""#,
1114+
insertTextFormat: .snippet,
1115+
textEdit: .textEdit(
1116+
TextEdit(
1117+
range: Range(positions["1️⃣"]),
1118+
newText: #"""
1119+
myMacroExpr(fn: ${1:{ ${2:Int} in ${3:String} \}})
1120+
"""#
1121+
)
1122+
)
1123+
),
1124+
]
1125+
)
1126+
}
1127+
10611128
func testCompletionScoring() async throws {
10621129
try await SkipUnless.sourcekitdSupportsPlugin()
10631130

0 commit comments

Comments
 (0)