@@ -21,13 +21,15 @@ import SourceKitD
21
21
/// request, such as the necessary macro expansion edits.
22
22
struct MacroExpansion : RefactoringResponse {
23
23
/// The title of the refactoring action.
24
- var title : String
24
+ let title : String
25
25
26
26
/// The URI of the file where the macro is used
27
- var uri : DocumentURI
27
+ let uri : DocumentURI
28
28
29
29
/// The resulting array of `RefactoringEdit` of a semantic refactoring request
30
- var edits : [ RefactoringEdit ]
30
+ let edits : [ RefactoringEdit ]
31
+
32
+ var expansionURIs : [ DocumentURI ] ?
31
33
32
34
init ( title: String , uri: DocumentURI , refactoringEdits: [ RefactoringEdit ] ) {
33
35
self . title = title
@@ -40,6 +42,8 @@ struct MacroExpansion: RefactoringResponse {
40
42
41
43
return refactoringEdit
42
44
}
45
+
46
+ expansionURIs = nil
43
47
}
44
48
}
45
49
@@ -69,6 +73,9 @@ extension SwiftLanguageService {
69
73
70
74
let expansion = try await self . refactoring ( expandMacroCommand)
71
75
76
+ var completeExpansionFileContent = " "
77
+ var completeExpansionFilePath = self . generatedMacroExpansionsPath
78
+ var macroExpansionFilePaths = [ URL] ( )
72
79
for macroEdit in expansion. edits {
73
80
if let bufferName = macroEdit. bufferName {
74
81
// buffer name without ".swift"
@@ -79,6 +86,9 @@ extension SwiftLanguageService {
79
86
80
87
let macroExpansionBufferDirectoryURL = self . generatedMacroExpansionsPath
81
88
. appendingPathComponent ( macroExpansionBufferDirectoryName)
89
+ completeExpansionFilePath = completeExpansionFilePath
90
+ . appendingPathComponent ( macroExpansionBufferDirectoryName)
91
+
82
92
do {
83
93
try FileManager . default. createDirectory (
84
94
at: macroExpansionBufferDirectoryURL,
@@ -111,19 +121,71 @@ extension SwiftLanguageService {
111
121
)
112
122
}
113
123
114
- Task {
115
- let req = ShowDocumentRequest ( uri: DocumentURI ( macroExpansionFilePath) )
124
+ macroExpansionFilePaths. append ( macroExpansionFilePath)
116
125
117
- let response = await orLog ( " Sending ShowDocumentRequest to Client " ) {
118
- try await sourceKitLSPServer. sendRequestToClient ( req)
119
- }
126
+ let editContent = " // \( sourceFileURL. lastPathComponent) @ \( macroEdit. range. lowerBound. line + 1 ) : \( macroEdit. range. lowerBound. utf16index + 1 ) - \( macroEdit. range. upperBound. line + 1 ) : \( macroEdit. range. upperBound. utf16index + 1 ) \n \( macroEdit. newText) \n "
127
+ completeExpansionFileContent += editContent
128
+ } else if !macroEdit. newText. isEmpty {
129
+ logger. fault ( " Unable to retrieve some parts of macro expansion " )
130
+ }
131
+ }
120
132
121
- if let response, !response. success {
122
- logger. error ( " client refused to show document for \( expansion. title, privacy: . public) " )
133
+ do {
134
+ try FileManager . default. createDirectory (
135
+ at: completeExpansionFilePath,
136
+ withIntermediateDirectories: true
137
+ )
138
+ } catch {
139
+ throw ResponseError . unknown (
140
+ " Failed to create directory for complete macro expansion at path: \( completeExpansionFilePath. path) "
141
+ )
142
+ }
143
+
144
+ completeExpansionFilePath =
145
+ completeExpansionFilePath. appendingPathComponent ( sourceFileURL. lastPathComponent)
146
+ do {
147
+ try completeExpansionFileContent. write ( to: completeExpansionFilePath, atomically: true , encoding: . utf8)
148
+ } catch {
149
+ throw ResponseError . unknown (
150
+ " Unable to write complete macro expansion to file path: \" \( completeExpansionFilePath. path) \" "
151
+ )
152
+ }
153
+
154
+ let completeMacroExpansionFilePath = completeExpansionFilePath
155
+ let macroExpansion = LanguageServerProtocol . MacroExpansion (
156
+ expansionURIs: macroExpansionFilePaths. map {
157
+ return DocumentURI ( $0)
158
+ }
159
+ )
160
+
161
+ let peekDocument = true
162
+ if peekDocument {
163
+ Task {
164
+ let req = PeekMacroRequest ( macroExpansion: macroExpansion, peekLocation: expandMacroCommand. positionRange. lowerBound)
165
+
166
+ let response = await orLog ( " Sending PeekMacroRequest to Client " ) {
167
+ try await sourceKitLSPServer. sendRequestToClient ( req)
168
+ }
169
+
170
+ if let response, !response. success {
171
+ if let failureReason = response. failureReason {
172
+ logger. error ( " client refused to peek macro, reason: \( failureReason) " )
173
+ } else {
174
+ logger. error ( " client refused to peek macro " )
123
175
}
124
176
}
125
- } else if !macroEdit. newText. isEmpty {
126
- logger. fault ( " Unable to retrieve some parts of macro expansion " )
177
+ }
178
+ } else {
179
+ Task {
180
+ let req = ShowDocumentRequest ( uri: DocumentURI ( completeMacroExpansionFilePath) )
181
+
182
+ let response = await orLog ( " Sending ShowDocumentRequest to Client " ) {
183
+ try await sourceKitLSPServer. sendRequestToClient ( req)
184
+ }
185
+
186
+ if let response, !response. success {
187
+ logger. error ( " client refused to show document for macro expansion " )
188
+ }
127
189
}
128
190
}
129
191
0 commit comments