]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide_api/src/db.rs
automatically collect garbage
[rust.git] / crates / ra_ide_api / src / db.rs
1 use std::{
2     sync::Arc,
3     time,
4 };
5
6 use ra_db::{
7     CheckCanceled, FileId, Canceled, SourceDatabase,
8     salsa,
9 };
10
11 use crate::{LineIndex, symbol_index::{self, SymbolsDatabase}};
12
13 #[salsa::database(
14     ra_db::SourceDatabaseStorage,
15     LineIndexDatabaseStorage,
16     symbol_index::SymbolsDatabaseStorage,
17     hir::db::HirDatabaseStorage
18 )]
19 #[derive(Debug)]
20 pub(crate) struct RootDatabase {
21     runtime: salsa::Runtime<RootDatabase>,
22     interner: Arc<hir::HirInterner>,
23     pub(crate) last_gc: time::Instant,
24     pub(crate) last_gc_check: time::Instant,
25 }
26
27 impl salsa::Database for RootDatabase {
28     fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
29         &self.runtime
30     }
31     fn on_propagated_panic(&self) -> ! {
32         Canceled::throw()
33     }
34 }
35
36 impl Default for RootDatabase {
37     fn default() -> RootDatabase {
38         let mut db = RootDatabase {
39             runtime: salsa::Runtime::default(),
40             interner: Default::default(),
41             last_gc: time::Instant::now(),
42             last_gc_check: time::Instant::now(),
43         };
44         db.set_crate_graph(Default::default());
45         db.set_local_roots(Default::default());
46         db.set_library_roots(Default::default());
47         db
48     }
49 }
50
51 impl salsa::ParallelDatabase for RootDatabase {
52     fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
53         salsa::Snapshot::new(RootDatabase {
54             runtime: self.runtime.snapshot(self),
55             interner: Arc::clone(&self.interner),
56             last_gc: self.last_gc.clone(),
57             last_gc_check: self.last_gc_check.clone(),
58         })
59     }
60 }
61
62 impl CheckCanceled for RootDatabase {}
63
64 impl AsRef<hir::HirInterner> for RootDatabase {
65     fn as_ref(&self) -> &hir::HirInterner {
66         &self.interner
67     }
68 }
69
70 #[salsa::query_group(LineIndexDatabaseStorage)]
71 pub(crate) trait LineIndexDatabase: ra_db::SourceDatabase + CheckCanceled {
72     fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
73 }
74
75 fn line_index(db: &impl ra_db::SourceDatabase, file_id: FileId) -> Arc<LineIndex> {
76     let text = db.file_text(file_id);
77     Arc::new(LineIndex::new(&*text))
78 }