aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_analysis/src/db/mod.rs7
-rw-r--r--crates/ra_analysis/src/descriptors.rs10
-rw-r--r--crates/ra_analysis/src/lib.rs2
-rw-r--r--crates/ra_analysis/src/module_map.rs286
-rw-r--r--crates/ra_analysis/src/roots.rs32
5 files changed, 172 insertions, 165 deletions
diff --git a/crates/ra_analysis/src/db/mod.rs b/crates/ra_analysis/src/db/mod.rs
index 4b3e0fc90..c54d50252 100644
--- a/crates/ra_analysis/src/db/mod.rs
+++ b/crates/ra_analysis/src/db/mod.rs
@@ -9,7 +9,8 @@ use ra_syntax::File;
9use ra_editor::{LineIndex}; 9use ra_editor::{LineIndex};
10use crate::{ 10use crate::{
11 symbol_index::SymbolIndex, 11 symbol_index::SymbolIndex,
12 FileId, FileResolverImp 12 module_map::{ModulesDatabase, ModuleTreeQuery, ModuleDescriptorQuery},
13 FileId, FileResolverImp,
13}; 14};
14 15
15#[derive(Default)] 16#[derive(Default)]
@@ -40,6 +41,10 @@ salsa::database_storage! {
40 fn file_lines() for FileLinesQuery; 41 fn file_lines() for FileLinesQuery;
41 fn file_symbols() for FileSymbolsQuery; 42 fn file_symbols() for FileSymbolsQuery;
42 } 43 }
44 impl ModulesDatabase {
45 fn module_tree() for ModuleTreeQuery;
46 fn module_descriptor() for ModuleDescriptorQuery;
47 }
43 } 48 }
44} 49}
45 50
diff --git a/crates/ra_analysis/src/descriptors.rs b/crates/ra_analysis/src/descriptors.rs
index f26dac875..8d9f38ca5 100644
--- a/crates/ra_analysis/src/descriptors.rs
+++ b/crates/ra_analysis/src/descriptors.rs
@@ -12,7 +12,7 @@ use crate::{
12 imp::FileResolverImp, 12 imp::FileResolverImp,
13}; 13};
14 14
15#[derive(Debug, Hash)] 15#[derive(Debug, PartialEq, Eq, Hash)]
16pub struct ModuleDescriptor { 16pub struct ModuleDescriptor {
17 pub submodules: Vec<Submodule> 17 pub submodules: Vec<Submodule>
18} 18}
@@ -43,7 +43,7 @@ pub struct Submodule {
43 pub name: SmolStr, 43 pub name: SmolStr,
44} 44}
45 45
46#[derive(Hash, Debug)] 46#[derive(Debug, PartialEq, Eq, Hash)]
47pub(crate) struct ModuleTreeDescriptor { 47pub(crate) struct ModuleTreeDescriptor {
48 nodes: Vec<NodeData>, 48 nodes: Vec<NodeData>,
49 links: Vec<LinkData>, 49 links: Vec<LinkData>,
@@ -52,7 +52,7 @@ pub(crate) struct ModuleTreeDescriptor {
52 52
53#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] 53#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
54struct Node(usize); 54struct Node(usize);
55#[derive(Hash, Debug)] 55#[derive(Hash, Debug, PartialEq, Eq)]
56struct NodeData { 56struct NodeData {
57 file_id: FileId, 57 file_id: FileId,
58 links: Vec<Link>, 58 links: Vec<Link>,
@@ -61,7 +61,7 @@ struct NodeData {
61 61
62#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] 62#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
63pub(crate) struct Link(usize); 63pub(crate) struct Link(usize);
64#[derive(Hash, Debug)] 64#[derive(Hash, Debug, PartialEq, Eq)]
65struct LinkData { 65struct LinkData {
66 owner: Node, 66 owner: Node,
67 name: SmolStr, 67 name: SmolStr,
@@ -70,7 +70,7 @@ struct LinkData {
70} 70}
71 71
72 72
73#[derive(Clone, Debug, Hash)] 73#[derive(Clone, Debug, Hash, PartialEq, Eq)]
74pub enum Problem { 74pub enum Problem {
75 UnresolvedModule { 75 UnresolvedModule {
76 candidate: RelativePathBuf, 76 candidate: RelativePathBuf,
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs
index 4c1ccdeaf..d49132513 100644
--- a/crates/ra_analysis/src/lib.rs
+++ b/crates/ra_analysis/src/lib.rs
@@ -14,7 +14,7 @@ extern crate salsa;
14extern crate rustc_hash; 14extern crate rustc_hash;
15 15
16mod symbol_index; 16mod symbol_index;
17// mod module_map; 17mod module_map;
18mod imp; 18mod imp;
19mod job; 19mod job;
20mod roots; 20mod roots;
diff --git a/crates/ra_analysis/src/module_map.rs b/crates/ra_analysis/src/module_map.rs
index c77c5cec6..95a770ae7 100644
--- a/crates/ra_analysis/src/module_map.rs
+++ b/crates/ra_analysis/src/module_map.rs
@@ -1,157 +1,157 @@
1use std::sync::Arc; 1use std::sync::Arc;
2use crate::{ 2use crate::{
3 FileId, 3 FileId,
4 db::{ 4 db::{SyntaxDatabase},
5 Query, QueryRegistry, QueryCtx,
6 file_set
7 },
8 queries::file_syntax,
9 descriptors::{ModuleDescriptor, ModuleTreeDescriptor}, 5 descriptors::{ModuleDescriptor, ModuleTreeDescriptor},
10}; 6};
11 7
12pub(crate) fn register_queries(reg: &mut QueryRegistry) { 8salsa::query_group! {
13 reg.add(MODULE_DESCR, "MODULE_DESCR"); 9 pub(crate) trait ModulesDatabase: SyntaxDatabase {
14 reg.add(MODULE_TREE, "MODULE_TREE"); 10 fn module_tree(key: ()) -> Arc<ModuleTreeDescriptor> {
15} 11 type ModuleTreeQuery;
16
17pub(crate) fn module_tree(ctx: QueryCtx) -> Arc<ModuleTreeDescriptor> {
18 ctx.get(MODULE_TREE, ())
19}
20
21const MODULE_DESCR: Query<FileId, ModuleDescriptor> = Query(30, |ctx, &file_id| {
22 let file = file_syntax(ctx, file_id);
23 ModuleDescriptor::new(file.ast())
24});
25
26const MODULE_TREE: Query<(), ModuleTreeDescriptor> = Query(31, |ctx, _| {
27 let file_set = file_set(ctx);
28 let mut files = Vec::new();
29 for &file_id in file_set.0.iter() {
30 let module_descr = ctx.get(MODULE_DESCR, file_id);
31 files.push((file_id, module_descr));
32 }
33 ModuleTreeDescriptor::new(files.iter().map(|(file_id, descr)| (*file_id, &**descr)), &file_set.1)
34});
35
36#[cfg(test)]
37mod tests {
38 use std::collections::HashMap;
39 use im;
40 use relative_path::{RelativePath, RelativePathBuf};
41 use crate::{
42 db::{Db},
43 imp::FileResolverImp,
44 FileId, FileResolver,
45 };
46 use super::*;
47
48 #[derive(Debug)]
49 struct FileMap(im::HashMap<FileId, RelativePathBuf>);
50
51 impl FileResolver for FileMap {
52 fn file_stem(&self, file_id: FileId) -> String {
53 self.0[&file_id].file_stem().unwrap().to_string()
54 } 12 }
55 fn resolve(&self, file_id: FileId, rel: &RelativePath) -> Option<FileId> { 13 fn module_descriptor(file_id: FileId) -> Arc<ModuleDescriptor> {
56 let path = self.0[&file_id].join(rel).normalize(); 14 type ModuleDescriptorQuery;
57 self.0.iter()
58 .filter_map(|&(id, ref p)| Some(id).filter(|_| p == &path))
59 .next()
60 } 15 }
61 } 16 }
17}
62 18
63 struct Fixture {
64 next_file_id: u32,
65 fm: im::HashMap<FileId, RelativePathBuf>,
66 db: Db,
67 }
68
69 impl Fixture {
70 fn new() -> Fixture {
71 Fixture {
72 next_file_id: 1,
73 fm: im::HashMap::new(),
74 db: Db::new(),
75 }
76 }
77 fn add_file(&mut self, path: &str, text: &str) -> FileId {
78 assert!(path.starts_with("/"));
79 let file_id = FileId(self.next_file_id);
80 self.next_file_id += 1;
81 self.fm.insert(file_id, RelativePathBuf::from(&path[1..]));
82 let mut new_state = self.db.state().clone();
83 new_state.file_map.insert(file_id, Arc::new(text.to_string()));
84 new_state.file_resolver = FileResolverImp::new(
85 Arc::new(FileMap(self.fm.clone()))
86 );
87 self.db = self.db.with_changes(new_state, &[file_id], true);
88 file_id
89 }
90 fn remove_file(&mut self, file_id: FileId) {
91 self.fm.remove(&file_id);
92 let mut new_state = self.db.state().clone();
93 new_state.file_map.remove(&file_id);
94 new_state.file_resolver = FileResolverImp::new(
95 Arc::new(FileMap(self.fm.clone()))
96 );
97 self.db = self.db.with_changes(new_state, &[file_id], true);
98 }
99 fn change_file(&mut self, file_id: FileId, new_text: &str) {
100 let mut new_state = self.db.state().clone();
101 new_state.file_map.insert(file_id, Arc::new(new_text.to_string()));
102 self.db = self.db.with_changes(new_state, &[file_id], false);
103 }
104 fn check_parent_modules(
105 &self,
106 file_id: FileId,
107 expected: &[FileId],
108 queries: &[(&'static str, u64)]
109 ) {
110 let (tree, events) = self.db.trace_query(|ctx| module_tree(ctx));
111 let actual = tree.parent_modules(file_id)
112 .into_iter()
113 .map(|link| link.owner(&tree))
114 .collect::<Vec<_>>();
115 assert_eq!(actual.as_slice(), expected);
116 let mut counts = HashMap::new();
117 events.into_iter()
118 .for_each(|event| *counts.entry(event).or_insert(0) += 1);
119 for &(query_id, expected_count) in queries.iter() {
120 let actual_count = *counts.get(&query_id).unwrap_or(&0);
121 assert_eq!(
122 actual_count,
123 expected_count,
124 "counts for {} differ",
125 query_id,
126 )
127 }
128
129 }
130 }
131
132 #[test]
133 fn test_parent_module() {
134 let mut f = Fixture::new();
135 let foo = f.add_file("/foo.rs", "");
136 f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
137
138 let lib = f.add_file("/lib.rs", "mod foo;");
139 f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
140 f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 0)]);
141
142 f.change_file(lib, "");
143 f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
144
145 f.change_file(lib, "mod foo;");
146 f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
147
148 f.change_file(lib, "mod bar;");
149 f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
150 19
151 f.change_file(lib, "mod foo;"); 20fn module_descriptor(db: &impl ModulesDatabase, file_id: FileId) -> Arc<ModuleDescriptor> {
152 f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]); 21 let file = db.file_syntax(file_id);
22 Arc::new(ModuleDescriptor::new(file.ast()))
23}
153 24
154 f.remove_file(lib); 25fn module_tree(db: &impl ModulesDatabase, (): ()) -> Arc<ModuleTreeDescriptor> {
155 f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 0)]); 26 let file_set = db.file_set(());
27 let mut files = Vec::new();
28 for &file_id in file_set.files.iter() {
29 let module_descr = db.module_descriptor(file_id);
30 files.push((file_id, module_descr));
156 } 31 }
32 let res = ModuleTreeDescriptor::new(files.iter().map(|(file_id, descr)| (*file_id, &**descr)), &file_set.resolver);
33 Arc::new(res)
157} 34}
35
36// #[cfg(test)]
37// mod tests {
38// use std::collections::HashMap;
39// use im;
40// use relative_path::{RelativePath, RelativePathBuf};
41// use {
42// db::{Db},
43// imp::FileResolverImp,
44// FileId, FileResolver,
45// };
46// use super::*;
47
48// #[derive(Debug)]
49// struct FileMap(im::HashMap<FileId, RelativePathBuf>);
50
51// impl FileResolver for FileMap {
52// fn file_stem(&self, file_id: FileId) -> String {
53// self.0[&file_id].file_stem().unwrap().to_string()
54// }
55// fn resolve(&self, file_id: FileId, rel: &RelativePath) -> Option<FileId> {
56// let path = self.0[&file_id].join(rel).normalize();
57// self.0.iter()
58// .filter_map(|&(id, ref p)| Some(id).filter(|_| p == &path))
59// .next()
60// }
61// }
62
63// struct Fixture {
64// next_file_id: u32,
65// fm: im::HashMap<FileId, RelativePathBuf>,
66// db: Db,
67// }
68
69// impl Fixture {
70// fn new() -> Fixture {
71// Fixture {
72// next_file_id: 1,
73// fm: im::HashMap::new(),
74// db: Db::new(),
75// }
76// }
77// fn add_file(&mut self, path: &str, text: &str) -> FileId {
78// assert!(path.starts_with("/"));
79// let file_id = FileId(self.next_file_id);
80// self.next_file_id += 1;
81// self.fm.insert(file_id, RelativePathBuf::from(&path[1..]));
82// let mut new_state = self.db.state().clone();
83// new_state.file_map.insert(file_id, Arc::new(text.to_string()));
84// new_state.file_resolver = FileResolverImp::new(
85// Arc::new(FileMap(self.fm.clone()))
86// );
87// self.db = self.db.with_changes(new_state, &[file_id], true);
88// file_id
89// }
90// fn remove_file(&mut self, file_id: FileId) {
91// self.fm.remove(&file_id);
92// let mut new_state = self.db.state().clone();
93// new_state.file_map.remove(&file_id);
94// new_state.file_resolver = FileResolverImp::new(
95// Arc::new(FileMap(self.fm.clone()))
96// );
97// self.db = self.db.with_changes(new_state, &[file_id], true);
98// }
99// fn change_file(&mut self, file_id: FileId, new_text: &str) {
100// let mut new_state = self.db.state().clone();
101// new_state.file_map.insert(file_id, Arc::new(new_text.to_string()));
102// self.db = self.db.with_changes(new_state, &[file_id], false);
103// }
104// fn check_parent_modules(
105// &self,
106// file_id: FileId,
107// expected: &[FileId],
108// queries: &[(&'static str, u64)]
109// ) {
110// let (tree, events) = self.db.trace_query(|ctx| module_tree(ctx));
111// let actual = tree.parent_modules(file_id)
112// .into_iter()
113// .map(|link| link.owner(&tree))
114// .collect::<Vec<_>>();
115// assert_eq!(actual.as_slice(), expected);
116// let mut counts = HashMap::new();
117// events.into_iter()
118// .for_each(|event| *counts.entry(event).or_insert(0) += 1);
119// for &(query_id, expected_count) in queries.iter() {
120// let actual_count = *counts.get(&query_id).unwrap_or(&0);
121// assert_eq!(
122// actual_count,
123// expected_count,
124// "counts for {} differ",
125// query_id,
126// )
127// }
128
129// }
130// }
131
132// #[test]
133// fn test_parent_module() {
134// let mut f = Fixture::new();
135// let foo = f.add_file("/foo.rs", "");
136// f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
137
138// let lib = f.add_file("/lib.rs", "mod foo;");
139// f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
140// f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 0)]);
141
142// f.change_file(lib, "");
143// f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
144
145// f.change_file(lib, "mod foo;");
146// f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
147
148// f.change_file(lib, "mod bar;");
149// f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 1)]);
150
151// f.change_file(lib, "mod foo;");
152// f.check_parent_modules(foo, &[lib], &[("MODULE_DESCR", 1)]);
153
154// f.remove_file(lib);
155// f.check_parent_modules(foo, &[], &[("MODULE_DESCR", 0)]);
156// }
157// }
diff --git a/crates/ra_analysis/src/roots.rs b/crates/ra_analysis/src/roots.rs
index 7a7d1169e..908f49b0a 100644
--- a/crates/ra_analysis/src/roots.rs
+++ b/crates/ra_analysis/src/roots.rs
@@ -16,7 +16,8 @@ use crate::{
16 imp::FileResolverImp, 16 imp::FileResolverImp,
17 symbol_index::SymbolIndex, 17 symbol_index::SymbolIndex,
18 descriptors::{ModuleDescriptor, ModuleTreeDescriptor}, 18 descriptors::{ModuleDescriptor, ModuleTreeDescriptor},
19 db::{self, FilesDatabase, SyntaxDatabase} 19 db::{self, FilesDatabase, SyntaxDatabase},
20 module_map::ModulesDatabase,
20}; 21};
21 22
22pub(crate) trait SourceRoot { 23pub(crate) trait SourceRoot {
@@ -53,17 +54,17 @@ impl WritableSourceRoot {
53 } 54 }
54 } 55 }
55 } 56 }
56 if let Some(resolver) = file_resolver { 57 let file_set = db.file_set(());
57 let mut files: HashSet<FileId> = db.file_set(()) 58 let mut files: HashSet<FileId> = file_set
58 .files 59 .files
59 .clone(); 60 .clone();
60 for file_id in removed { 61 for file_id in removed {
61 files.remove(&file_id); 62 files.remove(&file_id);
62 }
63 files.extend(changed);
64 db.query(db::FileSetQuery)
65 .set((), Arc::new(db::FileSet { files, resolver }))
66 } 63 }
64 files.extend(changed);
65 let resolver = file_resolver.unwrap_or_else(|| file_set.resolver.clone());
66 db.query(db::FileSetQuery)
67 .set((), Arc::new(db::FileSet { files, resolver }));
67 // TODO: reconcile sasla's API with our needs 68 // TODO: reconcile sasla's API with our needs
68 // https://github.com/salsa-rs/salsa/issues/12 69 // https://github.com/salsa-rs/salsa/issues/12
69 self.clone() 70 self.clone()
@@ -72,12 +73,13 @@ impl WritableSourceRoot {
72 73
73impl SourceRoot for WritableSourceRoot { 74impl SourceRoot for WritableSourceRoot {
74 fn module_tree(&self) -> Arc<ModuleTreeDescriptor> { 75 fn module_tree(&self) -> Arc<ModuleTreeDescriptor> {
75 unimplemented!() 76 self.db.read().module_tree(())
76 //self.db.make_query(::module_map::module_tree)
77 } 77 }
78
79 fn contains(&self, file_id: FileId) -> bool { 78 fn contains(&self, file_id: FileId) -> bool {
80 self.db.read().file_set(()).files.contains(&file_id) 79 let db = self.db.read();
80 let files = &db.file_set(()).files;
81 eprintln!("files = {:?}", files);
82 files.contains(&file_id)
81 } 83 }
82 fn lines(&self, file_id: FileId) -> Arc<LineIndex> { 84 fn lines(&self, file_id: FileId) -> Arc<LineIndex> {
83 self.db.read().file_lines(file_id) 85 self.db.read().file_lines(file_id)