diff options
Diffstat (limited to 'crates/ra_analysis/src/db.rs')
-rw-r--r-- | crates/ra_analysis/src/db.rs | 126 |
1 files changed, 47 insertions, 79 deletions
diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 4e34265fb..7fc3fe31b 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs | |||
@@ -1,17 +1,12 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | #[cfg(test)] | 2 | #[cfg(test)] |
3 | use parking_lot::Mutex; | 3 | use parking_lot::Mutex; |
4 | use ra_editor::LineIndex; | ||
5 | use ra_syntax::{SourceFileNode, SyntaxNode}; | ||
6 | use salsa::{self, Database}; | 4 | use salsa::{self, Database}; |
5 | use ra_db::{LocationIntener, BaseDatabase}; | ||
6 | use hir::{self, DefId, DefLoc, FnId, SourceItemId}; | ||
7 | 7 | ||
8 | use crate::{ | 8 | use crate::{ |
9 | db, | 9 | symbol_index, |
10 | descriptors, | ||
11 | symbol_index::SymbolIndex, | ||
12 | syntax_ptr::SyntaxPtr, | ||
13 | loc2id::{IdMaps, IdDatabase}, | ||
14 | Cancelable, Canceled, FileId, | ||
15 | }; | 10 | }; |
16 | 11 | ||
17 | #[derive(Debug)] | 12 | #[derive(Debug)] |
@@ -22,7 +17,13 @@ pub(crate) struct RootDatabase { | |||
22 | events: (), | 17 | events: (), |
23 | 18 | ||
24 | runtime: salsa::Runtime<RootDatabase>, | 19 | runtime: salsa::Runtime<RootDatabase>, |
25 | id_maps: IdMaps, | 20 | id_maps: Arc<IdMaps>, |
21 | } | ||
22 | |||
23 | #[derive(Debug, Default)] | ||
24 | struct IdMaps { | ||
25 | fns: LocationIntener<SourceItemId, FnId>, | ||
26 | defs: LocationIntener<DefLoc, DefId>, | ||
26 | } | 27 | } |
27 | 28 | ||
28 | impl salsa::Database for RootDatabase { | 29 | impl salsa::Database for RootDatabase { |
@@ -47,26 +48,18 @@ impl Default for RootDatabase { | |||
47 | let mut db = RootDatabase { | 48 | let mut db = RootDatabase { |
48 | events: Default::default(), | 49 | events: Default::default(), |
49 | runtime: salsa::Runtime::default(), | 50 | runtime: salsa::Runtime::default(), |
50 | id_maps: IdMaps::default(), | 51 | id_maps: Default::default(), |
51 | }; | 52 | }; |
52 | db.query_mut(crate::input::SourceRootQuery) | 53 | db.query_mut(ra_db::SourceRootQuery) |
53 | .set(crate::input::WORKSPACE, Default::default()); | 54 | .set(ra_db::WORKSPACE, Default::default()); |
54 | db.query_mut(crate::input::CrateGraphQuery) | 55 | db.query_mut(ra_db::CrateGraphQuery) |
55 | .set((), Default::default()); | 56 | .set((), Default::default()); |
56 | db.query_mut(crate::input::LibrariesQuery) | 57 | db.query_mut(ra_db::LibrariesQuery) |
57 | .set((), Default::default()); | 58 | .set((), Default::default()); |
58 | db | 59 | db |
59 | } | 60 | } |
60 | } | 61 | } |
61 | 62 | ||
62 | pub(crate) fn check_canceled(db: &impl salsa::Database) -> Cancelable<()> { | ||
63 | if db.salsa_runtime().is_current_revision_canceled() { | ||
64 | Err(Canceled) | ||
65 | } else { | ||
66 | Ok(()) | ||
67 | } | ||
68 | } | ||
69 | |||
70 | impl salsa::ParallelDatabase for RootDatabase { | 63 | impl salsa::ParallelDatabase for RootDatabase { |
71 | fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { | 64 | fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { |
72 | salsa::Snapshot::new(RootDatabase { | 65 | salsa::Snapshot::new(RootDatabase { |
@@ -77,9 +70,17 @@ impl salsa::ParallelDatabase for RootDatabase { | |||
77 | } | 70 | } |
78 | } | 71 | } |
79 | 72 | ||
80 | impl IdDatabase for RootDatabase { | 73 | impl BaseDatabase for RootDatabase {} |
81 | fn id_maps(&self) -> &IdMaps { | 74 | |
82 | &self.id_maps | 75 | impl AsRef<LocationIntener<DefLoc, DefId>> for RootDatabase { |
76 | fn as_ref(&self) -> &LocationIntener<DefLoc, DefId> { | ||
77 | &self.id_maps.defs | ||
78 | } | ||
79 | } | ||
80 | |||
81 | impl AsRef<LocationIntener<hir::SourceItemId, FnId>> for RootDatabase { | ||
82 | fn as_ref(&self) -> &LocationIntener<hir::SourceItemId, FnId> { | ||
83 | &self.id_maps.fns | ||
83 | } | 84 | } |
84 | } | 85 | } |
85 | 86 | ||
@@ -108,63 +109,30 @@ impl RootDatabase { | |||
108 | 109 | ||
109 | salsa::database_storage! { | 110 | salsa::database_storage! { |
110 | pub(crate) struct RootDatabaseStorage for RootDatabase { | 111 | pub(crate) struct RootDatabaseStorage for RootDatabase { |
111 | impl crate::input::FilesDatabase { | 112 | impl ra_db::FilesDatabase { |
112 | fn file_text() for crate::input::FileTextQuery; | 113 | fn file_text() for ra_db::FileTextQuery; |
113 | fn file_source_root() for crate::input::FileSourceRootQuery; | 114 | fn file_source_root() for ra_db::FileSourceRootQuery; |
114 | fn source_root() for crate::input::SourceRootQuery; | 115 | fn source_root() for ra_db::SourceRootQuery; |
115 | fn libraries() for crate::input::LibrariesQuery; | 116 | fn libraries() for ra_db::LibrariesQuery; |
116 | fn library_symbols() for crate::input::LibrarySymbolsQuery; | 117 | fn crate_graph() for ra_db::CrateGraphQuery; |
117 | fn crate_graph() for crate::input::CrateGraphQuery; | ||
118 | } | 118 | } |
119 | impl SyntaxDatabase { | 119 | impl ra_db::SyntaxDatabase { |
120 | fn file_syntax() for FileSyntaxQuery; | 120 | fn source_file() for ra_db::SourceFileQuery; |
121 | fn file_lines() for FileLinesQuery; | 121 | fn file_lines() for ra_db::FileLinesQuery; |
122 | fn file_symbols() for FileSymbolsQuery; | ||
123 | fn resolve_syntax_ptr() for ResolveSyntaxPtrQuery; | ||
124 | } | 122 | } |
125 | impl descriptors::DescriptorDatabase { | 123 | impl symbol_index::SymbolsDatabase { |
126 | fn module_tree() for descriptors::ModuleTreeQuery; | 124 | fn file_symbols() for symbol_index::FileSymbolsQuery; |
127 | fn fn_scopes() for descriptors::FnScopesQuery; | 125 | fn library_symbols() for symbol_index::LibrarySymbolsQuery; |
128 | fn _file_items() for descriptors::FileItemsQuery; | ||
129 | fn _file_item() for descriptors::FileItemQuery; | ||
130 | fn _input_module_items() for descriptors::InputModuleItemsQuery; | ||
131 | fn _item_map() for descriptors::ItemMapQuery; | ||
132 | fn _fn_syntax() for descriptors::FnSyntaxQuery; | ||
133 | fn _submodules() for descriptors::SubmodulesQuery; | ||
134 | } | 126 | } |
135 | } | 127 | impl hir::db::HirDatabase { |
136 | } | 128 | fn module_tree() for hir::db::ModuleTreeQuery; |
137 | 129 | fn fn_scopes() for hir::db::FnScopesQuery; | |
138 | salsa::query_group! { | 130 | fn file_items() for hir::db::SourceFileItemsQuery; |
139 | pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase { | 131 | fn file_item() for hir::db::FileItemQuery; |
140 | fn file_syntax(file_id: FileId) -> SourceFileNode { | 132 | fn input_module_items() for hir::db::InputModuleItemsQuery; |
141 | type FileSyntaxQuery; | 133 | fn item_map() for hir::db::ItemMapQuery; |
142 | } | 134 | fn fn_syntax() for hir::db::FnSyntaxQuery; |
143 | fn file_lines(file_id: FileId) -> Arc<LineIndex> { | 135 | fn submodules() for hir::db::SubmodulesQuery; |
144 | type FileLinesQuery; | ||
145 | } | ||
146 | fn file_symbols(file_id: FileId) -> Cancelable<Arc<SymbolIndex>> { | ||
147 | type FileSymbolsQuery; | ||
148 | } | ||
149 | fn resolve_syntax_ptr(ptr: SyntaxPtr) -> SyntaxNode { | ||
150 | type ResolveSyntaxPtrQuery; | ||
151 | // Don't retain syntax trees in memory | ||
152 | storage dependencies; | ||
153 | use fn crate::syntax_ptr::resolve_syntax_ptr; | ||
154 | } | 136 | } |
155 | } | 137 | } |
156 | } | 138 | } |
157 | |||
158 | fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode { | ||
159 | let text = db.file_text(file_id); | ||
160 | SourceFileNode::parse(&*text) | ||
161 | } | ||
162 | fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> { | ||
163 | let text = db.file_text(file_id); | ||
164 | Arc::new(LineIndex::new(&*text)) | ||
165 | } | ||
166 | fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable<Arc<SymbolIndex>> { | ||
167 | db::check_canceled(db)?; | ||
168 | let syntax = db.file_syntax(file_id); | ||
169 | Ok(Arc::new(SymbolIndex::for_file(file_id, syntax))) | ||
170 | } | ||