Skip to content

Commit 8899db8

Browse files
committed
Use semantic refactoring to perform macro expansion
1 parent c0468f9 commit 8899db8

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,21 +1300,20 @@ extension SwiftLanguageServer {
13001300
}
13011301

13021302
public func macroExpansion(_ req: MacroExpansionRequest) async throws -> MacroExpansion? {
1303-
guard let snapshot = documentManager.latestSnapshot(req.textDocument.uri) else {
1304-
let msg = "failed to find snapshot for url \(req.textDocument.uri)"
1305-
log(msg)
1306-
throw ResponseError.unknown(msg)
1307-
}
1308-
1309-
let syntaxTree = await syntaxTreeManager.syntaxTree(for: snapshot)
1303+
let command = SemanticRefactorCommand(
1304+
title: "Expand Macro",
1305+
actionString: "source.refactoring.kind.expand.macro",
1306+
positionRange: req.position..<req.position,
1307+
textDocument: req.textDocument
1308+
)
1309+
1310+
let refactor = try await semanticRefactoring(command)
13101311

1311-
// TODO: Find the moduleName, typeName and roles for the macro at the
1312-
// position given in the request somehow. Use these to construct
1313-
// `ExpansionSpecifier`s and pass them to SourceKitD via the
1314-
// `syntacticMacroExpansion` function. Extract the text edits from the
1315-
// result and wrap them in `MacroExpansion`.
1312+
guard let newText = refactor.edit.changes?[req.textDocument.uri]?.first?.newText else {
1313+
return nil
1314+
}
13161315

1317-
return nil
1316+
return MacroExpansion(sourceText: newText)
13181317
}
13191318

13201319
public func executeCommand(_ req: ExecuteCommandRequest) async throws -> LSPAny? {

0 commit comments

Comments
 (0)