1- //! Spanmaps allow turning absolute ranges into relative ranges for incrementality purposes as well
2- //! as associating spans with text ranges in a particular file.
3-
4- // FIXME: Consider moving this into the span crate
5-
6- use base_db:: FileId ;
7- use span:: { ErasedFileAstId , Span , SpanAnchor , SyntaxContextId , ROOT_ERASED_FILE_AST_ID } ;
8- use syntax:: { ast:: HasModuleItem , AstNode , TextRange , TextSize } ;
1+ //! Span maps for real files and macro expansions.
2+ use span:: Span ;
3+ use syntax:: TextRange ;
94use triomphe:: Arc ;
105
11- use crate :: db :: ExpandDatabase ;
6+ pub use span :: RealSpanMap ;
127
138pub type ExpansionSpanMap = span:: SpanMap < Span > ;
149
@@ -39,11 +34,6 @@ impl mbe::SpanMapper<Span> for SpanMapRef<'_> {
3934 self . span_for_range ( range)
4035 }
4136}
42- impl mbe:: SpanMapper < Span > for RealSpanMap {
43- fn span_for ( & self , range : TextRange ) -> Span {
44- self . span_for_range ( range)
45- }
46- }
4737
4838impl SpanMap {
4939 pub fn span_for_range ( & self , range : TextRange ) -> Span {
@@ -69,57 +59,3 @@ impl SpanMapRef<'_> {
6959 }
7060 }
7161}
72-
73- #[ derive( PartialEq , Eq , Hash , Debug ) ]
74- pub struct RealSpanMap {
75- file_id : FileId ,
76- /// Invariant: Sorted vec over TextSize
77- // FIXME: SortedVec<(TextSize, ErasedFileAstId)>?
78- pairs : Box < [ ( TextSize , ErasedFileAstId ) ] > ,
79- end : TextSize ,
80- }
81-
82- impl RealSpanMap {
83- /// Creates a real file span map that returns absolute ranges (relative ranges to the root ast id).
84- pub fn absolute ( file_id : FileId ) -> Self {
85- RealSpanMap {
86- file_id,
87- pairs : Box :: from ( [ ( TextSize :: new ( 0 ) , ROOT_ERASED_FILE_AST_ID ) ] ) ,
88- end : TextSize :: new ( !0 ) ,
89- }
90- }
91-
92- pub fn from_file ( db : & dyn ExpandDatabase , file_id : FileId ) -> Self {
93- let mut pairs = vec ! [ ( TextSize :: new( 0 ) , ROOT_ERASED_FILE_AST_ID ) ] ;
94- let ast_id_map = db. ast_id_map ( file_id. into ( ) ) ;
95- let tree = db. parse ( file_id) . tree ( ) ;
96- pairs
97- . extend ( tree. items ( ) . map ( |item| {
98- ( item. syntax ( ) . text_range ( ) . start ( ) , ast_id_map. ast_id ( & item) . erase ( ) )
99- } ) ) ;
100- RealSpanMap {
101- file_id,
102- pairs : pairs. into_boxed_slice ( ) ,
103- end : tree. syntax ( ) . text_range ( ) . end ( ) ,
104- }
105- }
106-
107- pub fn span_for_range ( & self , range : TextRange ) -> Span {
108- assert ! (
109- range. end( ) <= self . end,
110- "range {range:?} goes beyond the end of the file {:?}" ,
111- self . end
112- ) ;
113- let start = range. start ( ) ;
114- let idx = self
115- . pairs
116- . binary_search_by ( |& ( it, _) | it. cmp ( & start) . then ( std:: cmp:: Ordering :: Less ) )
117- . unwrap_err ( ) ;
118- let ( offset, ast_id) = self . pairs [ idx - 1 ] ;
119- Span {
120- range : range - offset,
121- anchor : SpanAnchor { file_id : self . file_id , ast_id } ,
122- ctx : SyntaxContextId :: ROOT ,
123- }
124- }
125- }
0 commit comments