diff options
Diffstat (limited to 'crates/ra_ide_api/src/db.rs')
-rw-r--r-- | crates/ra_ide_api/src/db.rs | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs new file mode 100644 index 000000000..9d46609ec --- /dev/null +++ b/crates/ra_ide_api/src/db.rs | |||
@@ -0,0 +1,128 @@ | |||
1 | use std::{fmt, sync::Arc}; | ||
2 | |||
3 | use salsa::{self, Database}; | ||
4 | use ra_db::{LocationIntener, BaseDatabase, FileId}; | ||
5 | |||
6 | use crate::{symbol_index, LineIndex}; | ||
7 | |||
8 | #[derive(Debug)] | ||
9 | pub(crate) struct RootDatabase { | ||
10 | runtime: salsa::Runtime<RootDatabase>, | ||
11 | id_maps: Arc<IdMaps>, | ||
12 | } | ||
13 | |||
14 | #[derive(Default)] | ||
15 | struct IdMaps { | ||
16 | defs: LocationIntener<hir::DefLoc, hir::DefId>, | ||
17 | macros: LocationIntener<hir::MacroCallLoc, hir::MacroCallId>, | ||
18 | } | ||
19 | |||
20 | impl fmt::Debug for IdMaps { | ||
21 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
22 | f.debug_struct("IdMaps") | ||
23 | .field("n_defs", &self.defs.len()) | ||
24 | .finish() | ||
25 | } | ||
26 | } | ||
27 | |||
28 | impl salsa::Database for RootDatabase { | ||
29 | fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> { | ||
30 | &self.runtime | ||
31 | } | ||
32 | } | ||
33 | |||
34 | impl Default for RootDatabase { | ||
35 | fn default() -> RootDatabase { | ||
36 | let mut db = RootDatabase { | ||
37 | runtime: salsa::Runtime::default(), | ||
38 | id_maps: Default::default(), | ||
39 | }; | ||
40 | db.query_mut(ra_db::CrateGraphQuery) | ||
41 | .set((), Default::default()); | ||
42 | db.query_mut(ra_db::LocalRootsQuery) | ||
43 | .set((), Default::default()); | ||
44 | db.query_mut(ra_db::LibraryRootsQuery) | ||
45 | .set((), Default::default()); | ||
46 | db | ||
47 | } | ||
48 | } | ||
49 | |||
50 | impl salsa::ParallelDatabase for RootDatabase { | ||
51 | fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { | ||
52 | salsa::Snapshot::new(RootDatabase { | ||
53 | runtime: self.runtime.snapshot(self), | ||
54 | id_maps: self.id_maps.clone(), | ||
55 | }) | ||
56 | } | ||
57 | } | ||
58 | |||
59 | impl BaseDatabase for RootDatabase {} | ||
60 | |||
61 | impl AsRef<LocationIntener<hir::DefLoc, hir::DefId>> for RootDatabase { | ||
62 | fn as_ref(&self) -> &LocationIntener<hir::DefLoc, hir::DefId> { | ||
63 | &self.id_maps.defs | ||
64 | } | ||
65 | } | ||
66 | |||
67 | impl AsRef<LocationIntener<hir::MacroCallLoc, hir::MacroCallId>> for RootDatabase { | ||
68 | fn as_ref(&self) -> &LocationIntener<hir::MacroCallLoc, hir::MacroCallId> { | ||
69 | &self.id_maps.macros | ||
70 | } | ||
71 | } | ||
72 | |||
73 | salsa::query_group! { | ||
74 | pub(crate) trait LineIndexDatabase: ra_db::FilesDatabase + BaseDatabase { | ||
75 | fn line_index(file_id: FileId) -> Arc<LineIndex> { | ||
76 | type LineIndexQuery; | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | |||
81 | fn line_index(db: &impl ra_db::FilesDatabase, file_id: FileId) -> Arc<LineIndex> { | ||
82 | let text = db.file_text(file_id); | ||
83 | Arc::new(LineIndex::new(&*text)) | ||
84 | } | ||
85 | |||
86 | salsa::database_storage! { | ||
87 | pub(crate) struct RootDatabaseStorage for RootDatabase { | ||
88 | impl ra_db::FilesDatabase { | ||
89 | fn file_text() for ra_db::FileTextQuery; | ||
90 | fn file_relative_path() for ra_db::FileRelativePathQuery; | ||
91 | fn file_source_root() for ra_db::FileSourceRootQuery; | ||
92 | fn source_root() for ra_db::SourceRootQuery; | ||
93 | fn local_roots() for ra_db::LocalRootsQuery; | ||
94 | fn library_roots() for ra_db::LibraryRootsQuery; | ||
95 | fn crate_graph() for ra_db::CrateGraphQuery; | ||
96 | } | ||
97 | impl ra_db::SyntaxDatabase { | ||
98 | fn source_file() for ra_db::SourceFileQuery; | ||
99 | } | ||
100 | impl LineIndexDatabase { | ||
101 | fn line_index() for LineIndexQuery; | ||
102 | } | ||
103 | impl symbol_index::SymbolsDatabase { | ||
104 | fn file_symbols() for symbol_index::FileSymbolsQuery; | ||
105 | fn library_symbols() for symbol_index::LibrarySymbolsQuery; | ||
106 | } | ||
107 | impl hir::db::HirDatabase { | ||
108 | fn hir_source_file() for hir::db::HirSourceFileQuery; | ||
109 | fn expand_macro_invocation() for hir::db::ExpandMacroCallQuery; | ||
110 | fn module_tree() for hir::db::ModuleTreeQuery; | ||
111 | fn fn_scopes() for hir::db::FnScopesQuery; | ||
112 | fn file_items() for hir::db::SourceFileItemsQuery; | ||
113 | fn file_item() for hir::db::FileItemQuery; | ||
114 | fn input_module_items() for hir::db::InputModuleItemsQuery; | ||
115 | fn item_map() for hir::db::ItemMapQuery; | ||
116 | fn submodules() for hir::db::SubmodulesQuery; | ||
117 | fn infer() for hir::db::InferQuery; | ||
118 | fn type_for_def() for hir::db::TypeForDefQuery; | ||
119 | fn type_for_field() for hir::db::TypeForFieldQuery; | ||
120 | fn struct_data() for hir::db::StructDataQuery; | ||
121 | fn enum_data() for hir::db::EnumDataQuery; | ||
122 | fn impls_in_module() for hir::db::ImplsInModuleQuery; | ||
123 | fn body_hir() for hir::db::BodyHirQuery; | ||
124 | fn body_syntax_mapping() for hir::db::BodySyntaxMappingQuery; | ||
125 | fn fn_signature() for hir::db::FnSignatureQuery; | ||
126 | } | ||
127 | } | ||
128 | } | ||