@@ -15,32 +15,34 @@ import LanguageServerProtocol
15
15
import RegexBuilder
16
16
17
17
/// Represents url of macro expansion reference document as follows:
18
- /// `sourcekit-lsp://swift-macro-expansion/LaCb-LcCd.swift?primaryFilePath=&sourceFilePath=& fromLine=&fromColumn=&toLine=&toColumn=&bufferName=`
18
+ /// `sourcekit-lsp://swift-macro-expansion/LaCb-LcCd.swift?primaryFilePath=&fromLine=&fromColumn=&toLine=&toColumn=&bufferName=&parentReferenceDocumentURL =`
19
19
///
20
20
/// Here,
21
21
/// - `LaCb-LcCd.swift`, the `displayName`, represents where the macro will expand to or
22
22
/// replace in the source file (i.e. `macroExpansionEditRange`)
23
23
/// - `primaryFilePath` denoting the URL of the source file
24
24
/// - `fromLine`, `fromColumn`, `toLine`, `toColumn` represents the cursor's `selectionRange`
25
25
/// - `bufferName` denotes the buffer name of the specific macro expansion edit
26
+ /// - `parentReferenceDocumentURL` the reference document url of the macro
27
+ /// which expands to the current macro (optional)
26
28
package struct MacroExpansionReferenceDocumentURLData {
27
29
package static let documentType = " swift-macro-expansion "
28
30
29
31
package var primaryFileURL : URL
30
- package var sourceFileURL : URL ?
32
+ package var parentReferenceDocumentURL : URL ?
31
33
package var selectionRange : Range < Position >
32
34
package var bufferName : String
33
35
package var macroExpansionEditRange : Range < Position >
34
36
35
37
package init (
36
38
macroExpansionEditRange: Range < Position > ,
37
39
primaryFileURL: URL ,
38
- sourceFileURL : URL ? ,
40
+ parentReferenceDocumentURL : URL ? ,
39
41
selectionRange: Range < Position > ,
40
42
bufferName: String
41
43
) {
42
44
self . primaryFileURL = primaryFileURL
43
- self . sourceFileURL = sourceFileURL
45
+ self . parentReferenceDocumentURL = parentReferenceDocumentURL
44
46
self . selectionRange = selectionRange
45
47
self . bufferName = bufferName
46
48
self . macroExpansionEditRange = macroExpansionEditRange
@@ -51,7 +53,7 @@ package struct MacroExpansionReferenceDocumentURLData {
51
53
}
52
54
53
55
package var queryItems : [ URLQueryItem ] {
54
- if let sourceFileURL {
56
+ if let parentReferenceDocumentURL { // usage
55
57
[
56
58
URLQueryItem ( name: Parameters . fromLine, value: String ( selectionRange. lowerBound. line) ) ,
57
59
URLQueryItem ( name: Parameters . fromColumn, value: String ( selectionRange. lowerBound. utf16index) ) ,
@@ -60,8 +62,8 @@ package struct MacroExpansionReferenceDocumentURLData {
60
62
URLQueryItem ( name: Parameters . bufferName, value: bufferName) ,
61
63
URLQueryItem ( name: Parameters . primaryFilePath, value: primaryFileURL. path ( percentEncoded: false ) ) ,
62
64
URLQueryItem (
63
- name: Parameters . sourceFilePath ,
64
- value: sourceFileURL . absoluteString. addingPercentEncoding ( withAllowedCharacters: . alphanumerics)
65
+ name: Parameters . parentReferenceDocumentURL ,
66
+ value: parentReferenceDocumentURL . absoluteString. addingPercentEncoding ( withAllowedCharacters: . alphanumerics)
65
67
) ,
66
68
]
67
69
} else {
@@ -93,62 +95,30 @@ package struct MacroExpansionReferenceDocumentURLData {
93
95
)
94
96
}
95
97
96
- var sourceFileURL : URL ? = nil
97
- if let sourceFilePath = queryItems. last ( where: { $0. name == Parameters . sourceFilePath } ) ? . value?
98
- . removingPercentEncoding
99
- {
100
- guard let url = URL ( string: sourceFilePath) else {
98
+ var parentReferenceDocumentURL : URL ? = nil
99
+ if let parentReferenceDocumentURLString = queryItems. last ( where: {
100
+ $0. name == Parameters . parentReferenceDocumentURL
101
+ } ) ? . value?
102
+ . removingPercentEncoding {
103
+ guard let url = URL ( string: parentReferenceDocumentURLString) else {
101
104
throw ReferenceDocumentURLError (
102
105
description: " Unable to parse source file url "
103
106
)
104
107
}
105
108
106
- sourceFileURL = url
109
+ parentReferenceDocumentURL = url
107
110
}
108
111
109
112
self . primaryFileURL = primaryFileURL
110
- self . sourceFileURL = sourceFileURL
113
+ self . parentReferenceDocumentURL = parentReferenceDocumentURL
111
114
self . selectionRange =
112
115
Position ( line: fromLine, utf16index: fromColumn) ..< Position ( line: toLine, utf16index: toColumn)
113
116
self . bufferName = bufferName
114
117
self . macroExpansionEditRange = try Self . parse ( displayName: displayName)
115
118
}
116
119
117
- package var actualFile : DocumentURI {
118
- get throws {
119
- guard let uri = try ? DocumentURI ( string: bufferName) else {
120
- throw ReferenceDocumentURLError (
121
- description: " Unable to retrieve actual file uri of macro expansion reference document "
122
- )
123
- }
124
-
125
- return uri
126
- }
127
- }
128
-
129
- package var sourceFile : DocumentURI {
130
- get throws {
131
- var uri : DocumentURI ?
132
- if let sourceFileURL {
133
- let referenceDocumentURL = try ReferenceDocumentURL ( from: sourceFileURL)
134
- guard case let . macroExpansion( urlData) = referenceDocumentURL else {
135
- throw ReferenceDocumentURLError (
136
- description: " Unable to retrieve buffer name from source file "
137
- )
138
- }
139
- uri = try ? DocumentURI ( string: urlData. bufferName)
140
- } else {
141
- uri = try ? DocumentURI ( string: bufferName)
142
- }
143
-
144
- guard let uri else {
145
- throw ReferenceDocumentURLError (
146
- description: " Unable to retrieve source file uri of macro expansion reference document "
147
- )
148
- }
149
-
150
- return uri
151
- }
120
+ package var actualFilePath : String {
121
+ bufferName
152
122
}
153
123
154
124
package var primaryFile : DocumentURI {
@@ -157,7 +127,7 @@ package struct MacroExpansionReferenceDocumentURLData {
157
127
158
128
private struct Parameters {
159
129
static let primaryFilePath = " primaryFilePath "
160
- static let sourceFilePath = " sourceFilePath "
130
+ static let parentReferenceDocumentURL = " parentReferenceDocumentURL "
161
131
static let fromLine = " fromLine "
162
132
static let fromColumn = " fromColumn "
163
133
static let toLine = " toLine "
@@ -173,8 +143,8 @@ package struct MacroExpansionReferenceDocumentURLData {
173
143
}
174
144
175
145
var result = URL ( string: urlWithoutEncoding)
176
- if urlWithoutEncoding. contains ( Parameters . sourceFilePath ) {
177
- let location = urlWithoutEncoding. firstRange ( of: " & \( Parameters . sourceFilePath ) = " )
146
+ if urlWithoutEncoding. contains ( Parameters . parentReferenceDocumentURL ) {
147
+ let location = urlWithoutEncoding. firstRange ( of: " & \( Parameters . parentReferenceDocumentURL ) = " )
178
148
guard let location else {
179
149
return nil
180
150
}
@@ -184,7 +154,7 @@ package struct MacroExpansionReferenceDocumentURLData {
184
154
let range = start..< end
185
155
186
156
guard
187
- let sourceFileURL = String ( urlWithoutEncoding [ range] ) . addingPercentEncoding (
157
+ let parentReferenceDocumentURL = String ( urlWithoutEncoding [ range] ) . addingPercentEncoding (
188
158
withAllowedCharacters: . alphanumerics
189
159
)
190
160
else {
@@ -195,9 +165,10 @@ package struct MacroExpansionReferenceDocumentURLData {
195
165
let firstEnd = location. lowerBound
196
166
let firstRange = firstStart..< firstEnd
197
167
198
- let urlExceptSourceFileURL = String ( urlWithoutEncoding [ firstRange] )
168
+ let urlExceptParentReferenceDocumentURL = String ( urlWithoutEncoding [ firstRange] )
199
169
200
- let finalURLString = urlExceptSourceFileURL + " & \( Parameters . sourceFilePath) = " + sourceFileURL
170
+ let finalURLString =
171
+ urlExceptParentReferenceDocumentURL + " & \( Parameters . parentReferenceDocumentURL) = " + parentReferenceDocumentURL
201
172
result = URL ( string: finalURLString)
202
173
}
203
174
0 commit comments