@@ -7,6 +7,7 @@ mod input;
7
7
8
8
use std:: panic;
9
9
10
+ use salsa:: Durability ;
10
11
use syntax:: { ast, Parse , SourceFile } ;
11
12
use triomphe:: Arc ;
12
13
@@ -42,6 +43,7 @@ pub trait Upcast<T: ?Sized> {
42
43
fn upcast ( & self ) -> & T ;
43
44
}
44
45
46
+ pub const DEFAULT_FILE_TEXT_LRU_CAP : usize = 16 ;
45
47
pub const DEFAULT_PARSE_LRU_CAP : usize = 128 ;
46
48
pub const DEFAULT_BORROWCK_LRU_CAP : usize = 1024 ;
47
49
@@ -89,7 +91,10 @@ fn parse(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
89
91
#[ salsa:: query_group( SourceDatabaseExtStorage ) ]
90
92
pub trait SourceDatabaseExt : SourceDatabase {
91
93
#[ salsa:: input]
94
+ fn compressed_file_text ( & self , file_id : FileId ) -> Arc < [ u8 ] > ;
95
+
92
96
fn file_text ( & self , file_id : FileId ) -> Arc < str > ;
97
+
93
98
/// Path to a file, relative to the root of its source root.
94
99
/// Source root of the file.
95
100
#[ salsa:: input]
@@ -101,6 +106,44 @@ pub trait SourceDatabaseExt: SourceDatabase {
101
106
fn source_root_crates ( & self , id : SourceRootId ) -> Arc < [ CrateId ] > ;
102
107
}
103
108
109
+ fn file_text ( db : & dyn SourceDatabaseExt , file_id : FileId ) -> Arc < str > {
110
+ let bytes = db. compressed_file_text ( file_id) ;
111
+ let bytes =
112
+ lz4_flex:: decompress_size_prepended ( & bytes) . expect ( "lz4 decompression should not fail" ) ;
113
+ let text = std:: str:: from_utf8 ( & bytes) . expect ( "file contents should be valid UTF-8" ) ;
114
+ Arc :: from ( text)
115
+ }
116
+
117
+ pub trait SourceDatabaseExt2 {
118
+ fn set_file_text ( & mut self , file_id : FileId , text : Arc < str > ) {
119
+ self . set_file_text_with_durability ( file_id, text, Durability :: LOW ) ;
120
+ }
121
+
122
+ fn set_file_text_with_durability (
123
+ & mut self ,
124
+ file_id : FileId ,
125
+ text : Arc < str > ,
126
+ durability : Durability ,
127
+ ) ;
128
+ }
129
+
130
+ impl < Db : ?Sized + SourceDatabaseExt > SourceDatabaseExt2 for Db {
131
+ fn set_file_text_with_durability (
132
+ & mut self ,
133
+ file_id : FileId ,
134
+ text : Arc < str > ,
135
+ durability : Durability ,
136
+ ) {
137
+ let bytes = text. as_bytes ( ) ;
138
+ let compressed = lz4_flex:: compress_prepend_size ( & bytes) ;
139
+ self . set_compressed_file_text_with_durability (
140
+ file_id,
141
+ Arc :: from ( compressed. as_slice ( ) ) ,
142
+ durability,
143
+ )
144
+ }
145
+ }
146
+
104
147
fn source_root_crates ( db : & dyn SourceDatabaseExt , id : SourceRootId ) -> Arc < [ CrateId ] > {
105
148
let graph = db. crate_graph ( ) ;
106
149
let mut crates = graph
0 commit comments