11//! Things to wrap other things in file ids.
2+ use std:: borrow:: Borrow ;
3+
24use either:: Either ;
35use span:: {
46 AstIdNode , ErasedFileAstId , FileAstId , FileId , FileRange , HirFileId , HirFileIdRepr ,
@@ -76,6 +78,13 @@ impl<FileKind: Copy, T> InFileWrapper<FileKind, T> {
7678 pub fn as_ref ( & self ) -> InFileWrapper < FileKind , & T > {
7779 self . with_value ( & self . value )
7880 }
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+ }
7988}
8089
8190impl < FileKind : Copy , T : Clone > InFileWrapper < FileKind , & T > {
@@ -156,8 +165,13 @@ impl<FileId: Copy, N: AstNode> InFileWrapper<FileId, &N> {
156165}
157166
158167// 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+ }
159173
160- impl InFile < & SyntaxNode > {
174+ impl < SN : Borrow < SyntaxNode > > InFile < SN > {
161175 pub fn parent_ancestors_with_macros (
162176 self ,
163177 db : & dyn db:: ExpandDatabase ,
@@ -172,7 +186,7 @@ impl InFile<&SyntaxNode> {
172186 . map ( |node| node. parent ( ) )
173187 . transpose ( ) ,
174188 } ;
175- std:: iter:: successors ( succ ( & self . cloned ( ) ) , succ)
189+ std:: iter:: successors ( succ ( & self . borrow ( ) . cloned ( ) ) , succ)
176190 }
177191
178192 pub fn ancestors_with_macros (
@@ -189,31 +203,31 @@ impl InFile<&SyntaxNode> {
189203 . map ( |node| node. parent ( ) )
190204 . transpose ( ) ,
191205 } ;
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 ( )
193215 }
194216
195217 /// Falls back to the macro call range if the node cannot be mapped up fully.
196218 ///
197219 /// For attributes and derives, this will point back to the attribute only.
198220 /// For the entire item use [`InFile::original_file_range_full`].
199221 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)
201223 }
202224
203225 /// Falls back to the macro call range if the node cannot be mapped up fully.
204226 pub fn original_file_range_with_macro_call_body (
205227 self ,
206228 db : & dyn db:: ExpandDatabase ,
207229 ) -> 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)
217231 }
218232
219233 pub fn original_syntax_node_rooted (
@@ -224,16 +238,19 @@ impl InFile<&SyntaxNode> {
224238 // as we don't have node inputs otherwise and therefore can't find an `N` node in the input
225239 let file_id = match self . file_id . repr ( ) {
226240 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 ( ) } )
228242 }
229243 HirFileIdRepr :: MacroFile ( m) if m. is_attr_macro ( db) => m,
230244 _ => return None ,
231245 } ;
232246
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+ ) ?;
235252
236- let kind = self . value . kind ( ) ;
253+ let kind = self . kind ( ) ;
237254 let value = db
238255 . parse ( file_id)
239256 . syntax_node ( )
@@ -245,6 +262,16 @@ impl InFile<&SyntaxNode> {
245262 }
246263}
247264
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+
248275impl InMacroFile < SyntaxToken > {
249276 pub fn upmap_once (
250277 self ,
0 commit comments