Skip to content

Commit 237cbeb

Browse files
committed
Use semantic refactoring to perform macro expansion
1 parent 88a71c9 commit 237cbeb

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,21 +1255,22 @@ extension SwiftLanguageServer {
12551255
}
12561256

12571257
public func macroExpansion(_ req: MacroExpansionRequest) async throws -> MacroExpansion? {
1258-
guard let snapshot = documentManager.latestSnapshot(req.textDocument.uri) else {
1259-
let msg = "failed to find snapshot for url \(req.textDocument.uri)"
1260-
log(msg)
1261-
throw ResponseError.unknown(msg)
1262-
}
1263-
1264-
let syntaxTree = await syntaxTreeManager.syntaxTree(for: snapshot)
1258+
let command = SemanticRefactorCommand(
1259+
title: "Expand Macro",
1260+
actionString: "source.refactoring.kind.expand.macro",
1261+
positionRange: req.position..<req.position,
1262+
textDocument: req.textDocument
1263+
)
1264+
1265+
log("Performing refactoring", level: .warning) // DEBUG
1266+
let refactor = try await semanticRefactoring(command)
1267+
log("Got \(refactor)", level: .warning) // DEBUG
12651268

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

1272-
return nil
1273+
return MacroExpansion(sourceText: newText)
12731274
}
12741275

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

0 commit comments

Comments
 (0)