@@ -778,36 +778,19 @@ impl<'a> InFile<&'a SyntaxNode> {
778
778
/// For attributes and derives, this will point back to the attribute only.
779
779
/// For the entire item `InFile::use original_file_range_full`.
780
780
pub fn original_file_range ( self , db : & dyn db:: AstDatabase ) -> FileRange {
781
- if let Some ( res) = self . original_file_range_opt ( db) {
782
- return res;
783
- }
784
-
785
- // Fall back to whole macro call.
786
781
match self . file_id . 0 {
787
782
HirFileIdRepr :: FileId ( file_id) => FileRange { file_id, range : self . value . text_range ( ) } ,
788
783
HirFileIdRepr :: MacroFile ( mac_file) => {
784
+ if let Some ( res) = self . original_file_range_opt ( db) {
785
+ return res;
786
+ }
787
+ // Fall back to whole macro call.
789
788
let loc = db. lookup_intern_macro_call ( mac_file. macro_call_id ) ;
790
789
loc. kind . original_call_range ( db)
791
790
}
792
791
}
793
792
}
794
793
795
- /// Falls back to the macro call range if the node cannot be mapped up fully.
796
- pub fn original_file_range_full ( self , db : & dyn db:: AstDatabase ) -> FileRange {
797
- if let Some ( res) = self . original_file_range_opt ( db) {
798
- return res;
799
- }
800
-
801
- // Fall back to whole macro call.
802
- match self . file_id . 0 {
803
- HirFileIdRepr :: FileId ( file_id) => FileRange { file_id, range : self . value . text_range ( ) } ,
804
- HirFileIdRepr :: MacroFile ( mac_file) => {
805
- let loc = db. lookup_intern_macro_call ( mac_file. macro_call_id ) ;
806
- loc. kind . original_call_range_with_body ( db)
807
- }
808
- }
809
- }
810
-
811
794
/// Attempts to map the syntax node back up its macro calls.
812
795
pub fn original_file_range_opt ( self , db : & dyn db:: AstDatabase ) -> Option < FileRange > {
813
796
match ascend_node_border_tokens ( db, self ) {
@@ -834,6 +817,49 @@ impl InFile<SyntaxToken> {
834
817
let expansion = self . file_id . expansion_info ( db) ?;
835
818
expansion. map_token_up ( db, self . as_ref ( ) ) . map ( |( it, _) | it)
836
819
}
820
+
821
+ /// Falls back to the macro call range if the node cannot be mapped up fully.
822
+ pub fn original_file_range ( self , db : & dyn db:: AstDatabase ) -> FileRange {
823
+ match self . file_id . 0 {
824
+ HirFileIdRepr :: FileId ( file_id) => FileRange { file_id, range : self . value . text_range ( ) } ,
825
+ HirFileIdRepr :: MacroFile ( mac_file) => {
826
+ if let Some ( res) = self . original_file_range_opt ( db) {
827
+ return res;
828
+ }
829
+ // Fall back to whole macro call.
830
+ let loc = db. lookup_intern_macro_call ( mac_file. macro_call_id ) ;
831
+ loc. kind . original_call_range ( db)
832
+ }
833
+ }
834
+ }
835
+
836
+ /// Attempts to map the syntax node back up its macro calls.
837
+ pub fn original_file_range_opt ( self , db : & dyn db:: AstDatabase ) -> Option < FileRange > {
838
+ match self . file_id . 0 {
839
+ HirFileIdRepr :: FileId ( file_id) => {
840
+ Some ( FileRange { file_id, range : self . value . text_range ( ) } )
841
+ }
842
+ HirFileIdRepr :: MacroFile ( _) => {
843
+ let expansion = self . file_id . expansion_info ( db) ?;
844
+ let InFile { file_id, value } = ascend_call_token ( db, & expansion, self ) ?;
845
+ let original_file = file_id. original_file ( db) ;
846
+ if file_id != original_file. into ( ) {
847
+ return None ;
848
+ }
849
+ Some ( FileRange { file_id : original_file, range : value. text_range ( ) } )
850
+ }
851
+ }
852
+ }
853
+
854
+ pub fn ancestors_with_macros (
855
+ self ,
856
+ db : & dyn db:: AstDatabase ,
857
+ ) -> impl Iterator < Item = InFile < SyntaxNode > > + ' _ {
858
+ self . value . parent ( ) . into_iter ( ) . flat_map ( {
859
+ let file_id = self . file_id ;
860
+ move |parent| InFile :: new ( file_id, & parent) . ancestors_with_macros ( db)
861
+ } )
862
+ }
837
863
}
838
864
839
865
fn ascend_node_border_tokens (
@@ -867,18 +893,6 @@ fn ascend_call_token(
867
893
None
868
894
}
869
895
870
- impl InFile < SyntaxToken > {
871
- pub fn ancestors_with_macros (
872
- self ,
873
- db : & dyn db:: AstDatabase ,
874
- ) -> impl Iterator < Item = InFile < SyntaxNode > > + ' _ {
875
- self . value . parent ( ) . into_iter ( ) . flat_map ( {
876
- let file_id = self . file_id ;
877
- move |parent| InFile :: new ( file_id, & parent) . ancestors_with_macros ( db)
878
- } )
879
- }
880
- }
881
-
882
896
impl < N : AstNode > InFile < N > {
883
897
pub fn descendants < T : AstNode > ( self ) -> impl Iterator < Item = InFile < T > > {
884
898
self . value . syntax ( ) . descendants ( ) . filter_map ( T :: cast) . map ( move |n| self . with_value ( n) )
0 commit comments