1
1
//! base_db defines basic database traits. The concrete DB is defined by ide.
2
-
2
+ // FIXME: Rename this crate, base db is non descriptive
3
3
mod change;
4
4
mod input;
5
5
@@ -47,8 +47,6 @@ pub const DEFAULT_PARSE_LRU_CAP: u16 = 128;
47
47
pub const DEFAULT_BORROWCK_LRU_CAP : u16 = 2024 ;
48
48
49
49
pub trait FileLoader {
50
- /// Text of the file.
51
- fn file_text ( & self , file_id : FileId ) -> Arc < str > ;
52
50
fn resolve_path ( & self , path : AnchoredPath < ' _ > ) -> Option < FileId > ;
53
51
/// Crates whose root's source root is the same as the source root of `file_id`
54
52
fn relevant_crates ( & self , file_id : FileId ) -> Arc < [ CrateId ] > ;
@@ -58,6 +56,13 @@ pub trait FileLoader {
58
56
/// model. Everything else in rust-analyzer is derived from these queries.
59
57
#[ salsa:: query_group( SourceDatabaseStorage ) ]
60
58
pub 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
+
61
66
/// Parses the file into the syntax tree.
62
67
#[ salsa:: lru]
63
68
fn parse ( & self , file_id : EditionedFileId ) -> Parse < ast:: SourceFile > ;
@@ -99,16 +104,18 @@ fn parse_errors(db: &dyn SourceDatabase, file_id: EditionedFileId) -> Option<Arc
99
104
}
100
105
}
101
106
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
+
102
115
/// We don't want to give HIR knowledge of source roots, hence we extract these
103
116
/// 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 {
112
119
/// Path to a file, relative to the root of its source root.
113
120
/// Source root of the file.
114
121
#[ salsa:: input]
@@ -121,15 +128,7 @@ pub trait SourceDatabaseExt: SourceDatabase {
121
128
fn source_root_crates ( & self , id : SourceRootId ) -> Arc < [ CrateId ] > ;
122
129
}
123
130
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 {
133
132
fn set_file_text ( & mut self , file_id : FileId , text : & str ) {
134
133
self . set_file_text_with_durability ( file_id, text, Durability :: LOW ) ;
135
134
}
@@ -142,7 +141,7 @@ pub trait SourceDatabaseExt2 {
142
141
) ;
143
142
}
144
143
145
- impl < Db : ?Sized + SourceDatabaseExt > SourceDatabaseExt2 for Db {
144
+ impl < Db : ?Sized + SourceRootDatabase > SourceDatabaseFileInputExt for Db {
146
145
fn set_file_text_with_durability (
147
146
& mut self ,
148
147
file_id : FileId ,
@@ -159,7 +158,7 @@ impl<Db: ?Sized + SourceDatabaseExt> SourceDatabaseExt2 for Db {
159
158
}
160
159
}
161
160
162
- fn source_root_crates ( db : & dyn SourceDatabaseExt , id : SourceRootId ) -> Arc < [ CrateId ] > {
161
+ fn source_root_crates ( db : & dyn SourceRootDatabase , id : SourceRootId ) -> Arc < [ CrateId ] > {
163
162
let graph = db. crate_graph ( ) ;
164
163
let mut crates = graph
165
164
. iter ( )
@@ -173,13 +172,12 @@ fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<[Crat
173
172
crates. into_iter ( ) . collect ( )
174
173
}
175
174
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
177
178
pub struct FileLoaderDelegate < T > ( pub T ) ;
178
179
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 > {
183
181
fn resolve_path ( & self , path : AnchoredPath < ' _ > ) -> Option < FileId > {
184
182
// FIXME: this *somehow* should be platform agnostic...
185
183
let source_root = self . 0 . file_source_root ( path. anchor ) ;
0 commit comments