diff options
Diffstat (limited to 'crates/ra_analysis/src/db.rs')
-rw-r--r-- | crates/ra_analysis/src/db.rs | 103 |
1 files changed, 103 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..0773edcc1 --- /dev/null +++ b/crates/ra_analysis/src/db.rs | |||
@@ -0,0 +1,103 @@ | |||
1 | use std::{ | ||
2 | fmt, | ||
3 | sync::Arc, | ||
4 | hash::{Hash, Hasher}, | ||
5 | }; | ||
6 | use salsa; | ||
7 | use rustc_hash::FxHashSet; | ||
8 | use ra_syntax::File; | ||
9 | use ra_editor::{LineIndex}; | ||
10 | use crate::{ | ||
11 | symbol_index::SymbolIndex, | ||
12 | module_map::{ModulesDatabase, ModuleTreeQuery, ModuleDescriptorQuery}, | ||
13 | FileId, FileResolverImp, | ||
14 | }; | ||
15 | |||
16 | #[derive(Default)] | ||
17 | pub(crate) struct RootDatabase { | ||
18 | runtime: salsa::runtime::Runtime<RootDatabase>, | ||
19 | } | ||
20 | |||
21 | impl fmt::Debug for RootDatabase { | ||
22 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
23 | fmt.write_str("RootDatabase { ... }") | ||
24 | } | ||
25 | } | ||
26 | |||
27 | impl salsa::Database for RootDatabase { | ||
28 | fn salsa_runtime(&self) -> &salsa::runtime::Runtime<RootDatabase> { | ||
29 | &self.runtime | ||
30 | } | ||
31 | } | ||
32 | |||
33 | salsa::database_storage! { | ||
34 | pub(crate) struct RootDatabaseStorage for RootDatabase { | ||
35 | impl FilesDatabase { | ||
36 | fn file_text() for FileTextQuery; | ||
37 | fn file_set() for FileSetQuery; | ||
38 | } | ||
39 | impl SyntaxDatabase { | ||
40 | fn file_syntax() for FileSyntaxQuery; | ||
41 | fn file_lines() for FileLinesQuery; | ||
42 | fn file_symbols() for FileSymbolsQuery; | ||
43 | } | ||
44 | impl ModulesDatabase { | ||
45 | fn module_tree() for ModuleTreeQuery; | ||
46 | fn module_descriptor() for ModuleDescriptorQuery; | ||
47 | } | ||
48 | } | ||
49 | } | ||
50 | |||
51 | salsa::query_group! { | ||
52 | pub(crate) trait FilesDatabase: salsa::Database { | ||
53 | fn file_text(file_id: FileId) -> Arc<String> { | ||
54 | type FileTextQuery; | ||
55 | storage input; | ||
56 | } | ||
57 | fn file_set(key: ()) -> Arc<FileSet> { | ||
58 | type FileSetQuery; | ||
59 | storage input; | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | |||
64 | #[derive(Default, Debug, PartialEq, Eq)] | ||
65 | pub(crate) struct FileSet { | ||
66 | pub(crate) files: FxHashSet<FileId>, | ||
67 | pub(crate) resolver: FileResolverImp, | ||
68 | } | ||
69 | |||
70 | impl Hash for FileSet { | ||
71 | fn hash<H: Hasher>(&self, hasher: &mut H) { | ||
72 | let mut files = self.files.iter().cloned().collect::<Vec<_>>(); | ||
73 | files.sort(); | ||
74 | files.hash(hasher); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | salsa::query_group! { | ||
79 | pub(crate) trait SyntaxDatabase: FilesDatabase { | ||
80 | fn file_syntax(file_id: FileId) -> File { | ||
81 | type FileSyntaxQuery; | ||
82 | } | ||
83 | fn file_lines(file_id: FileId) -> Arc<LineIndex> { | ||
84 | type FileLinesQuery; | ||
85 | } | ||
86 | fn file_symbols(file_id: FileId) -> Arc<SymbolIndex> { | ||
87 | type FileSymbolsQuery; | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | |||
92 | fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> File { | ||
93 | let text = db.file_text(file_id); | ||
94 | File::parse(&*text) | ||
95 | } | ||
96 | fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> { | ||
97 | let text = db.file_text(file_id); | ||
98 | Arc::new(LineIndex::new(&*text)) | ||
99 | } | ||
100 | fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<SymbolIndex> { | ||
101 | let syntax = db.file_syntax(file_id); | ||
102 | Arc::new(SymbolIndex::for_file(file_id, syntax)) | ||
103 | } | ||