11//! base_db defines basic database traits. The concrete DB is defined by ide.
2-
2+ // FIXME: Rename this crate, base db is non descriptive
33mod change;
44mod input;
55
@@ -47,8 +47,6 @@ pub const DEFAULT_PARSE_LRU_CAP: u16 = 128;
4747pub const DEFAULT_BORROWCK_LRU_CAP : u16 = 2024 ;
4848
4949pub trait FileLoader {
50- /// Text of the file.
51- fn file_text ( & self , file_id : FileId ) -> Arc < str > ;
5250 fn resolve_path ( & self , path : AnchoredPath < ' _ > ) -> Option < FileId > ;
5351 /// Crates whose root's source root is the same as the source root of `file_id`
5452 fn relevant_crates ( & self , file_id : FileId ) -> Arc < [ CrateId ] > ;
@@ -58,6 +56,13 @@ pub trait FileLoader {
5856/// model. Everything else in rust-analyzer is derived from these queries.
5957#[ salsa:: query_group( SourceDatabaseStorage ) ]
6058pub trait SourceDatabase : FileLoader + std:: fmt:: Debug {
59+ #[ salsa:: input]
60+ fn compressed_file_text ( & self , file_id : FileId ) -> Arc < [ u8 ] > ;
61+
62+ /// Text of the file.
63+ #[ salsa:: lru]
64+ fn file_text ( & self , file_id : FileId ) -> Arc < str > ;
65+
6166 /// Parses the file into the syntax tree.
6267 #[ salsa:: lru]
6368 fn parse ( & self , file_id : EditionedFileId ) -> Parse < ast:: SourceFile > ;
@@ -99,16 +104,18 @@ fn parse_errors(db: &dyn SourceDatabase, file_id: EditionedFileId) -> Option<Arc
99104 }
100105}
101106
107+ fn file_text ( db : & dyn SourceDatabase , file_id : FileId ) -> Arc < str > {
108+ let bytes = db. compressed_file_text ( file_id) ;
109+ let bytes =
110+ lz4_flex:: decompress_size_prepended ( & bytes) . expect ( "lz4 decompression should not fail" ) ;
111+ let text = std:: str:: from_utf8 ( & bytes) . expect ( "file contents should be valid UTF-8" ) ;
112+ Arc :: from ( text)
113+ }
114+
102115/// We don't want to give HIR knowledge of source roots, hence we extract these
103116/// methods into a separate DB.
104- #[ salsa:: query_group( SourceDatabaseExtStorage ) ]
105- pub trait SourceDatabaseExt : SourceDatabase {
106- #[ salsa:: input]
107- fn compressed_file_text ( & self , file_id : FileId ) -> Arc < [ u8 ] > ;
108-
109- #[ salsa:: lru]
110- fn file_text ( & self , file_id : FileId ) -> Arc < str > ;
111-
117+ #[ salsa:: query_group( SourceRootDatabaseStorage ) ]
118+ pub trait SourceRootDatabase : SourceDatabase {
112119 /// Path to a file, relative to the root of its source root.
113120 /// Source root of the file.
114121 #[ salsa:: input]
@@ -121,15 +128,7 @@ pub trait SourceDatabaseExt: SourceDatabase {
121128 fn source_root_crates ( & self , id : SourceRootId ) -> Arc < [ CrateId ] > ;
122129}
123130
124- fn file_text ( db : & dyn SourceDatabaseExt , file_id : FileId ) -> Arc < str > {
125- let bytes = db. compressed_file_text ( file_id) ;
126- let bytes =
127- lz4_flex:: decompress_size_prepended ( & bytes) . expect ( "lz4 decompression should not fail" ) ;
128- let text = std:: str:: from_utf8 ( & bytes) . expect ( "file contents should be valid UTF-8" ) ;
129- Arc :: from ( text)
130- }
131-
132- pub trait SourceDatabaseExt2 {
131+ pub trait SourceDatabaseFileInputExt {
133132 fn set_file_text ( & mut self , file_id : FileId , text : & str ) {
134133 self . set_file_text_with_durability ( file_id, text, Durability :: LOW ) ;
135134 }
@@ -142,7 +141,7 @@ pub trait SourceDatabaseExt2 {
142141 ) ;
143142}
144143
145- impl < Db : ?Sized + SourceDatabaseExt > SourceDatabaseExt2 for Db {
144+ impl < Db : ?Sized + SourceRootDatabase > SourceDatabaseFileInputExt for Db {
146145 fn set_file_text_with_durability (
147146 & mut self ,
148147 file_id : FileId ,
@@ -159,7 +158,7 @@ impl<Db: ?Sized + SourceDatabaseExt> SourceDatabaseExt2 for Db {
159158 }
160159}
161160
162- fn source_root_crates ( db : & dyn SourceDatabaseExt , id : SourceRootId ) -> Arc < [ CrateId ] > {
161+ fn source_root_crates ( db : & dyn SourceRootDatabase , id : SourceRootId ) -> Arc < [ CrateId ] > {
163162 let graph = db. crate_graph ( ) ;
164163 let mut crates = graph
165164 . iter ( )
@@ -173,13 +172,12 @@ fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<[Crat
173172 crates. into_iter ( ) . collect ( )
174173}
175174
176- /// Silly workaround for cyclic deps between the traits
175+ // FIXME: Would be nice to get rid of this somehow
176+ /// Silly workaround for cyclic deps due to the SourceRootDatabase and SourceDatabase split
177+ /// regarding FileLoader
177178pub struct FileLoaderDelegate < T > ( pub T ) ;
178179
179- impl < T : SourceDatabaseExt > FileLoader for FileLoaderDelegate < & ' _ T > {
180- fn file_text ( & self , file_id : FileId ) -> Arc < str > {
181- SourceDatabaseExt :: file_text ( self . 0 , file_id)
182- }
180+ impl < T : SourceRootDatabase > FileLoader for FileLoaderDelegate < & ' _ T > {
183181 fn resolve_path ( & self , path : AnchoredPath < ' _ > ) -> Option < FileId > {
184182 // FIXME: this *somehow* should be platform agnostic...
185183 let source_root = self . 0 . file_source_root ( path. anchor ) ;
0 commit comments