@@ -70,6 +70,25 @@ impl FlagId {
70
70
}
71
71
}
72
72
73
+ /// Same as `FileLongId`, but without the interning inside virtual files.
74
+ /// This is used to avoid the need to intern the file id inside salsa database inputs.
75
+ #[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
76
+ pub enum FileInput {
77
+ OnDisk ( PathBuf ) ,
78
+ Virtual ( VirtualFileInput ) ,
79
+ External ( salsa:: InternId ) ,
80
+ }
81
+
82
+ impl FileInput {
83
+ pub fn into_file_long_id ( self , db : & dyn FilesGroup ) -> FileLongId {
84
+ match self {
85
+ FileInput :: OnDisk ( path) => FileLongId :: OnDisk ( path) ,
86
+ FileInput :: Virtual ( vf) => FileLongId :: Virtual ( vf. into_virtual_file ( db) ) ,
87
+ FileInput :: External ( id) => FileLongId :: External ( id) ,
88
+ }
89
+ }
90
+ }
91
+
73
92
/// We use a higher level FileId struct, because not all files are on disk. Some might be online.
74
93
/// Some might be virtual/computed on demand.
75
94
#[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
@@ -79,7 +98,7 @@ pub enum FileLongId {
79
98
External ( salsa:: InternId ) ,
80
99
}
81
100
/// Whether the file holds syntax for a module or for an expression.
82
- #[ derive( Clone , Debug , Hash , PartialEq , Eq , Serialize , Deserialize ) ]
101
+ #[ derive( Clone , Copy , Debug , Hash , PartialEq , Eq , Serialize , Deserialize ) ]
83
102
pub enum FileKind {
84
103
Module ,
85
104
Expr ,
@@ -139,6 +158,31 @@ impl CodeOrigin {
139
158
}
140
159
}
141
160
161
+ /// Same as `VirtualFile`, but without the interning inside virtual files.
162
+ /// This is used to avoid the need to intern the file id inside salsa database inputs.
163
+ #[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
164
+ pub struct VirtualFileInput {
165
+ pub parent : Option < Arc < FileInput > > ,
166
+ pub name : SmolStr ,
167
+ pub content : Arc < str > ,
168
+ pub code_mappings : Arc < [ CodeMapping ] > ,
169
+ pub kind : FileKind ,
170
+ pub original_item_removed : bool ,
171
+ }
172
+
173
+ impl VirtualFileInput {
174
+ fn into_virtual_file ( self , db : & dyn FilesGroup ) -> VirtualFile {
175
+ VirtualFile {
176
+ parent : self . parent . map ( |id| id. as_ref ( ) . clone ( ) . into_file_long_id ( db) . intern ( db) ) ,
177
+ name : self . name ,
178
+ content : self . content ,
179
+ code_mappings : self . code_mappings ,
180
+ kind : self . kind ,
181
+ original_item_removed : self . original_item_removed ,
182
+ }
183
+ }
184
+ }
185
+
142
186
#[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
143
187
pub struct VirtualFile {
144
188
pub parent : Option < FileId > ,
@@ -160,36 +204,72 @@ impl VirtualFile {
160
204
self . name . clone ( ) . into ( )
161
205
}
162
206
}
163
- }
164
207
165
- define_short_id ! ( FileId , FileLongId , FilesGroup , lookup_intern_file, intern_file) ;
166
- impl FileId {
167
- pub fn new ( db : & dyn FilesGroup , path : PathBuf ) -> FileId {
168
- FileLongId :: OnDisk ( path. clean ( ) ) . intern ( db)
208
+ fn into_virtual_file_input ( self , db : & dyn FilesGroup ) -> VirtualFileInput {
209
+ VirtualFileInput {
210
+ parent : self
211
+ . parent
212
+ . map ( |id| Arc :: new ( id. clone ( ) . lookup_intern ( db) . into_file_input ( db) ) ) ,
213
+ name : self . name ,
214
+ content : self . content ,
215
+ code_mappings : self . code_mappings ,
216
+ kind : self . kind ,
217
+ original_item_removed : self . original_item_removed ,
218
+ }
169
219
}
170
- pub fn file_name ( self , db : & dyn FilesGroup ) -> String {
171
- match self . lookup_intern ( db) {
220
+ }
221
+
222
+ impl FileLongId {
223
+ pub fn file_name ( & self , db : & dyn FilesGroup ) -> String {
224
+ match self {
172
225
FileLongId :: OnDisk ( path) => {
173
226
path. file_name ( ) . and_then ( |x| x. to_str ( ) ) . unwrap_or ( "<unknown>" ) . to_string ( )
174
227
}
175
228
FileLongId :: Virtual ( vf) => vf. name . to_string ( ) ,
176
- FileLongId :: External ( external_id) => db. ext_as_virtual ( external_id) . name . to_string ( ) ,
229
+ FileLongId :: External ( external_id) => db. ext_as_virtual ( * external_id) . name . to_string ( ) ,
177
230
}
178
231
}
179
- pub fn full_path ( self , db : & dyn FilesGroup ) -> String {
180
- match self . lookup_intern ( db ) {
232
+ pub fn full_path ( & self , db : & dyn FilesGroup ) -> String {
233
+ match self {
181
234
FileLongId :: OnDisk ( path) => path. to_str ( ) . unwrap_or ( "<unknown>" ) . to_string ( ) ,
182
235
FileLongId :: Virtual ( vf) => vf. full_path ( db) ,
183
- FileLongId :: External ( external_id) => db. ext_as_virtual ( external_id) . full_path ( db) ,
236
+ FileLongId :: External ( external_id) => db. ext_as_virtual ( * external_id) . full_path ( db) ,
184
237
}
185
238
}
186
- pub fn kind ( self , db : & dyn FilesGroup ) -> FileKind {
187
- match self . lookup_intern ( db ) {
239
+ pub fn kind ( & self ) -> FileKind {
240
+ match self {
188
241
FileLongId :: OnDisk ( _) => FileKind :: Module ,
189
242
FileLongId :: Virtual ( vf) => vf. kind ,
190
243
FileLongId :: External ( _) => FileKind :: Module ,
191
244
}
192
245
}
246
+
247
+ pub fn into_file_input ( & self , db : & dyn FilesGroup ) -> FileInput {
248
+ match self {
249
+ FileLongId :: OnDisk ( path) => FileInput :: OnDisk ( path. clone ( ) ) ,
250
+ FileLongId :: Virtual ( vf) => FileInput :: Virtual ( vf. clone ( ) . into_virtual_file_input ( db) ) ,
251
+ FileLongId :: External ( id) => FileInput :: External ( * id) ,
252
+ }
253
+ }
254
+ }
255
+
256
+ define_short_id ! ( FileId , FileLongId , FilesGroup , lookup_intern_file, intern_file) ;
257
+ impl FileId {
258
+ pub fn new ( db : & dyn FilesGroup , path : PathBuf ) -> FileId {
259
+ FileLongId :: OnDisk ( path. clean ( ) ) . intern ( db)
260
+ }
261
+
262
+ pub fn file_name ( self , db : & dyn FilesGroup ) -> String {
263
+ self . lookup_intern ( db) . file_name ( db)
264
+ }
265
+
266
+ pub fn full_path ( self , db : & dyn FilesGroup ) -> String {
267
+ self . lookup_intern ( db) . full_path ( db)
268
+ }
269
+
270
+ pub fn kind ( self , db : & dyn FilesGroup ) -> FileKind {
271
+ self . lookup_intern ( db) . kind ( )
272
+ }
193
273
}
194
274
195
275
#[ derive( Clone , Debug , Hash , PartialEq , Eq ) ]
0 commit comments