@@ -33,7 +33,7 @@ struct MacroExpansion: RefactoringResponse {
33
33
self . title = title
34
34
self . uri = uri
35
35
self . edits = refactoringEdits. compactMap { refactoringEdit in
36
- if refactoringEdit. bufferName. isEmpty {
36
+ if refactoringEdit. bufferName == nil && !refactoringEdit . newText . isEmpty {
37
37
logger. fault ( " Unable to retrieve some parts of the expansion " )
38
38
return nil
39
39
}
@@ -46,14 +46,14 @@ struct MacroExpansion: RefactoringResponse {
46
46
extension SwiftLanguageService {
47
47
/// Handles the `ExpandMacroCommand`.
48
48
///
49
- /// Makes request to sourcekitd and wraps the result into a `MacroExpansion`
50
- /// and then makes `ShowDocumentRequest` to the client side for each
49
+ /// Makes a request to sourcekitd and wraps the result into a `MacroExpansion`
50
+ /// and then makes a `ShowDocumentRequest` to the client side for each
51
51
/// expansion to be displayed.
52
52
///
53
53
/// - Parameters:
54
54
/// - expandMacroCommand: The `ExpandMacroCommand` that triggered this request.
55
55
///
56
- /// - Returns: An `[RefactoringEdit]` with the necessary edits and buffer name as a `LSPAny`
56
+ /// - Returns: A `[RefactoringEdit]` with the necessary edits and buffer name as a `LSPAny`
57
57
func expandMacro(
58
58
_ expandMacroCommand: ExpandMacroCommand
59
59
) async throws -> LSPAny {
@@ -70,46 +70,57 @@ extension SwiftLanguageService {
70
70
let expansion = try await self . refactoring ( expandMacroCommand)
71
71
72
72
for macroEdit in expansion. edits {
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)
81
- do {
82
- try FileManager . default. createDirectory ( at: macroExpansionBufferDirectoryURL, withIntermediateDirectories: true )
83
- } catch {
84
- throw ResponseError . unknown (
85
- " Failed to create directory for Macro Expansion Buffer at path: \( macroExpansionBufferDirectoryURL. path) "
86
- )
87
- }
88
-
89
- let macroExpansionFileName = sourceFileURL. deletingPathExtension ( ) . lastPathComponent // name of the source file
90
- let macroExpansionPositionRangeIndicator =
91
- " L \( macroEdit. range. lowerBound. line) C \( macroEdit. range. lowerBound. utf16index) -L \( macroEdit. range. upperBound. line) C \( macroEdit. range. upperBound. utf16index) " // github permalink notation for position range
92
-
93
- let macroExpansionFilePath = macroExpansionBufferDirectoryURL. appendingPathComponent (
94
- " \( macroExpansionFileName) _ \( macroExpansionPositionRangeIndicator) . \( sourceFileURL. pathExtension) "
95
- )
73
+ if let bufferName = macroEdit. bufferName {
74
+ // buffer name without ".swift"
75
+ let macroExpansionBufferDirectoryName =
76
+ bufferName. hasSuffix ( " .swift " )
77
+ ? String ( bufferName. dropLast ( 6 ) )
78
+ : bufferName
79
+
80
+ let macroExpansionBufferDirectoryURL = self . generatedMacroExpansionsPath
81
+ . appendingPathComponent ( macroExpansionBufferDirectoryName)
82
+ do {
83
+ try FileManager . default. createDirectory (
84
+ at: macroExpansionBufferDirectoryURL,
85
+ withIntermediateDirectories: true
86
+ )
87
+ } catch {
88
+ throw ResponseError . unknown (
89
+ " Failed to create directory for macro expansion buffer at path: \( macroExpansionBufferDirectoryURL. path) "
90
+ )
91
+ }
96
92
97
- do {
98
- try macroEdit. newText. write ( to: macroExpansionFilePath, atomically: true , encoding: . utf8)
99
- } catch {
100
- throw ResponseError . unknown ( " Unable to write macro expansion to file path: \" \( macroExpansionFilePath. path) \" " )
101
- }
93
+ let macroExpansionFileName = sourceFileURL. deletingPathExtension ( ) . lastPathComponent // name of the source file
94
+ let macroExpansionPositionRangeIndicator =
95
+ " L \( macroEdit. range. lowerBound. line) C \( macroEdit. range. lowerBound. utf16index) -L \( macroEdit. range. upperBound. line) C \( macroEdit. range. upperBound. utf16index) " // github permalink notation for position range
96
+
97
+ let macroExpansionFilePath =
98
+ macroExpansionBufferDirectoryURL
99
+ . appendingPathComponent (
100
+ " \( macroExpansionFileName) _ \( macroExpansionPositionRangeIndicator) . \( sourceFileURL. pathExtension) "
101
+ )
102
+
103
+ do {
104
+ try macroEdit. newText. write ( to: macroExpansionFilePath, atomically: true , encoding: . utf8)
105
+ } catch {
106
+ throw ResponseError . unknown (
107
+ " Unable to write macro expansion to file path: \" \( macroExpansionFilePath. path) \" "
108
+ )
109
+ }
102
110
103
- Task {
104
- let req = ShowDocumentRequest ( uri: DocumentURI ( macroExpansionFilePath) , selection: macroEdit. range)
111
+ Task {
112
+ let req = ShowDocumentRequest ( uri: DocumentURI ( macroExpansionFilePath) , selection: macroEdit. range)
105
113
106
- let response = await orLog ( " Sending ShowDocumentRequest to Client " ) {
107
- try await sourceKitLSPServer. sendRequestToClient ( req)
108
- }
114
+ let response = await orLog ( " Sending ShowDocumentRequest to Client " ) {
115
+ try await sourceKitLSPServer. sendRequestToClient ( req)
116
+ }
109
117
110
- if let response, !response. success {
111
- logger. error ( " client refused to show document for \( expansion. title, privacy: . public) " )
118
+ if let response, !response. success {
119
+ logger. error ( " client refused to show document for \( expansion. title, privacy: . public) " )
120
+ }
112
121
}
122
+ } else if !macroEdit. newText. isEmpty {
123
+ logger. fault ( " Unable to retrieve some parts of macro expansion " )
113
124
}
114
125
}
115
126
0 commit comments