1
1
//! Things to wrap other things in file ids.
2
+ use std:: borrow:: Borrow ;
3
+
2
4
use either:: Either ;
3
5
use span:: {
4
6
AstIdNode , ErasedFileAstId , FileAstId , FileId , FileRange , HirFileId , HirFileIdRepr ,
@@ -76,6 +78,13 @@ impl<FileKind: Copy, T> InFileWrapper<FileKind, T> {
76
78
pub fn as_ref ( & self ) -> InFileWrapper < FileKind , & T > {
77
79
self . with_value ( & self . value )
78
80
}
81
+
82
+ pub fn borrow < U > ( & self ) -> InFileWrapper < FileKind , & U >
83
+ where
84
+ T : Borrow < U > ,
85
+ {
86
+ self . with_value ( self . value . borrow ( ) )
87
+ }
79
88
}
80
89
81
90
impl < FileKind : Copy , T : Clone > InFileWrapper < FileKind , & T > {
@@ -156,8 +165,13 @@ impl<FileId: Copy, N: AstNode> InFileWrapper<FileId, &N> {
156
165
}
157
166
158
167
// region:specific impls
168
+ impl < SN : Borrow < SyntaxNode > > InRealFile < SN > {
169
+ pub fn file_range ( & self ) -> FileRange {
170
+ FileRange { file_id : self . file_id , range : self . value . borrow ( ) . text_range ( ) }
171
+ }
172
+ }
159
173
160
- impl InFile < & SyntaxNode > {
174
+ impl < SN : Borrow < SyntaxNode > > InFile < SN > {
161
175
pub fn parent_ancestors_with_macros (
162
176
self ,
163
177
db : & dyn db:: ExpandDatabase ,
@@ -172,7 +186,7 @@ impl InFile<&SyntaxNode> {
172
186
. map ( |node| node. parent ( ) )
173
187
. transpose ( ) ,
174
188
} ;
175
- std:: iter:: successors ( succ ( & self . cloned ( ) ) , succ)
189
+ std:: iter:: successors ( succ ( & self . borrow ( ) . cloned ( ) ) , succ)
176
190
}
177
191
178
192
pub fn ancestors_with_macros (
@@ -189,31 +203,31 @@ impl InFile<&SyntaxNode> {
189
203
. map ( |node| node. parent ( ) )
190
204
. transpose ( ) ,
191
205
} ;
192
- std:: iter:: successors ( Some ( self . cloned ( ) ) , succ)
206
+ std:: iter:: successors ( Some ( self . borrow ( ) . cloned ( ) ) , succ)
207
+ }
208
+
209
+ pub fn kind ( & self ) -> parser:: SyntaxKind {
210
+ self . value . borrow ( ) . kind ( )
211
+ }
212
+
213
+ pub fn text_range ( & self ) -> TextRange {
214
+ self . value . borrow ( ) . text_range ( )
193
215
}
194
216
195
217
/// Falls back to the macro call range if the node cannot be mapped up fully.
196
218
///
197
219
/// For attributes and derives, this will point back to the attribute only.
198
220
/// For the entire item use [`InFile::original_file_range_full`].
199
221
pub fn original_file_range_rooted ( self , db : & dyn db:: ExpandDatabase ) -> FileRange {
200
- self . map ( SyntaxNode :: text_range) . original_node_file_range_rooted ( db)
222
+ self . borrow ( ) . map ( SyntaxNode :: text_range) . original_node_file_range_rooted ( db)
201
223
}
202
224
203
225
/// Falls back to the macro call range if the node cannot be mapped up fully.
204
226
pub fn original_file_range_with_macro_call_body (
205
227
self ,
206
228
db : & dyn db:: ExpandDatabase ,
207
229
) -> FileRange {
208
- self . map ( SyntaxNode :: text_range) . original_node_file_range_with_macro_call_body ( db)
209
- }
210
-
211
- /// Attempts to map the syntax node back up its macro calls.
212
- pub fn original_file_range_opt (
213
- self ,
214
- db : & dyn db:: ExpandDatabase ,
215
- ) -> Option < ( FileRange , SyntaxContextId ) > {
216
- self . map ( SyntaxNode :: text_range) . original_node_file_range_opt ( db)
230
+ self . borrow ( ) . map ( SyntaxNode :: text_range) . original_node_file_range_with_macro_call_body ( db)
217
231
}
218
232
219
233
pub fn original_syntax_node_rooted (
@@ -224,16 +238,19 @@ impl InFile<&SyntaxNode> {
224
238
// as we don't have node inputs otherwise and therefore can't find an `N` node in the input
225
239
let file_id = match self . file_id . repr ( ) {
226
240
HirFileIdRepr :: FileId ( file_id) => {
227
- return Some ( InRealFile { file_id, value : self . value . clone ( ) } )
241
+ return Some ( InRealFile { file_id, value : self . value . borrow ( ) . clone ( ) } )
228
242
}
229
243
HirFileIdRepr :: MacroFile ( m) if m. is_attr_macro ( db) => m,
230
244
_ => return None ,
231
245
} ;
232
246
233
- let FileRange { file_id, range } =
234
- map_node_range_up_rooted ( db, & db. expansion_span_map ( file_id) , self . value . text_range ( ) ) ?;
247
+ let FileRange { file_id, range } = map_node_range_up_rooted (
248
+ db,
249
+ & db. expansion_span_map ( file_id) ,
250
+ self . value . borrow ( ) . text_range ( ) ,
251
+ ) ?;
235
252
236
- let kind = self . value . kind ( ) ;
253
+ let kind = self . kind ( ) ;
237
254
let value = db
238
255
. parse ( file_id)
239
256
. syntax_node ( )
@@ -245,6 +262,16 @@ impl InFile<&SyntaxNode> {
245
262
}
246
263
}
247
264
265
+ impl InFile < & SyntaxNode > {
266
+ /// Attempts to map the syntax node back up its macro calls.
267
+ pub fn original_file_range_opt (
268
+ self ,
269
+ db : & dyn db:: ExpandDatabase ,
270
+ ) -> Option < ( FileRange , SyntaxContextId ) > {
271
+ self . borrow ( ) . map ( SyntaxNode :: text_range) . original_node_file_range_opt ( db)
272
+ }
273
+ }
274
+
248
275
impl InMacroFile < SyntaxToken > {
249
276
pub fn upmap_once (
250
277
self ,
0 commit comments