@@ -15,15 +15,16 @@ import LanguageServerProtocol
1515import RegexBuilder
1616
1717/// 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 =`
1919///
2020/// Here,
2121/// - `LaCb-LcCd.swift`, the `displayName`, represents where the macro will expand to or
2222/// 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
2326/// - `parent` denoting the URI of the document from which the macro was expanded. For a first-level macro expansion,
2427/// 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
2728package struct MacroExpansionReferenceDocumentURLData {
2829 package static let documentType = " swift-macro-expansion "
2930
@@ -33,7 +34,7 @@ package struct MacroExpansionReferenceDocumentURLData {
3334 package var parent : DocumentURI
3435
3536 /// The range that was selected in `parent` when the macro was expanded.
36- package var selectionRange : Range < Position >
37+ package var parentSelectionRange : Range < Position >
3738
3839 /// ## Example
3940 ///
@@ -47,7 +48,7 @@ package struct MacroExpansionReferenceDocumentURLData {
4748 ///
4849 /// Generated content of reference document url:
4950 /// 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`
5152 /// ```swift
5253 /// (a + b, "a + b")
5354 /// ```
@@ -64,12 +65,12 @@ package struct MacroExpansionReferenceDocumentURLData {
6465 package init (
6566 macroExpansionEditRange: Range < Position > ,
6667 parent: DocumentURI ,
67- selectionRange : Range < Position > ,
68+ parentSelectionRange : Range < Position > ,
6869 bufferName: String
6970 ) {
7071 self . macroExpansionEditRange = macroExpansionEditRange
7172 self . parent = parent
72- self . selectionRange = selectionRange
73+ self . parentSelectionRange = parentSelectionRange
7374 self . bufferName = bufferName
7475 }
7576
@@ -78,13 +79,17 @@ package struct MacroExpansionReferenceDocumentURLData {
7879 }
7980
8081 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) ) ,
8787 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) ,
8893 ]
8994 }
9095
@@ -100,7 +105,7 @@ package struct MacroExpansionReferenceDocumentURLData {
100105 }
101106
102107 self . parent = try DocumentURI ( string: parent)
103- self . selectionRange =
108+ self . parentSelectionRange =
104109 Position ( line: fromLine, utf16index: fromColumn) ..< Position ( line: toLine, utf16index: toColumn)
105110 self . bufferName = bufferName
106111 self . macroExpansionEditRange = try Self . parse ( displayName: displayName)
@@ -121,7 +126,7 @@ package struct MacroExpansionReferenceDocumentURLData {
121126 ///
122127 /// Generated content of reference document url:
123128 /// 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`
125130 /// ```swift
126131 /// (a + b, "a + b")
127132 /// ```
@@ -134,9 +139,18 @@ package struct MacroExpansionReferenceDocumentURLData {
134139 package var primaryFile : DocumentURI {
135140 switch try ? ReferenceDocumentURL ( from: parent) {
136141 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
138152 case nil :
139- return parent
153+ self . parentSelectionRange
140154 }
141155 }
142156
0 commit comments