@@ -15,8 +15,6 @@ import LSPLogging
15
15
import LanguageServerProtocol
16
16
import SourceKitD
17
17
18
- @_spi ( Testing) public typealias MacroExpansionEdit = RefactoringEdit
19
-
20
18
/// Detailed information about the result of a macro expansion operation.
21
19
///
22
20
/// Wraps the information returned by sourcekitd's `semantic_refactoring`
@@ -28,15 +26,15 @@ struct MacroExpansion: RefactoringResponse {
28
26
/// The URI of the file where the macro is used
29
27
var uri : DocumentURI
30
28
31
- /// The resulting array of `MacroExpansionEdit ` of a semantic refactoring request
32
- var edits : [ MacroExpansionEdit ]
29
+ /// The resulting array of `RefactoringEdit ` of a semantic refactoring request
30
+ var edits : [ RefactoringEdit ]
33
31
34
32
init ( title: String , uri: DocumentURI , refactoringEdits: [ RefactoringEdit ] ) {
35
33
self . title = title
36
34
self . uri = uri
37
35
self . edits = refactoringEdits. compactMap { refactoringEdit in
38
36
if refactoringEdit. bufferName. isEmpty {
39
- logger. error ( " Unable to retrieve some parts of the expansion " )
37
+ logger. fault ( " Unable to retrieve some parts of the expansion " )
40
38
return nil
41
39
}
42
40
@@ -53,12 +51,9 @@ extension SwiftLanguageService {
53
51
/// expansion to be displayed.
54
52
///
55
53
/// - Parameters:
56
- /// - expandMacroCommand: The `ExpandMacroCommand` that triggered this
57
- /// request.
54
+ /// - expandMacroCommand: The `ExpandMacroCommand` that triggered this request.
58
55
///
59
- /// - Returns:
60
- /// - an `[MacroExpansionEdit]` with the necessary edits and buffer name as
61
- /// a `LSPAny`
56
+ /// - Returns: An `[RefactoringEdit]` with the necessary edits and buffer name as a `LSPAny`
62
57
func expandMacro(
63
58
_ expandMacroCommand: ExpandMacroCommand
64
59
) async throws -> LSPAny {
@@ -69,17 +64,20 @@ extension SwiftLanguageService {
69
64
}
70
65
71
66
guard let sourceFileURL = expandMacroCommand. textDocument. uri. fileURL else {
72
- throw ResponseError . unknown ( " Given URL is not a file URL " )
67
+ throw ResponseError . unknown ( " Given URI is not a file URL " )
73
68
}
74
69
75
70
let expansion = try await self . refactoring ( expandMacroCommand)
76
71
77
72
for macroEdit in expansion. edits {
78
- let macroExpansionBufferDirectoryName = macroEdit. bufferName. dropLast ( 6 ) // buffer name without ".swift"
79
-
80
- let macroExpansionBufferDirectoryURL = self . generatedMacroExpansionsPath. appendingPathComponent (
81
- String ( macroExpansionBufferDirectoryName)
82
- )
73
+ // buffer name without ".swift"
74
+ let macroExpansionBufferDirectoryName =
75
+ macroEdit. bufferName. hasSuffix ( " .swift " )
76
+ ? String ( macroEdit. bufferName. dropLast ( 6 ) )
77
+ : macroEdit. bufferName
78
+
79
+ let macroExpansionBufferDirectoryURL = self . generatedMacroExpansionsPath
80
+ . appendingPathComponent ( macroExpansionBufferDirectoryName)
83
81
do {
84
82
try FileManager . default. createDirectory ( at: macroExpansionBufferDirectoryURL, withIntermediateDirectories: true )
85
83
} catch {
@@ -96,22 +94,20 @@ extension SwiftLanguageService {
96
94
" \( macroExpansionFileName) _ \( macroExpansionPositionRangeIndicator) . \( sourceFileURL. pathExtension) "
97
95
)
98
96
99
- let macroExpansionDocURI = DocumentURI ( macroExpansionFilePath)
100
-
101
97
do {
102
- try macroEdit. newText. write ( to: macroExpansionFilePath, atomically: true , encoding: String . Encoding . utf8)
98
+ try macroEdit. newText. write ( to: macroExpansionFilePath, atomically: true , encoding: . utf8)
103
99
} catch {
104
100
throw ResponseError . unknown ( " Unable to write macro expansion to file path: \" \( macroExpansionFilePath. path) \" " )
105
101
}
106
102
107
103
Task {
108
- let req = ShowDocumentRequest ( uri: macroExpansionDocURI , selection: macroEdit. range)
104
+ let req = ShowDocumentRequest ( uri: DocumentURI ( macroExpansionFilePath ) , selection: macroEdit. range)
109
105
110
106
let response = await orLog ( " Sending ShowDocumentRequest to Client " ) {
111
107
try await sourceKitLSPServer. sendRequestToClient ( req)
112
108
}
113
109
114
- if ! ( response? . success ?? true ) {
110
+ if let response , ! response. success {
115
111
logger. error ( " client refused to show document for \( expansion. title, privacy: . public) " )
116
112
}
117
113
}
0 commit comments