@@ -6,9 +6,9 @@ use base_db::{
66 FileId , FileRange ,
77} ;
88use either:: Either ;
9- use syntax:: { AstNode , SyntaxNode , SyntaxToken , TextRange } ;
9+ use syntax:: { AstNode , SyntaxNode , SyntaxToken , TextRange , TextSize } ;
1010
11- use crate :: { db, ExpansionInfo , HirFileIdExt as _ } ;
11+ use crate :: { db, ExpansionInfo , MacroFileIdExt } ;
1212
1313/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
1414///
@@ -119,16 +119,6 @@ impl<FileId: Copy, N: AstNode> InFileWrapper<FileId, N> {
119119// region:specific impls
120120
121121impl InFile < & SyntaxNode > {
122- pub fn ancestors_with_macros (
123- self ,
124- db : & dyn db:: ExpandDatabase ,
125- ) -> impl Iterator < Item = InFile < SyntaxNode > > + Clone + ' _ {
126- iter:: successors ( Some ( self . cloned ( ) ) , move |node| match node. value . parent ( ) {
127- Some ( parent) => Some ( node. with_value ( parent) ) ,
128- None => node. file_id . call_node ( db) ,
129- } )
130- }
131-
132122 /// Skips the attributed item that caused the macro invocation we are climbing up
133123 pub fn ancestors_with_macros_skip_attr_item (
134124 self ,
@@ -137,8 +127,9 @@ impl InFile<&SyntaxNode> {
137127 let succ = move |node : & InFile < SyntaxNode > | match node. value . parent ( ) {
138128 Some ( parent) => Some ( node. with_value ( parent) ) ,
139129 None => {
140- let parent_node = node. file_id . call_node ( db) ?;
141- if node. file_id . is_attr_macro ( db) {
130+ let macro_file_id = node. file_id . macro_file ( ) ?;
131+ let parent_node = macro_file_id. call_node ( db) ;
132+ if macro_file_id. is_attr_macro ( db) {
142133 // macro call was an attributed item, skip it
143134 // FIXME: does this fail if this is a direct expansion of another macro?
144135 parent_node. map ( |node| node. parent ( ) ) . transpose ( )
@@ -222,7 +213,7 @@ impl InFile<&SyntaxNode> {
222213 }
223214 HirFileIdRepr :: MacroFile ( m) => m,
224215 } ;
225- if !self . file_id . is_attr_macro ( db) {
216+ if !file_id. is_attr_macro ( db) {
226217 return None ;
227218 }
228219
@@ -243,21 +234,23 @@ impl InFile<&SyntaxNode> {
243234 }
244235}
245236
246- impl InFile < SyntaxToken > {
237+ impl InMacroFile < SyntaxToken > {
247238 pub fn upmap_once (
248239 self ,
249240 db : & dyn db:: ExpandDatabase ,
250- ) -> Option < InFile < smallvec:: SmallVec < [ TextRange ; 1 ] > > > {
251- Some ( self . file_id . expansion_info ( db) ? . map_range_up_once ( db, self . value . text_range ( ) ) )
241+ ) -> InFile < smallvec:: SmallVec < [ TextRange ; 1 ] > > {
242+ self . file_id . expansion_info ( db) . map_range_up_once ( db, self . value . text_range ( ) )
252243 }
244+ }
253245
246+ impl InFile < SyntaxToken > {
254247 /// Falls back to the macro call range if the node cannot be mapped up fully.
255248 pub fn original_file_range ( self , db : & dyn db:: ExpandDatabase ) -> FileRange {
256249 match self . file_id . repr ( ) {
257250 HirFileIdRepr :: FileId ( file_id) => FileRange { file_id, range : self . value . text_range ( ) } ,
258251 HirFileIdRepr :: MacroFile ( mac_file) => {
259252 let ( range, ctxt) = ExpansionInfo :: new ( db, mac_file)
260- . map_token_range_up ( db, self . value . text_range ( ) ) ;
253+ . span_for_offset ( db, self . value . text_range ( ) . start ( ) ) ;
261254
262255 // FIXME: Figure out an API that makes proper use of ctx, this only exists to
263256 // keep pre-token map rewrite behaviour.
@@ -280,7 +273,7 @@ impl InFile<SyntaxToken> {
280273 }
281274 HirFileIdRepr :: MacroFile ( mac_file) => {
282275 let ( range, ctxt) = ExpansionInfo :: new ( db, mac_file)
283- . map_token_range_up ( db, self . value . text_range ( ) ) ;
276+ . span_for_offset ( db, self . value . text_range ( ) . start ( ) ) ;
284277
285278 // FIXME: Figure out an API that makes proper use of ctx, this only exists to
286279 // keep pre-token map rewrite behaviour.
@@ -294,20 +287,13 @@ impl InFile<SyntaxToken> {
294287 }
295288}
296289
297- impl InFile < TextRange > {
298- /// Attempts to map the syntax node back up its macro calls.
299- pub fn original_file_range ( self , db : & dyn db:: ExpandDatabase ) -> FileRange {
300- let ( range, _ctxt) = match self . file_id . repr ( ) {
301- HirFileIdRepr :: FileId ( file_id) => {
302- ( FileRange { file_id, range : self . value } , SyntaxContextId :: ROOT )
303- }
304- HirFileIdRepr :: MacroFile ( m) => {
305- ExpansionInfo :: new ( db, m) . map_token_range_up ( db, self . value )
306- }
307- } ;
308- range
290+ impl InMacroFile < TextSize > {
291+ pub fn original_file_range ( self , db : & dyn db:: ExpandDatabase ) -> ( FileRange , SyntaxContextId ) {
292+ ExpansionInfo :: new ( db, self . file_id ) . span_for_offset ( db, self . value )
309293 }
294+ }
310295
296+ impl InFile < TextRange > {
311297 pub fn original_node_file_range (
312298 self ,
313299 db : & dyn db:: ExpandDatabase ,
@@ -353,7 +339,7 @@ impl<N: AstNode> InFile<N> {
353339 }
354340 HirFileIdRepr :: MacroFile ( m) => m,
355341 } ;
356- if !self . file_id . is_attr_macro ( db) {
342+ if !file_id. is_attr_macro ( db) {
357343 return None ;
358344 }
359345
0 commit comments