diff options
Diffstat (limited to 'crates/libanalysis/src/module_map_db/mod.rs')
-rw-r--r-- | crates/libanalysis/src/module_map_db/mod.rs | 108 |
1 files changed, 51 insertions, 57 deletions
diff --git a/crates/libanalysis/src/module_map_db/mod.rs b/crates/libanalysis/src/module_map_db/mod.rs index 02ac06c5f..5560e4a34 100644 --- a/crates/libanalysis/src/module_map_db/mod.rs +++ b/crates/libanalysis/src/module_map_db/mod.rs | |||
@@ -4,15 +4,16 @@ use std::sync::Arc; | |||
4 | use { | 4 | use { |
5 | FileId, | 5 | FileId, |
6 | db::{ | 6 | db::{ |
7 | BoxedQuery, Query, QueryCtx | 7 | Query, QueryRegistry, QueryCtx, |
8 | file_syntax, file_set | ||
8 | }, | 9 | }, |
9 | module_map::resolve_submodule, | 10 | module_map::resolve_submodule, |
10 | }; | 11 | }; |
11 | 12 | ||
12 | pub(crate) fn queries(acc: &mut Vec<BoxedQuery>) { | 13 | pub(crate) fn register_queries(reg: &mut QueryRegistry) { |
13 | acc.push(MODULE_DESCR.into()); | 14 | reg.add(MODULE_DESCR, "MODULE_DESCR"); |
14 | acc.push(RESOLVE_SUBMODULE.into()); | 15 | reg.add(RESOLVE_SUBMODULE, "RESOLVE_SUBMODULE"); |
15 | acc.push(PARENT_MODULE.into()); | 16 | reg.add(PARENT_MODULE, "PARENT_MODULE"); |
16 | } | 17 | } |
17 | 18 | ||
18 | impl<'a> QueryCtx<'a> { | 19 | impl<'a> QueryCtx<'a> { |
@@ -24,41 +25,32 @@ impl<'a> QueryCtx<'a> { | |||
24 | } | 25 | } |
25 | } | 26 | } |
26 | 27 | ||
27 | pub(crate) const MODULE_DESCR: Query<FileId, descr::ModuleDescr> = Query { | 28 | const MODULE_DESCR: Query<FileId, descr::ModuleDescr> = Query(30, |ctx, &file_id| { |
28 | id: 30, | 29 | let file = file_syntax(ctx, file_id); |
29 | f: |ctx, &file_id| { | 30 | descr::ModuleDescr::new(file.ast()) |
30 | let file = ctx.file_syntax(file_id); | 31 | }); |
31 | descr::ModuleDescr::new(file.ast()) | 32 | |
32 | } | 33 | const RESOLVE_SUBMODULE: Query<(FileId, descr::Submodule), Vec<FileId>> = Query(31, |ctx, params| { |
33 | }; | 34 | let files = file_set(ctx); |
34 | 35 | resolve_submodule(params.0, ¶ms.1.name, &files.1).0 | |
35 | pub(crate) const RESOLVE_SUBMODULE: Query<(FileId, descr::Submodule), Vec<FileId>> = Query { | 36 | }); |
36 | id: 31, | 37 | |
37 | f: |ctx, params| { | 38 | const PARENT_MODULE: Query<FileId, Vec<FileId>> = Query(40, |ctx, file_id| { |
38 | let files = ctx.file_set(); | 39 | let files = file_set(ctx); |
39 | resolve_submodule(params.0, ¶ms.1.name, &files.1).0 | 40 | let res = files.0.iter() |
40 | } | 41 | .map(|&parent_id| (parent_id, ctx.module_descr(parent_id))) |
41 | }; | 42 | .filter(|(parent_id, descr)| { |
42 | 43 | descr.submodules.iter() | |
43 | pub(crate) const PARENT_MODULE: Query<FileId, Vec<FileId>> = Query { | 44 | .any(|subm| { |
44 | id: 40, | 45 | ctx.resolve_submodule(*parent_id, subm.clone()) |
45 | f: |ctx, file_id| { | 46 | .iter() |
46 | let files = ctx.file_set(); | 47 | .any(|it| it == file_id) |
47 | let res = files.0.iter() | 48 | }) |
48 | .map(|&parent_id| (parent_id, ctx.module_descr(parent_id))) | 49 | }) |
49 | .filter(|(parent_id, descr)| { | 50 | .map(|(id, _)| id) |
50 | descr.submodules.iter() | 51 | .collect(); |
51 | .any(|subm| { | 52 | res |
52 | ctx.resolve_submodule(*parent_id, subm.clone()) | 53 | }); |
53 | .iter() | ||
54 | .any(|it| it == file_id) | ||
55 | }) | ||
56 | }) | ||
57 | .map(|(id, _)| id) | ||
58 | .collect(); | ||
59 | res | ||
60 | } | ||
61 | }; | ||
62 | 54 | ||
63 | #[cfg(test)] | 55 | #[cfg(test)] |
64 | mod tests { | 56 | mod tests { |
@@ -107,34 +99,36 @@ mod tests { | |||
107 | self.next_file_id += 1; | 99 | self.next_file_id += 1; |
108 | self.fm.insert(file_id, RelativePathBuf::from(&path[1..])); | 100 | self.fm.insert(file_id, RelativePathBuf::from(&path[1..])); |
109 | let mut new_state = self.db.state().clone(); | 101 | let mut new_state = self.db.state().clone(); |
110 | new_state.file_map.insert(file_id, text.to_string().into_boxed_str().into()); | 102 | new_state.file_map.insert(file_id, Arc::new(text.to_string())); |
111 | new_state.resolver = FileResolverImp::new( | 103 | new_state.file_resolver = FileResolverImp::new( |
112 | Arc::new(FileMap(self.fm.clone())) | 104 | Arc::new(FileMap(self.fm.clone())) |
113 | ); | 105 | ); |
114 | self.db = self.db.with_state(new_state, &[file_id], true); | 106 | self.db = self.db.with_changes(new_state, &[file_id], true); |
115 | file_id | 107 | file_id |
116 | } | 108 | } |
117 | fn remove_file(&mut self, file_id: FileId) { | 109 | fn remove_file(&mut self, file_id: FileId) { |
118 | self.fm.remove(&file_id); | 110 | self.fm.remove(&file_id); |
119 | let mut new_state = self.db.state().clone(); | 111 | let mut new_state = self.db.state().clone(); |
120 | new_state.file_map.remove(&file_id); | 112 | new_state.file_map.remove(&file_id); |
121 | new_state.resolver = FileResolverImp::new( | 113 | new_state.file_resolver = FileResolverImp::new( |
122 | Arc::new(FileMap(self.fm.clone())) | 114 | Arc::new(FileMap(self.fm.clone())) |
123 | ); | 115 | ); |
124 | self.db = self.db.with_state(new_state, &[file_id], true); | 116 | self.db = self.db.with_changes(new_state, &[file_id], true); |
125 | } | 117 | } |
126 | fn change_file(&mut self, file_id: FileId, new_text: &str) { | 118 | fn change_file(&mut self, file_id: FileId, new_text: &str) { |
127 | let mut new_state = self.db.state().clone(); | 119 | let mut new_state = self.db.state().clone(); |
128 | new_state.file_map.insert(file_id, new_text.to_string().into_boxed_str().into()); | 120 | new_state.file_map.insert(file_id, Arc::new(new_text.to_string())); |
129 | self.db = self.db.with_state(new_state, &[file_id], false); | 121 | self.db = self.db.with_changes(new_state, &[file_id], false); |
130 | } | 122 | } |
131 | fn check_parent_modules( | 123 | fn check_parent_modules( |
132 | &self, | 124 | &self, |
133 | file_id: FileId, | 125 | file_id: FileId, |
134 | expected: &[FileId], | 126 | expected: &[FileId], |
135 | queries: &[(u16, u64)] | 127 | queries: &[(&'static str, u64)] |
136 | ) { | 128 | ) { |
137 | let (actual, events) = self.db.get(PARENT_MODULE, file_id); | 129 | let (actual, events) = self.db.trace_query(|ctx| { |
130 | ctx.get(PARENT_MODULE, file_id) | ||
131 | }); | ||
138 | assert_eq!(actual.as_slice(), expected); | 132 | assert_eq!(actual.as_slice(), expected); |
139 | let mut counts = HashMap::new(); | 133 | let mut counts = HashMap::new(); |
140 | events.into_iter() | 134 | events.into_iter() |
@@ -156,25 +150,25 @@ mod tests { | |||
156 | fn test_parent_module() { | 150 | fn test_parent_module() { |
157 | let mut f = Fixture::new(); | 151 | let mut f = Fixture::new(); |
158 | let foo = f.add_file("/foo.rs", ""); | 152 | let foo = f.add_file("/foo.rs", ""); |
159 | f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 1)]); | 153 | f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]); |
160 | 154 | ||
161 | let lib = f.add_file("/lib.rs", "mod foo;"); | 155 | let lib = f.add_file("/lib.rs", "mod foo;"); |
162 | f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 1)]); | 156 | f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]); |
163 | f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 0)]); | 157 | f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 0)]); |
164 | 158 | ||
165 | f.change_file(lib, ""); | 159 | f.change_file(lib, ""); |
166 | f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 1)]); | 160 | f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]); |
167 | 161 | ||
168 | f.change_file(lib, "mod foo;"); | 162 | f.change_file(lib, "mod foo;"); |
169 | f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 1)]); | 163 | f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]); |
170 | 164 | ||
171 | f.change_file(lib, "mod bar;"); | 165 | f.change_file(lib, "mod bar;"); |
172 | f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 1)]); | 166 | f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]); |
173 | 167 | ||
174 | f.change_file(lib, "mod foo;"); | 168 | f.change_file(lib, "mod foo;"); |
175 | f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 1)]); | 169 | f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]); |
176 | 170 | ||
177 | f.remove_file(lib); | 171 | f.remove_file(lib); |
178 | f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 0)]); | 172 | f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 0)]); |
179 | } | 173 | } |
180 | } | 174 | } |