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