@@ -15,15 +15,16 @@ 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?parent=& fromLine=&fromColumn=&toLine=&toColumn=&bufferName=`
18
+ /// `sourcekit-lsp://swift-macro-expansion/LaCb-LcCd.swift?fromLine=&fromColumn=&toLine=&toColumn=&bufferName=&parent =`
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
+ /// - `fromLine`, `fromColumn`, `toLine`, `toColumn` represents the cursor's selection range in
24
+ /// its `parent` (i.e. `parentSelectionRange`)
25
+ /// - `bufferName` denotes the buffer name of the specific macro expansion edit
23
26
/// - `parent` denoting the URI of the document from which the macro was expanded. For a first-level macro expansion,
24
27
/// this is a file URI. For nested macro expansions, this is a `sourcekit-lsp://swift-macro-expansion` URL.
25
- /// - `fromLine`, `fromColumn`, `toLine`, `toColumn` represents the cursor's `selectionRange`
26
- /// - `bufferName` denotes the buffer name of the specific macro expansion edit
27
28
package struct MacroExpansionReferenceDocumentURLData {
28
29
package static let documentType = " swift-macro-expansion "
29
30
@@ -33,7 +34,7 @@ package struct MacroExpansionReferenceDocumentURLData {
33
34
package var parent : DocumentURI
34
35
35
36
/// The range that was selected in `parent` when the macro was expanded.
36
- package var selectionRange : Range < Position >
37
+ package var parentSelectionRange : Range < Position >
37
38
38
39
/// ## Example
39
40
///
@@ -47,7 +48,7 @@ package struct MacroExpansionReferenceDocumentURLData {
47
48
///
48
49
/// Generated content of reference document url:
49
50
/// URL:
50
- /// `sourcekit-lsp://swift-macro-expansion/L3C7-L3C23.swift?primaryFilePath=/path/to/swift_file.swift& fromLine=3&fromColumn=8&toLine=3&toColumn=8&bufferName=@__swift_macro_..._Stringify_.swift`
51
+ /// `sourcekit-lsp://swift-macro-expansion/L3C7-L3C23.swift?fromLine=3&fromColumn=8&toLine=3&toColumn=8&bufferName=@__swift_macro_..._Stringify_.swift&parent=/path/to/swift_file .swift`
51
52
/// ```swift
52
53
/// (a + b, "a + b")
53
54
/// ```
@@ -64,12 +65,12 @@ package struct MacroExpansionReferenceDocumentURLData {
64
65
package init (
65
66
macroExpansionEditRange: Range < Position > ,
66
67
parent: DocumentURI ,
67
- selectionRange : Range < Position > ,
68
+ parentSelectionRange : Range < Position > ,
68
69
bufferName: String
69
70
) {
70
71
self . macroExpansionEditRange = macroExpansionEditRange
71
72
self . parent = parent
72
- self . selectionRange = selectionRange
73
+ self . parentSelectionRange = parentSelectionRange
73
74
self . bufferName = bufferName
74
75
}
75
76
@@ -78,13 +79,17 @@ package struct MacroExpansionReferenceDocumentURLData {
78
79
}
79
80
80
81
package var queryItems : [ URLQueryItem ] {
81
- return [
82
- URLQueryItem ( name: Parameters . parent, value: parent. stringValue) ,
83
- URLQueryItem ( name: Parameters . fromLine, value: String ( selectionRange. lowerBound. line) ) ,
84
- URLQueryItem ( name: Parameters . fromColumn, value: String ( selectionRange. lowerBound. utf16index) ) ,
85
- URLQueryItem ( name: Parameters . toLine, value: String ( selectionRange. upperBound. line) ) ,
86
- URLQueryItem ( name: Parameters . toColumn, value: String ( selectionRange. upperBound. utf16index) ) ,
82
+ [
83
+ URLQueryItem ( name: Parameters . fromLine, value: String ( parentSelectionRange. lowerBound. line) ) ,
84
+ URLQueryItem ( name: Parameters . fromColumn, value: String ( parentSelectionRange. lowerBound. utf16index) ) ,
85
+ URLQueryItem ( name: Parameters . toLine, value: String ( parentSelectionRange. upperBound. line) ) ,
86
+ URLQueryItem ( name: Parameters . toColumn, value: String ( parentSelectionRange. upperBound. utf16index) ) ,
87
87
URLQueryItem ( name: Parameters . bufferName, value: bufferName) ,
88
+
89
+ // *Note*: Having `parent` as the last parameter will ensure that the url's parameters aren't mistaken to be its
90
+ // `parent`'s parameters in certain environments where percent encoding gets removed or added
91
+ // unnecessarily (for example: VS Code)
92
+ URLQueryItem ( name: Parameters . parent, value: parent. stringValue) ,
88
93
]
89
94
}
90
95
@@ -100,7 +105,7 @@ package struct MacroExpansionReferenceDocumentURLData {
100
105
}
101
106
102
107
self . parent = try DocumentURI ( string: parent)
103
- self . selectionRange =
108
+ self . parentSelectionRange =
104
109
Position ( line: fromLine, utf16index: fromColumn) ..< Position ( line: toLine, utf16index: toColumn)
105
110
self . bufferName = bufferName
106
111
self . macroExpansionEditRange = try Self . parse ( displayName: displayName)
@@ -121,7 +126,7 @@ package struct MacroExpansionReferenceDocumentURLData {
121
126
///
122
127
/// Generated content of reference document url:
123
128
/// URL:
124
- /// `sourcekit-lsp://swift-macro-expansion/L3C7-L3C23.swift?primaryFilePath=/path/to/swift_file.swift& fromLine=3&fromColumn=8&toLine=3&toColumn=8&bufferName=@__swift_macro_..._Stringify_.swift`
129
+ /// `sourcekit-lsp://swift-macro-expansion/L3C7-L3C23.swift?fromLine=3&fromColumn=8&toLine=3&toColumn=8&bufferName=@__swift_macro_..._Stringify_.swift&parent=/path/to/swift_file .swift`
125
130
/// ```swift
126
131
/// (a + b, "a + b")
127
132
/// ```
@@ -134,9 +139,18 @@ package struct MacroExpansionReferenceDocumentURLData {
134
139
package var primaryFile : DocumentURI {
135
140
switch try ? ReferenceDocumentURL ( from: parent) {
136
141
case . macroExpansion( let data) :
137
- return data. primaryFile
142
+ data. primaryFile
143
+ case nil :
144
+ parent
145
+ }
146
+ }
147
+
148
+ package var primaryFileSelectionRange : Range < Position > {
149
+ switch try ? ReferenceDocumentURL ( from: parent) {
150
+ case . macroExpansion( let data) :
151
+ data. primaryFileSelectionRange
138
152
case nil :
139
- return parent
153
+ self . parentSelectionRange
140
154
}
141
155
}
142
156
0 commit comments