diff options
Diffstat (limited to 'crates/libanalysis/src/module_map_db')
-rw-r--r-- | crates/libanalysis/src/module_map_db/mod.rs | 159 |
1 files changed, 0 insertions, 159 deletions
diff --git a/crates/libanalysis/src/module_map_db/mod.rs b/crates/libanalysis/src/module_map_db/mod.rs deleted file mode 100644 index adad943da..000000000 --- a/crates/libanalysis/src/module_map_db/mod.rs +++ /dev/null | |||
@@ -1,159 +0,0 @@ | |||
1 | use std::sync::Arc; | ||
2 | use { | ||
3 | FileId, | ||
4 | db::{ | ||
5 | Query, QueryRegistry, QueryCtx, | ||
6 | file_syntax, file_set | ||
7 | }, | ||
8 | descriptors::{ModuleDescriptor, ModuleTreeDescriptor} | ||
9 | }; | ||
10 | |||
11 | pub(crate) fn register_queries(reg: &mut QueryRegistry) { | ||
12 | reg.add(MODULE_DESCR, "MODULE_DESCR"); | ||
13 | } | ||
14 | |||
15 | pub(crate) fn module_tree(ctx: QueryCtx) -> Arc<ModuleTreeDescriptor> { | ||
16 | ctx.get(MODULE_TREE, ()) | ||
17 | } | ||
18 | |||
19 | impl<'a> QueryCtx<'a> { | ||
20 | fn module_descr(&self, file_id: FileId) -> Arc<ModuleDescriptor> { | ||
21 | self.get(MODULE_DESCR, file_id) | ||
22 | } | ||
23 | } | ||
24 | |||
25 | const MODULE_DESCR: Query<FileId, ModuleDescriptor> = Query(30, |ctx, &file_id| { | ||
26 | let file = file_syntax(ctx, file_id); | ||
27 | ModuleDescriptor::new(file.ast()) | ||
28 | }); | ||
29 | |||
30 | const MODULE_TREE: Query<(), ModuleTreeDescriptor> = Query(31, |ctx, _| { | ||
31 | let file_set = file_set(ctx); | ||
32 | let mut files = Vec::new(); | ||
33 | for &file_id in file_set.0.iter() { | ||
34 | let module_descr = ctx.get(MODULE_DESCR, file_id); | ||
35 | files.push((file_id, module_descr)); | ||
36 | } | ||
37 | ModuleTreeDescriptor::new(files.iter().map(|(file_id, descr)| (*file_id, &**descr)), &file_set.1) | ||
38 | }); | ||
39 | |||
40 | #[cfg(test)] | ||
41 | mod tests { | ||
42 | use std::collections::HashMap; | ||
43 | use im; | ||
44 | use relative_path::{RelativePath, RelativePathBuf}; | ||
45 | use { | ||
46 | db::{Query, Db, State}, | ||
47 | imp::FileResolverImp, | ||
48 | FileId, FileResolver, | ||
49 | }; | ||
50 | use super::*; | ||
51 | |||
52 | #[derive(Debug)] | ||
53 | struct FileMap(im::HashMap<FileId, RelativePathBuf>); | ||
54 | |||
55 | impl FileResolver for FileMap { | ||
56 | fn file_stem(&self, file_id: FileId) -> String { | ||
57 | self.0[&file_id].file_stem().unwrap().to_string() | ||
58 | } | ||
59 | fn resolve(&self, file_id: FileId, rel: &RelativePath) -> Option<FileId> { | ||
60 | let path = self.0[&file_id].join(rel).normalize(); | ||
61 | self.0.iter() | ||
62 | .filter_map(|&(id, ref p)| Some(id).filter(|_| p == &path)) | ||
63 | .next() | ||
64 | } | ||
65 | } | ||
66 | |||
67 | struct Fixture { | ||
68 | next_file_id: u32, | ||
69 | fm: im::HashMap<FileId, RelativePathBuf>, | ||
70 | db: Db, | ||
71 | } | ||
72 | |||
73 | impl Fixture { | ||
74 | fn new() -> Fixture { | ||
75 | Fixture { | ||
76 | next_file_id: 1, | ||
77 | fm: im::HashMap::new(), | ||
78 | db: Db::new(), | ||
79 | } | ||
80 | } | ||
81 | fn add_file(&mut self, path: &str, text: &str) -> FileId { | ||
82 | assert!(path.starts_with("/")); | ||
83 | let file_id = FileId(self.next_file_id); | ||
84 | self.next_file_id += 1; | ||
85 | self.fm.insert(file_id, RelativePathBuf::from(&path[1..])); | ||
86 | let mut new_state = self.db.state().clone(); | ||
87 | new_state.file_map.insert(file_id, Arc::new(text.to_string())); | ||
88 | new_state.file_resolver = FileResolverImp::new( | ||
89 | Arc::new(FileMap(self.fm.clone())) | ||
90 | ); | ||
91 | self.db = self.db.with_changes(new_state, &[file_id], true); | ||
92 | file_id | ||
93 | } | ||
94 | fn remove_file(&mut self, file_id: FileId) { | ||
95 | self.fm.remove(&file_id); | ||
96 | let mut new_state = self.db.state().clone(); | ||
97 | new_state.file_map.remove(&file_id); | ||
98 | new_state.file_resolver = FileResolverImp::new( | ||
99 | Arc::new(FileMap(self.fm.clone())) | ||
100 | ); | ||
101 | self.db = self.db.with_changes(new_state, &[file_id], true); | ||
102 | } | ||
103 | fn change_file(&mut self, file_id: FileId, new_text: &str) { | ||
104 | let mut new_state = self.db.state().clone(); | ||
105 | new_state.file_map.insert(file_id, Arc::new(new_text.to_string())); | ||
106 | self.db = self.db.with_changes(new_state, &[file_id], false); | ||
107 | } | ||
108 | fn check_parent_modules( | ||
109 | &self, | ||
110 | file_id: FileId, | ||
111 | expected: &[FileId], | ||
112 | queries: &[(&'static str, u64)] | ||
113 | ) { | ||
114 | let (actual, events) = self.db.trace_query(|ctx| { | ||
115 | ctx.get(PARENT_MODULE, file_id) | ||
116 | }); | ||
117 | assert_eq!(actual.as_slice(), expected); | ||
118 | let mut counts = HashMap::new(); | ||
119 | events.into_iter() | ||
120 | .for_each(|event| *counts.entry(event).or_insert(0) += 1); | ||
121 | for &(query_id, expected_count) in queries.iter() { | ||
122 | let actual_count = *counts.get(&query_id).unwrap_or(&0); | ||
123 | assert_eq!( | ||
124 | actual_count, | ||
125 | expected_count, | ||
126 | "counts for {} differ", | ||
127 | query_id, | ||
128 | ) | ||
129 | } | ||
130 | |||
131 | } | ||
132 | } | ||
133 | |||
134 | #[test] | ||
135 | fn test_parent_module() { | ||
136 | let mut f = Fixture::new(); | ||
137 | let foo = f.add_file("/foo.rs", ""); | ||
138 | f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]); | ||
139 | |||
140 | let lib = f.add_file("/lib.rs", "mod foo;"); | ||
141 | f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]); | ||
142 | f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 0)]); | ||
143 | |||
144 | f.change_file(lib, ""); | ||
145 | f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]); | ||
146 | |||
147 | f.change_file(lib, "mod foo;"); | ||
148 | f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]); | ||
149 | |||
150 | f.change_file(lib, "mod bar;"); | ||
151 | f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]); | ||
152 | |||
153 | f.change_file(lib, "mod foo;"); | ||
154 | f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]); | ||
155 | |||
156 | f.remove_file(lib); | ||
157 | f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 0)]); | ||
158 | } | ||
159 | } | ||