@@ -74,50 +74,38 @@ extension SwiftLanguageService {
74
74
var macroExpansionFilePaths : [ URL ] = [ ]
75
75
for macroEdit in expansion. edits {
76
76
if let bufferName = macroEdit. bufferName {
77
- // buffer name without ".swift"
78
- let macroExpansionBufferDirectoryName =
79
- bufferName. hasSuffix ( " .swift " )
80
- ? String ( bufferName. dropLast ( 6 ) )
81
- : bufferName
82
-
83
- let macroExpansionBufferDirectoryURL = self . generatedMacroExpansionsPath
84
- . appendingPathComponent ( macroExpansionBufferDirectoryName)
85
-
86
- completeExpansionDirectoryName += " \( bufferName) - "
87
-
88
- do {
89
- try FileManager . default. createDirectory (
90
- at: macroExpansionBufferDirectoryURL,
91
- withIntermediateDirectories: true
92
- )
93
- } catch {
94
- throw ResponseError . unknown (
95
- " Failed to create directory for macro expansion buffer at path: \( macroExpansionBufferDirectoryURL. path) "
96
- )
77
+ // Custom Scheme for Macro Expansions:
78
+ // `swift-macro-expansion://[path_to_source_file]/LaCb-LcCd.swift?lowerL=&lowerC=&upperL=&upperC=&bufferName=`
79
+ // --------------------- --------------------- --------------- ------------------------------- -----------
80
+ // 1 2 3 4 5
81
+ //
82
+ // 1. Scheme
83
+ // 2. The path to the source file
84
+ // 3. Position range of macro after expansion (used for display purpose in VS Code)
85
+ // 4. Selection range of the cursor
86
+ // 5. Buffer Name of the required Macro Expansion
87
+
88
+ guard var components = URLComponents ( string: sourceFileURL. absoluteString) else {
89
+ throw ResponseError . unknown ( " Invalid URL " )
97
90
}
98
-
99
- // name of the source file
100
- let macroExpansionFileName = sourceFileURL. deletingPathExtension ( ) . lastPathComponent
101
-
102
- // github permalink notation for position range
103
- let macroExpansionPositionRangeIndicator =
104
- " L \( macroEdit. range. lowerBound. line + 1 ) C \( macroEdit. range. lowerBound. utf16index + 1 ) -L \( macroEdit. range. upperBound. line + 1 ) C \( macroEdit. range. upperBound. utf16index + 1 ) "
105
-
106
- let macroExpansionFilePath =
107
- macroExpansionBufferDirectoryURL
108
- . appendingPathComponent (
109
- " \( macroExpansionFileName) _ \( macroExpansionPositionRangeIndicator) . \( sourceFileURL. pathExtension) "
110
- )
111
-
112
- do {
113
- try macroEdit. newText. write ( to: macroExpansionFilePath, atomically: true , encoding: . utf8)
114
- } catch {
115
- throw ResponseError . unknown (
116
- " Unable to write macro expansion to file path: \" \( macroExpansionFilePath. path) \" "
117
- )
91
+ components. scheme = " swift-macro-expansion "
92
+ components. path +=
93
+ " /L \( macroEdit. range. lowerBound. line + 1 ) C \( macroEdit. range. lowerBound. utf16index + 1 ) -L \( macroEdit. range. upperBound. line + 1 ) C \( macroEdit. range. upperBound. utf16index + 1 ) .swift "
94
+ let queryItems = [
95
+ URLQueryItem ( name: " lowerL " , value: String ( expandMacroCommand. positionRange. lowerBound. line) ) ,
96
+ URLQueryItem ( name: " lowerC " , value: String ( expandMacroCommand. positionRange. lowerBound. utf16index) ) ,
97
+ URLQueryItem ( name: " upperL " , value: String ( expandMacroCommand. positionRange. upperBound. line) ) ,
98
+ URLQueryItem ( name: " upperC " , value: String ( expandMacroCommand. positionRange. upperBound. utf16index) ) ,
99
+ URLQueryItem ( name: " bufferName " , value: bufferName) ,
100
+ ]
101
+ components. queryItems = queryItems
102
+
103
+ guard let url = components. url else {
104
+ throw ResponseError . unknown ( " Failed to construct URL " )
118
105
}
106
+ macroExpansionFilePaths. append ( url)
119
107
120
- macroExpansionFilePaths . append ( macroExpansionFilePath )
108
+ completeExpansionDirectoryName += " \( bufferName ) - "
121
109
122
110
let editContent =
123
111
"""
@@ -208,4 +196,29 @@ extension SwiftLanguageService {
208
196
}
209
197
}
210
198
}
199
+
200
+ func expandMacro( uri: DocumentURI , selectionRange: Range < Position > , bufferName: String ) async throws -> String {
201
+ guard let sourceKitLSPServer else {
202
+ // `SourceKitLSPServer` has been destructed. We are tearing down the
203
+ // language server. Nothing left to do.
204
+ throw ResponseError . unknown ( " Connection to the editor closed " )
205
+ }
206
+
207
+ let expandMacroCommand = ExpandMacroCommand (
208
+ positionRange: selectionRange,
209
+ textDocument: TextDocumentIdentifier ( uri)
210
+ )
211
+
212
+ let expansion = try await self . refactoring ( expandMacroCommand)
213
+
214
+ let macroExpansionEdit = expansion. edits. filter {
215
+ $0. bufferName == bufferName
216
+ } . only
217
+
218
+ guard let content = macroExpansionEdit? . newText else {
219
+ throw ResponseError . unknown ( " Macro Expansion Edit doesn't Exist " )
220
+ }
221
+
222
+ return content
223
+ }
211
224
}
0 commit comments