diff options
Diffstat (limited to 'crates/libanalysis/src/module_map_db.rs')
-rw-r--r-- | crates/libanalysis/src/module_map_db.rs | 130 |
1 files changed, 61 insertions, 69 deletions
diff --git a/crates/libanalysis/src/module_map_db.rs b/crates/libanalysis/src/module_map_db.rs index 4d4bd9104..ff69cc0d2 100644 --- a/crates/libanalysis/src/module_map_db.rs +++ b/crates/libanalysis/src/module_map_db.rs | |||
@@ -2,66 +2,55 @@ use std::sync::Arc; | |||
2 | use { | 2 | use { |
3 | FileId, | 3 | FileId, |
4 | db::{ | 4 | db::{ |
5 | Query, Eval, QueryCtx, FileSyntax, Files, | 5 | Query, QueryCtx |
6 | Cache, QueryCache, | ||
7 | }, | 6 | }, |
8 | module_map::resolve_submodule, | 7 | module_map::resolve_submodule, |
9 | }; | 8 | }; |
10 | 9 | ||
11 | pub(crate) enum ModuleDescr {} | 10 | impl<'a> QueryCtx<'a> { |
12 | impl Query for ModuleDescr { | 11 | fn module_descr(&self, file_id: FileId) -> Arc<descr::ModuleDescr> { |
13 | const ID: u32 = 30; | 12 | self.get(MODULE_DESCR, file_id) |
14 | type Params = FileId; | 13 | } |
15 | type Output = Arc<descr::ModuleDescr>; | 14 | fn resolve_submodule(&self, file_id: FileId, submod: descr::Submodule) -> Arc<Vec<FileId>> { |
16 | } | 15 | self.get(RESOLVE_SUBMODULE, (file_id, submod)) |
17 | 16 | } | |
18 | enum ResolveSubmodule {} | ||
19 | impl Query for ResolveSubmodule { | ||
20 | const ID: u32 = 31; | ||
21 | type Params = (FileId, descr::Submodule); | ||
22 | type Output = Arc<Vec<FileId>>; | ||
23 | } | ||
24 | |||
25 | enum ParentModule {} | ||
26 | impl Query for ParentModule { | ||
27 | const ID: u32 = 40; | ||
28 | type Params = FileId; | ||
29 | type Output = Arc<Vec<FileId>>; | ||
30 | } | 17 | } |
31 | 18 | ||
32 | impl Eval for ModuleDescr { | 19 | pub(crate) const MODULE_DESCR: Query<FileId, descr::ModuleDescr> = Query { |
33 | fn eval(ctx: &QueryCtx, file_id: &FileId) -> Arc<descr::ModuleDescr> { | 20 | id: 30, |
34 | let file = ctx.get::<FileSyntax>(file_id); | 21 | f: |ctx, &file_id| { |
35 | Arc::new(descr::ModuleDescr::new(file.ast())) | 22 | let file = ctx.file_syntax(file_id); |
23 | descr::ModuleDescr::new(file.ast()) | ||
36 | } | 24 | } |
37 | } | 25 | }; |
38 | 26 | ||
39 | impl Eval for ResolveSubmodule { | 27 | pub(crate) const RESOLVE_SUBMODULE: Query<(FileId, descr::Submodule), Vec<FileId>> = Query { |
40 | fn eval(ctx: &QueryCtx, &(file_id, ref submodule): &(FileId, descr::Submodule)) -> Arc<Vec<FileId>> { | 28 | id: 31, |
41 | let files = ctx.get::<Files>(&()); | 29 | f: |ctx, params| { |
42 | let res = resolve_submodule(file_id, &submodule.name, &files.file_resolver()).0; | 30 | let files = ctx.file_set(); |
43 | Arc::new(res) | 31 | resolve_submodule(params.0, ¶ms.1.name, &files.1).0 |
44 | } | 32 | } |
45 | } | 33 | }; |
46 | 34 | ||
47 | impl Eval for ParentModule { | 35 | pub(crate) const PARENT_MODULE: Query<FileId, Vec<FileId>> = Query { |
48 | fn eval(ctx: &QueryCtx, file_id: &FileId) -> Arc<Vec<FileId>> { | 36 | id: 40, |
49 | let files = ctx.get::<Files>(&()); | 37 | f: |ctx, file_id| { |
50 | let res = files.iter() | 38 | let files = ctx.file_set(); |
51 | .map(|parent_id| (parent_id, ctx.get::<ModuleDescr>(&parent_id))) | 39 | let res = files.0.iter() |
40 | .map(|&parent_id| (parent_id, ctx.module_descr(parent_id))) | ||
52 | .filter(|(parent_id, descr)| { | 41 | .filter(|(parent_id, descr)| { |
53 | descr.submodules.iter() | 42 | descr.submodules.iter() |
54 | .any(|subm| { | 43 | .any(|subm| { |
55 | ctx.get::<ResolveSubmodule>(&(*parent_id, subm.clone())) | 44 | ctx.resolve_submodule(*parent_id, subm.clone()) |
56 | .iter() | 45 | .iter() |
57 | .any(|it| it == file_id) | 46 | .any(|it| it == file_id) |
58 | }) | 47 | }) |
59 | }) | 48 | }) |
60 | .map(|(id, _)| id) | 49 | .map(|(id, _)| id) |
61 | .collect(); | 50 | .collect(); |
62 | Arc::new(res) | 51 | res |
63 | } | 52 | } |
64 | } | 53 | }; |
65 | 54 | ||
66 | mod descr { | 55 | mod descr { |
67 | use libsyntax2::{ | 56 | use libsyntax2::{ |
@@ -102,7 +91,7 @@ mod tests { | |||
102 | use im; | 91 | use im; |
103 | use relative_path::{RelativePath, RelativePathBuf}; | 92 | use relative_path::{RelativePath, RelativePathBuf}; |
104 | use { | 93 | use { |
105 | db::{Query, DbHost, TraceEventKind}, | 94 | db::{Query, Db, State}, |
106 | imp::FileResolverImp, | 95 | imp::FileResolverImp, |
107 | FileId, FileResolver, | 96 | FileId, FileResolver, |
108 | }; | 97 | }; |
@@ -126,7 +115,7 @@ mod tests { | |||
126 | struct Fixture { | 115 | struct Fixture { |
127 | next_file_id: u32, | 116 | next_file_id: u32, |
128 | fm: im::HashMap<FileId, RelativePathBuf>, | 117 | fm: im::HashMap<FileId, RelativePathBuf>, |
129 | db: DbHost, | 118 | db: Db, |
130 | } | 119 | } |
131 | 120 | ||
132 | impl Fixture { | 121 | impl Fixture { |
@@ -134,7 +123,7 @@ mod tests { | |||
134 | Fixture { | 123 | Fixture { |
135 | next_file_id: 1, | 124 | next_file_id: 1, |
136 | fm: im::HashMap::new(), | 125 | fm: im::HashMap::new(), |
137 | db: DbHost::new(), | 126 | db: Db::new(State::default()), |
138 | } | 127 | } |
139 | } | 128 | } |
140 | fn add_file(&mut self, path: &str, text: &str) -> FileId { | 129 | fn add_file(&mut self, path: &str, text: &str) -> FileId { |
@@ -142,36 +131,39 @@ mod tests { | |||
142 | let file_id = FileId(self.next_file_id); | 131 | let file_id = FileId(self.next_file_id); |
143 | self.next_file_id += 1; | 132 | self.next_file_id += 1; |
144 | self.fm.insert(file_id, RelativePathBuf::from(&path[1..])); | 133 | self.fm.insert(file_id, RelativePathBuf::from(&path[1..])); |
145 | self.db.change_file(file_id, Some(text.to_string())); | 134 | let mut new_state = self.db.state().clone(); |
146 | self.db.set_file_resolver(FileResolverImp::new( | 135 | new_state.file_map.insert(file_id, text.to_string().into_boxed_str().into()); |
136 | new_state.resolver = FileResolverImp::new( | ||
147 | Arc::new(FileMap(self.fm.clone())) | 137 | Arc::new(FileMap(self.fm.clone())) |
148 | )); | 138 | ); |
149 | 139 | self.db = self.db.with_state(new_state, &[file_id], true); | |
150 | file_id | 140 | file_id |
151 | } | 141 | } |
152 | fn remove_file(&mut self, file_id: FileId) { | 142 | fn remove_file(&mut self, file_id: FileId) { |
153 | self.fm.remove(&file_id); | 143 | self.fm.remove(&file_id); |
154 | self.db.change_file(file_id, None); | 144 | let mut new_state = self.db.state().clone(); |
155 | self.db.set_file_resolver(FileResolverImp::new( | 145 | new_state.file_map.remove(&file_id); |
146 | new_state.resolver = FileResolverImp::new( | ||
156 | Arc::new(FileMap(self.fm.clone())) | 147 | Arc::new(FileMap(self.fm.clone())) |
157 | )) | 148 | ); |
149 | self.db = self.db.with_state(new_state, &[file_id], true); | ||
158 | } | 150 | } |
159 | fn change_file(&mut self, file_id: FileId, new_text: &str) { | 151 | fn change_file(&mut self, file_id: FileId, new_text: &str) { |
160 | self.db.change_file(file_id, Some(new_text.to_string())); | 152 | let mut new_state = self.db.state().clone(); |
153 | new_state.file_map.insert(file_id, new_text.to_string().into_boxed_str().into()); | ||
154 | self.db = self.db.with_state(new_state, &[file_id], false); | ||
161 | } | 155 | } |
162 | fn check_parent_modules( | 156 | fn check_parent_modules( |
163 | &self, | 157 | &self, |
164 | file_id: FileId, | 158 | file_id: FileId, |
165 | expected: &[FileId], | 159 | expected: &[FileId], |
166 | queries: &[(u32, u64)] | 160 | queries: &[(u16, u64)] |
167 | ) { | 161 | ) { |
168 | let ctx = self.db.query_ctx(); | 162 | let (actual, events) = self.db.get(PARENT_MODULE, file_id); |
169 | let actual = ctx.get::<ParentModule>(&file_id); | ||
170 | assert_eq!(actual.as_slice(), expected); | 163 | assert_eq!(actual.as_slice(), expected); |
171 | let mut counts = HashMap::new(); | 164 | let mut counts = HashMap::new(); |
172 | ctx.trace.borrow().iter() | 165 | events.into_iter() |
173 | .filter(|event| event.kind == TraceEventKind::Evaluating) | 166 | .for_each(|event| *counts.entry(event).or_insert(0) += 1); |
174 | .for_each(|event| *counts.entry(event.query_id).or_insert(0) += 1); | ||
175 | for &(query_id, expected_count) in queries.iter() { | 167 | for &(query_id, expected_count) in queries.iter() { |
176 | let actual_count = *counts.get(&query_id).unwrap_or(&0); | 168 | let actual_count = *counts.get(&query_id).unwrap_or(&0); |
177 | assert_eq!( | 169 | assert_eq!( |
@@ -189,25 +181,25 @@ mod tests { | |||
189 | fn test_parent_module() { | 181 | fn test_parent_module() { |
190 | let mut f = Fixture::new(); | 182 | let mut f = Fixture::new(); |
191 | let foo = f.add_file("/foo.rs", ""); | 183 | let foo = f.add_file("/foo.rs", ""); |
192 | // f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 1)]); | 184 | f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 1)]); |
193 | 185 | ||
194 | let lib = f.add_file("/lib.rs", "mod foo;"); | 186 | let lib = f.add_file("/lib.rs", "mod foo;"); |
195 | f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); | 187 | f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 1)]); |
196 | f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 0)]); | 188 | f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 0)]); |
197 | 189 | ||
198 | f.change_file(lib, ""); | 190 | f.change_file(lib, ""); |
199 | f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]); | 191 | f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 1)]); |
200 | 192 | ||
201 | // f.change_file(lib, "mod foo;"); | 193 | f.change_file(lib, "mod foo;"); |
202 | // f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); | 194 | f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 1)]); |
203 | 195 | ||
204 | // f.change_file(lib, "mod bar;"); | 196 | f.change_file(lib, "mod bar;"); |
205 | // f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 2)]); | 197 | f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 1)]); |
206 | 198 | ||
207 | // f.change_file(lib, "mod foo;"); | 199 | f.change_file(lib, "mod foo;"); |
208 | // f.check_parent_modules(foo, &[lib], &[(ModuleDescr::ID, 2)]); | 200 | f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 1)]); |
209 | 201 | ||
210 | // f.remove_file(lib); | 202 | f.remove_file(lib); |
211 | // f.check_parent_modules(foo, &[], &[(ModuleDescr::ID, 1)]); | 203 | f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 0)]); |
212 | } | 204 | } |
213 | } | 205 | } |