@@ -6,9 +6,9 @@ use base_db::{
6
6
FileId , FileRange ,
7
7
} ;
8
8
use either:: Either ;
9
- use syntax:: { AstNode , SyntaxNode , SyntaxToken , TextRange } ;
9
+ use syntax:: { AstNode , SyntaxNode , SyntaxToken , TextRange , TextSize } ;
10
10
11
- use crate :: { db, ExpansionInfo , HirFileIdExt as _ } ;
11
+ use crate :: { db, ExpansionInfo , MacroFileIdExt } ;
12
12
13
13
/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
14
14
///
@@ -119,16 +119,6 @@ impl<FileId: Copy, N: AstNode> InFileWrapper<FileId, N> {
119
119
// region:specific impls
120
120
121
121
impl 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
-
132
122
/// Skips the attributed item that caused the macro invocation we are climbing up
133
123
pub fn ancestors_with_macros_skip_attr_item (
134
124
self ,
@@ -137,8 +127,9 @@ impl InFile<&SyntaxNode> {
137
127
let succ = move |node : & InFile < SyntaxNode > | match node. value . parent ( ) {
138
128
Some ( parent) => Some ( node. with_value ( parent) ) ,
139
129
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) {
142
133
// macro call was an attributed item, skip it
143
134
// FIXME: does this fail if this is a direct expansion of another macro?
144
135
parent_node. map ( |node| node. parent ( ) ) . transpose ( )
@@ -222,7 +213,7 @@ impl InFile<&SyntaxNode> {
222
213
}
223
214
HirFileIdRepr :: MacroFile ( m) => m,
224
215
} ;
225
- if !self . file_id . is_attr_macro ( db) {
216
+ if !file_id. is_attr_macro ( db) {
226
217
return None ;
227
218
}
228
219
@@ -243,21 +234,23 @@ impl InFile<&SyntaxNode> {
243
234
}
244
235
}
245
236
246
- impl InFile < SyntaxToken > {
237
+ impl InMacroFile < SyntaxToken > {
247
238
pub fn upmap_once (
248
239
self ,
249
240
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 ( ) )
252
243
}
244
+ }
253
245
246
+ impl InFile < SyntaxToken > {
254
247
/// Falls back to the macro call range if the node cannot be mapped up fully.
255
248
pub fn original_file_range ( self , db : & dyn db:: ExpandDatabase ) -> FileRange {
256
249
match self . file_id . repr ( ) {
257
250
HirFileIdRepr :: FileId ( file_id) => FileRange { file_id, range : self . value . text_range ( ) } ,
258
251
HirFileIdRepr :: MacroFile ( mac_file) => {
259
252
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 ( ) ) ;
261
254
262
255
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
263
256
// keep pre-token map rewrite behaviour.
@@ -280,7 +273,7 @@ impl InFile<SyntaxToken> {
280
273
}
281
274
HirFileIdRepr :: MacroFile ( mac_file) => {
282
275
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 ( ) ) ;
284
277
285
278
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
286
279
// keep pre-token map rewrite behaviour.
@@ -294,20 +287,13 @@ impl InFile<SyntaxToken> {
294
287
}
295
288
}
296
289
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 )
309
293
}
294
+ }
310
295
296
+ impl InFile < TextRange > {
311
297
pub fn original_node_file_range (
312
298
self ,
313
299
db : & dyn db:: ExpandDatabase ,
@@ -353,7 +339,7 @@ impl<N: AstNode> InFile<N> {
353
339
}
354
340
HirFileIdRepr :: MacroFile ( m) => m,
355
341
} ;
356
- if !self . file_id . is_attr_macro ( db) {
342
+ if !file_id. is_attr_macro ( db) {
357
343
return None ;
358
344
}
359
345
0 commit comments