diff options
author | Aleksey Kladov <[email protected]> | 2018-10-25 08:57:55 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-10-25 14:25:40 +0100 |
commit | ee4d904cfb1b604bc8627491e05980ac43cd59e3 (patch) | |
tree | 54d1b937de544b8f6a8f2821ad9599aa82192375 /crates/ra_analysis/src/db/mod.rs | |
parent | 2cb2074c4b7219b32993abdcc7084637c0123d49 (diff) |
Store all the data in the Salsa Database
Diffstat (limited to 'crates/ra_analysis/src/db/mod.rs')
-rw-r--r-- | crates/ra_analysis/src/db/mod.rs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/crates/ra_analysis/src/db/mod.rs b/crates/ra_analysis/src/db/mod.rs new file mode 100644 index 000000000..8387118ad --- /dev/null +++ b/crates/ra_analysis/src/db/mod.rs | |||
@@ -0,0 +1,107 @@ | |||
1 | pub(crate) mod input; | ||
2 | |||
3 | use std::{ | ||
4 | fmt, | ||
5 | sync::Arc, | ||
6 | }; | ||
7 | |||
8 | use ra_editor::LineIndex; | ||
9 | use ra_syntax::File; | ||
10 | use salsa; | ||
11 | |||
12 | use crate::{ | ||
13 | db, | ||
14 | Cancelable, Canceled, | ||
15 | descriptors::module::{SubmodulesQuery, ModuleTreeQuery, ModulesDatabase}, | ||
16 | symbol_index::SymbolIndex, | ||
17 | FileId, | ||
18 | }; | ||
19 | |||
20 | #[derive(Default)] | ||
21 | pub(crate) struct RootDatabase { | ||
22 | runtime: salsa::Runtime<RootDatabase>, | ||
23 | } | ||
24 | |||
25 | impl fmt::Debug for RootDatabase { | ||
26 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
27 | fmt.write_str("RootDatabase { ... }") | ||
28 | } | ||
29 | } | ||
30 | |||
31 | impl salsa::Database for RootDatabase { | ||
32 | fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> { | ||
33 | &self.runtime | ||
34 | } | ||
35 | } | ||
36 | |||
37 | pub(crate) fn check_canceled(db: &impl salsa::Database) -> Cancelable<()> { | ||
38 | if db.salsa_runtime().is_current_revision_canceled() { | ||
39 | Err(Canceled) | ||
40 | } else { | ||
41 | Ok(()) | ||
42 | } | ||
43 | } | ||
44 | |||
45 | impl salsa::ParallelDatabase for RootDatabase { | ||
46 | fn fork(&self) -> Self { | ||
47 | RootDatabase { | ||
48 | runtime: self.runtime.fork(), | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | |||
53 | impl Clone for RootDatabase { | ||
54 | fn clone(&self) -> RootDatabase { | ||
55 | salsa::ParallelDatabase::fork(self) | ||
56 | } | ||
57 | } | ||
58 | |||
59 | salsa::database_storage! { | ||
60 | pub(crate) struct RootDatabaseStorage for RootDatabase { | ||
61 | impl input::FilesDatabase { | ||
62 | fn file_text() for input::FileTextQuery; | ||
63 | fn file_source_root() for input::FileSourceRootQuery; | ||
64 | fn source_root() for input::SourceRootQuery; | ||
65 | fn libraries() for input::LibrarieseQuery; | ||
66 | fn library_symbols() for input::LibrarySymbolsQuery; | ||
67 | fn crate_graph() for input::CrateGraphQuery; | ||
68 | } | ||
69 | impl SyntaxDatabase { | ||
70 | fn file_syntax() for FileSyntaxQuery; | ||
71 | fn file_lines() for FileLinesQuery; | ||
72 | fn file_symbols() for FileSymbolsQuery; | ||
73 | } | ||
74 | impl ModulesDatabase { | ||
75 | fn module_tree() for ModuleTreeQuery; | ||
76 | fn module_descriptor() for SubmodulesQuery; | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | |||
81 | salsa::query_group! { | ||
82 | pub(crate) trait SyntaxDatabase: input::FilesDatabase { | ||
83 | fn file_syntax(file_id: FileId) -> File { | ||
84 | type FileSyntaxQuery; | ||
85 | } | ||
86 | fn file_lines(file_id: FileId) -> Arc<LineIndex> { | ||
87 | type FileLinesQuery; | ||
88 | } | ||
89 | fn file_symbols(file_id: FileId) -> Cancelable<Arc<SymbolIndex>> { | ||
90 | type FileSymbolsQuery; | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | |||
95 | fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> File { | ||
96 | let text = db.file_text(file_id); | ||
97 | File::parse(&*text) | ||
98 | } | ||
99 | fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> { | ||
100 | let text = db.file_text(file_id); | ||
101 | Arc::new(LineIndex::new(&*text)) | ||
102 | } | ||
103 | fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable<Arc<SymbolIndex>> { | ||
104 | db::check_canceled(db)?; | ||
105 | let syntax = db.file_syntax(file_id); | ||
106 | Ok(Arc::new(SymbolIndex::for_file(file_id, syntax))) | ||
107 | } | ||