aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src')
-rw-r--r--crates/ra_analysis/src/completion.rs4
-rw-r--r--crates/ra_analysis/src/db.rs58
-rw-r--r--crates/ra_analysis/src/descriptors/module/imp.rs24
-rw-r--r--crates/ra_analysis/src/descriptors/module/mod.rs9
-rw-r--r--crates/ra_analysis/src/imp.rs184
-rw-r--r--crates/ra_analysis/src/input.rs79
-rw-r--r--crates/ra_analysis/src/lib.rs99
-rw-r--r--crates/ra_analysis/src/roots.rs116
-rw-r--r--crates/ra_analysis/src/symbol_index.rs2
9 files changed, 278 insertions, 297 deletions
diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs
index a0fd6828d..0a2f99575 100644
--- a/crates/ra_analysis/src/completion.rs
+++ b/crates/ra_analysis/src/completion.rs
@@ -6,13 +6,15 @@ use ra_syntax::{
6 6
7use crate::{ 7use crate::{
8 FileId, Cancelable, 8 FileId, Cancelable,
9 input::FilesDatabase,
9 db::{self, SyntaxDatabase}, 10 db::{self, SyntaxDatabase},
10 descriptors::module::{ModulesDatabase, ModuleTree, ModuleId}, 11 descriptors::module::{ModulesDatabase, ModuleTree, ModuleId},
11}; 12};
12 13
13pub(crate) fn resolve_based_completion(db: &db::RootDatabase, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> { 14pub(crate) fn resolve_based_completion(db: &db::RootDatabase, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> {
15 let source_root_id = db.file_source_root(file_id);
14 let file = db.file_syntax(file_id); 16 let file = db.file_syntax(file_id);
15 let module_tree = db.module_tree()?; 17 let module_tree = db.module_tree(source_root_id)?;
16 let file = { 18 let file = {
17 let edit = AtomEdit::insert(offset, "intellijRulezz".to_string()); 19 let edit = AtomEdit::insert(offset, "intellijRulezz".to_string());
18 file.reparse(&edit) 20 file.reparse(&edit)
diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs
index b527cde61..3ca14af79 100644
--- a/crates/ra_analysis/src/db.rs
+++ b/crates/ra_analysis/src/db.rs
@@ -1,12 +1,9 @@
1use std::{ 1use std::{
2 fmt,
3 hash::{Hash, Hasher},
4 sync::Arc, 2 sync::Arc,
5}; 3};
6 4
7use ra_editor::LineIndex; 5use ra_editor::LineIndex;
8use ra_syntax::File; 6use ra_syntax::File;
9use rustc_hash::FxHashSet;
10use salsa; 7use salsa;
11 8
12use crate::{ 9use crate::{
@@ -14,20 +11,14 @@ use crate::{
14 Cancelable, Canceled, 11 Cancelable, Canceled,
15 descriptors::module::{SubmodulesQuery, ModuleTreeQuery, ModulesDatabase}, 12 descriptors::module::{SubmodulesQuery, ModuleTreeQuery, ModulesDatabase},
16 symbol_index::SymbolIndex, 13 symbol_index::SymbolIndex,
17 FileId, FileResolverImp, 14 FileId,
18}; 15};
19 16
20#[derive(Default)] 17#[derive(Default, Debug)]
21pub(crate) struct RootDatabase { 18pub(crate) struct RootDatabase {
22 runtime: salsa::Runtime<RootDatabase>, 19 runtime: salsa::Runtime<RootDatabase>,
23} 20}
24 21
25impl fmt::Debug for RootDatabase {
26 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
27 fmt.write_str("RootDatabase { ... }")
28 }
29}
30
31impl salsa::Database for RootDatabase { 22impl salsa::Database for RootDatabase {
32 fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> { 23 fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
33 &self.runtime 24 &self.runtime
@@ -58,9 +49,13 @@ impl Clone for RootDatabase {
58 49
59salsa::database_storage! { 50salsa::database_storage! {
60 pub(crate) struct RootDatabaseStorage for RootDatabase { 51 pub(crate) struct RootDatabaseStorage for RootDatabase {
61 impl FilesDatabase { 52 impl crate::input::FilesDatabase {
62 fn file_text() for FileTextQuery; 53 fn file_text() for crate::input::FileTextQuery;
63 fn file_set() for FileSetQuery; 54 fn file_source_root() for crate::input::FileSourceRootQuery;
55 fn source_root() for crate::input::SourceRootQuery;
56 fn libraries() for crate::input::LibrarieseQuery;
57 fn library_symbols() for crate::input::LibrarySymbolsQuery;
58 fn crate_graph() for crate::input::CrateGraphQuery;
64 } 59 }
65 impl SyntaxDatabase { 60 impl SyntaxDatabase {
66 fn file_syntax() for FileSyntaxQuery; 61 fn file_syntax() for FileSyntaxQuery;
@@ -75,40 +70,7 @@ salsa::database_storage! {
75} 70}
76 71
77salsa::query_group! { 72salsa::query_group! {
78 pub(crate) trait FilesDatabase: salsa::Database { 73 pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase {
79 fn file_text(file_id: FileId) -> Arc<String> {
80 type FileTextQuery;
81 storage input;
82 }
83 fn file_set() -> Arc<FileSet> {
84 type FileSetQuery;
85 storage input;
86 }
87 }
88}
89
90#[derive(Default, Debug, Eq)]
91pub(crate) struct FileSet {
92 pub(crate) files: FxHashSet<FileId>,
93 pub(crate) resolver: FileResolverImp,
94}
95
96impl PartialEq for FileSet {
97 fn eq(&self, other: &FileSet) -> bool {
98 self.files == other.files && self.resolver == other.resolver
99 }
100}
101
102impl Hash for FileSet {
103 fn hash<H: Hasher>(&self, hasher: &mut H) {
104 let mut files = self.files.iter().cloned().collect::<Vec<_>>();
105 files.sort();
106 files.hash(hasher);
107 }
108}
109
110salsa::query_group! {
111 pub(crate) trait SyntaxDatabase: FilesDatabase {
112 fn file_syntax(file_id: FileId) -> File { 74 fn file_syntax(file_id: FileId) -> File {
113 type FileSyntaxQuery; 75 type FileSyntaxQuery;
114 } 76 }
diff --git a/crates/ra_analysis/src/descriptors/module/imp.rs b/crates/ra_analysis/src/descriptors/module/imp.rs
index 22e4bd785..aecf6e29a 100644
--- a/crates/ra_analysis/src/descriptors/module/imp.rs
+++ b/crates/ra_analysis/src/descriptors/module/imp.rs
@@ -8,8 +8,8 @@ use ra_syntax::{
8}; 8};
9 9
10use crate::{ 10use crate::{
11 FileId, Cancelable, FileResolverImp, 11 FileId, Cancelable, FileResolverImp, db,
12 db, 12 input::{SourceRoot, SourceRootId},
13}; 13};
14 14
15use super::{ 15use super::{
@@ -35,9 +35,12 @@ pub(super) fn modules(root: ast::Root<'_>) -> impl Iterator<Item = (SmolStr, ast
35 }) 35 })
36} 36}
37 37
38pub(super) fn module_tree(db: &impl ModulesDatabase) -> Cancelable<Arc<ModuleTree>> { 38pub(super) fn module_tree(
39 db: &impl ModulesDatabase,
40 source_root: SourceRootId,
41) -> Cancelable<Arc<ModuleTree>> {
39 db::check_canceled(db)?; 42 db::check_canceled(db)?;
40 let res = create_module_tree(db)?; 43 let res = create_module_tree(db, source_root)?;
41 Ok(Arc::new(res)) 44 Ok(Arc::new(res))
42} 45}
43 46
@@ -50,6 +53,7 @@ pub struct Submodule {
50 53
51fn create_module_tree<'a>( 54fn create_module_tree<'a>(
52 db: &impl ModulesDatabase, 55 db: &impl ModulesDatabase,
56 source_root: SourceRootId,
53) -> Cancelable<ModuleTree> { 57) -> Cancelable<ModuleTree> {
54 let mut tree = ModuleTree { 58 let mut tree = ModuleTree {
55 mods: Vec::new(), 59 mods: Vec::new(),
@@ -59,12 +63,13 @@ fn create_module_tree<'a>(
59 let mut roots = FxHashMap::default(); 63 let mut roots = FxHashMap::default();
60 let mut visited = FxHashSet::default(); 64 let mut visited = FxHashSet::default();
61 65
62 for &file_id in db.file_set().files.iter() { 66 let source_root = db.source_root(source_root);
67 for &file_id in source_root.files.iter() {
63 if visited.contains(&file_id) { 68 if visited.contains(&file_id) {
64 continue; // TODO: use explicit crate_roots here 69 continue; // TODO: use explicit crate_roots here
65 } 70 }
66 assert!(!roots.contains_key(&file_id)); 71 assert!(!roots.contains_key(&file_id));
67 let module_id = build_subtree(db, &mut tree, &mut visited, &mut roots, None, file_id)?; 72 let module_id = build_subtree(db, &source_root, &mut tree, &mut visited, &mut roots, None, file_id)?;
68 roots.insert(file_id, module_id); 73 roots.insert(file_id, module_id);
69 } 74 }
70 Ok(tree) 75 Ok(tree)
@@ -72,6 +77,7 @@ fn create_module_tree<'a>(
72 77
73fn build_subtree( 78fn build_subtree(
74 db: &impl ModulesDatabase, 79 db: &impl ModulesDatabase,
80 source_root: &SourceRoot,
75 tree: &mut ModuleTree, 81 tree: &mut ModuleTree,
76 visited: &mut FxHashSet<FileId>, 82 visited: &mut FxHashSet<FileId>,
77 roots: &mut FxHashMap<FileId, ModuleId>, 83 roots: &mut FxHashMap<FileId, ModuleId>,
@@ -84,10 +90,8 @@ fn build_subtree(
84 parent, 90 parent,
85 children: Vec::new(), 91 children: Vec::new(),
86 }); 92 });
87 let file_set = db.file_set();
88 let file_resolver = &file_set.resolver;
89 for name in db.submodules(file_id)?.iter() { 93 for name in db.submodules(file_id)?.iter() {
90 let (points_to, problem) = resolve_submodule(file_id, name, file_resolver); 94 let (points_to, problem) = resolve_submodule(file_id, name, &source_root.file_resolver);
91 let link = tree.push_link(LinkData { 95 let link = tree.push_link(LinkData {
92 name: name.clone(), 96 name: name.clone(),
93 owner: id, 97 owner: id,
@@ -102,7 +106,7 @@ fn build_subtree(
102 tree.module_mut(module_id).parent = Some(link); 106 tree.module_mut(module_id).parent = Some(link);
103 Ok(module_id) 107 Ok(module_id)
104 } 108 }
105 None => build_subtree(db, tree, visited, roots, Some(link), file_id), 109 None => build_subtree(db, source_root, tree, visited, roots, Some(link), file_id),
106 }) 110 })
107 .collect::<Cancelable<Vec<_>>>()?; 111 .collect::<Cancelable<Vec<_>>>()?;
108 tree.link_mut(link).points_to = points_to; 112 tree.link_mut(link).points_to = points_to;
diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs
index 52da650b3..8968c4afd 100644
--- a/crates/ra_analysis/src/descriptors/module/mod.rs
+++ b/crates/ra_analysis/src/descriptors/module/mod.rs
@@ -8,11 +8,12 @@ use ra_syntax::{ast::{self, NameOwner, AstNode}, SmolStr, SyntaxNode};
8use crate::{ 8use crate::{
9 FileId, Cancelable, 9 FileId, Cancelable,
10 db::SyntaxDatabase, 10 db::SyntaxDatabase,
11 input::SourceRootId,
11}; 12};
12 13
13salsa::query_group! { 14salsa::query_group! {
14 pub(crate) trait ModulesDatabase: SyntaxDatabase { 15 pub(crate) trait ModulesDatabase: SyntaxDatabase {
15 fn module_tree() -> Cancelable<Arc<ModuleTree>> { 16 fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
16 type ModuleTreeQuery; 17 type ModuleTreeQuery;
17 use fn imp::module_tree; 18 use fn imp::module_tree;
18 } 19 }
@@ -110,15 +111,9 @@ impl ModuleId {
110} 111}
111 112
112impl LinkId { 113impl LinkId {
113 pub(crate) fn name(self, tree: &ModuleTree) -> SmolStr {
114 tree.link(self).name.clone()
115 }
116 pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId { 114 pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId {
117 tree.link(self).owner 115 tree.link(self).owner
118 } 116 }
119 fn points_to(self, tree: &ModuleTree) -> &[ModuleId] {
120 &tree.link(self).points_to
121 }
122 pub(crate) fn bind_source<'a>( 117 pub(crate) fn bind_source<'a>(
123 self, 118 self,
124 tree: &ModuleTree, 119 tree: &ModuleTree,
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs
index f3e5b2887..5a6e2450d 100644
--- a/crates/ra_analysis/src/imp.rs
+++ b/crates/ra_analysis/src/imp.rs
@@ -1,7 +1,5 @@
1use std::{ 1use std::{
2 fmt,
3 hash::{Hash, Hasher}, 2 hash::{Hash, Hasher},
4 iter,
5 sync::Arc, 3 sync::Arc,
6}; 4};
7 5
@@ -14,12 +12,17 @@ use ra_syntax::{
14}; 12};
15use relative_path::RelativePath; 13use relative_path::RelativePath;
16use rustc_hash::FxHashSet; 14use rustc_hash::FxHashSet;
15use salsa::{ParallelDatabase, Database};
17 16
18use crate::{ 17use crate::{
19 db::SyntaxDatabase, 18 AnalysisChange,
19 db::{
20 self, SyntaxDatabase,
21
22 },
23 input::{SourceRootId, FilesDatabase, SourceRoot, WORKSPACE},
20 descriptors::module::{ModulesDatabase, ModuleTree, Problem}, 24 descriptors::module::{ModulesDatabase, ModuleTree, Problem},
21 descriptors::{FnDescriptor}, 25 descriptors::{FnDescriptor},
22 roots::{ReadonlySourceRoot, SourceRoot, WritableSourceRoot},
23 CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position, 26 CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position,
24 Query, SourceChange, SourceFileEdit, Cancelable, 27 Query, SourceChange, SourceFileEdit, Cancelable,
25}; 28};
@@ -80,96 +83,128 @@ impl Default for FileResolverImp {
80 } 83 }
81} 84}
82 85
83#[derive(Debug)] 86#[derive(Debug, Default)]
84pub(crate) struct AnalysisHostImpl { 87pub(crate) struct AnalysisHostImpl {
85 data: WorldData, 88 db: db::RootDatabase,
86} 89}
87 90
91
88impl AnalysisHostImpl { 92impl AnalysisHostImpl {
89 pub fn new() -> AnalysisHostImpl { 93 pub fn new() -> AnalysisHostImpl {
90 AnalysisHostImpl { 94 AnalysisHostImpl::default()
91 data: WorldData::default(),
92 }
93 } 95 }
94 pub fn analysis(&self) -> AnalysisImpl { 96 pub fn analysis(&self) -> AnalysisImpl {
95 AnalysisImpl { 97 AnalysisImpl {
96 data: self.data.clone(), 98 db: self.db.fork() // freeze revision here
97 } 99 }
98 } 100 }
99 pub fn change_files(&mut self, changes: &mut dyn Iterator<Item = (FileId, Option<String>)>) { 101 pub fn apply_change(&mut self, change: AnalysisChange) {
100 self.data_mut().root.apply_changes(changes, None); 102 log::info!("apply_change {:?}", change);
101 } 103
102 pub fn set_file_resolver(&mut self, resolver: FileResolverImp) { 104 for (file_id, text) in change.files_changed {
103 self.data_mut() 105 self.db
104 .root 106 .query(crate::input::FileTextQuery)
105 .apply_changes(&mut iter::empty(), Some(resolver)); 107 .set(file_id, Arc::new(text))
106 } 108 }
107 pub fn set_crate_graph(&mut self, graph: CrateGraph) { 109 if !(change.files_added.is_empty() && change.files_removed.is_empty()) {
108 let mut visited = FxHashSet::default(); 110 let file_resolver = change.file_resolver
109 for &file_id in graph.crate_roots.values() { 111 .expect("change resolver when changing set of files");
110 if !visited.insert(file_id) { 112 let mut source_root = SourceRoot::clone(&self.db.source_root(WORKSPACE));
111 panic!("duplicate crate root: {:?}", file_id); 113 for (file_id, text) in change.files_added {
114 self.db
115 .query(crate::input::FileTextQuery)
116 .set(file_id, Arc::new(text));
117 self.db
118 .query(crate::input::FileSourceRootQuery)
119 .set(file_id, crate::input::WORKSPACE);
120 source_root.files.insert(file_id);
121 }
122 for file_id in change.files_removed {
123 self.db
124 .query(crate::input::FileTextQuery)
125 .set(file_id, Arc::new(String::new()));
126 source_root.files.remove(&file_id);
112 } 127 }
128 source_root.file_resolver = file_resolver;
129 self.db
130 .query(crate::input::SourceRootQuery)
131 .set(WORKSPACE, Arc::new(source_root))
132 }
133 if !change.libraries_added.is_empty() {
134 let mut libraries = Vec::clone(&self.db.libraries());
135 for library in change.libraries_added {
136 let source_root_id = SourceRootId(1 + libraries.len() as u32);
137 libraries.push(source_root_id);
138 let mut files = FxHashSet::default();
139 for (file_id, text) in library.files {
140 files.insert(file_id);
141 self.db
142 .query(crate::input::FileSourceRootQuery)
143 .set_constant(file_id, source_root_id);
144 self.db
145 .query(crate::input::FileTextQuery)
146 .set_constant(file_id, Arc::new(text));
147 }
148 let source_root = SourceRoot {
149 files,
150 file_resolver: library.file_resolver,
151 };
152 self.db
153 .query(crate::input::SourceRootQuery)
154 .set(source_root_id, Arc::new(source_root));
155 self.db
156 .query(crate::input::LibrarySymbolsQuery)
157 .set(source_root_id, Arc::new(library.symbol_index));
158 }
159 self.db
160 .query(crate::input::LibrarieseQuery)
161 .set((), Arc::new(libraries));
162 }
163 if let Some(crate_graph) = change.crate_graph {
164 self.db.query(crate::input::CrateGraphQuery)
165 .set((), Arc::new(crate_graph))
113 } 166 }
114 self.data_mut().crate_graph = graph;
115 }
116 pub fn add_library(&mut self, root: ReadonlySourceRoot) {
117 self.data_mut().libs.push(root);
118 }
119 fn data_mut(&mut self) -> &mut WorldData {
120 &mut self.data
121 } 167 }
122} 168}
123 169
170#[derive(Debug)]
124pub(crate) struct AnalysisImpl { 171pub(crate) struct AnalysisImpl {
125 data: WorldData, 172 db: db::RootDatabase,
126}
127
128impl fmt::Debug for AnalysisImpl {
129 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
130 self.data.fmt(f)
131 }
132} 173}
133 174
134impl AnalysisImpl { 175impl AnalysisImpl {
135 fn root(&self, file_id: FileId) -> &SourceRoot {
136 if self.data.root.contains(file_id) {
137 return &self.data.root;
138 }
139 self
140 .data
141 .libs
142 .iter()
143 .find(|it| it.contains(file_id))
144 .unwrap()
145 }
146 pub fn file_syntax(&self, file_id: FileId) -> File { 176 pub fn file_syntax(&self, file_id: FileId) -> File {
147 self.root(file_id).db().file_syntax(file_id) 177 self.db.file_syntax(file_id)
148 } 178 }
149 pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> { 179 pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
150 self.root(file_id).db().file_lines(file_id) 180 self.db.file_lines(file_id)
151 } 181 }
152 pub fn world_symbols(&self, query: Query) -> Cancelable<Vec<(FileId, FileSymbol)>> { 182 pub fn world_symbols(&self, query: Query) -> Cancelable<Vec<(FileId, FileSymbol)>> {
153 let mut buf = Vec::new(); 183 let mut buf = Vec::new();
154 if query.libs { 184 if query.libs {
155 for lib in self.data.libs.iter() { 185 for &lib_id in self.db.libraries().iter() {
156 lib.symbols(&mut buf)?; 186 buf.push(self.db.library_symbols(lib_id));
157 } 187 }
158 } else { 188 } else {
159 self.data.root.symbols(&mut buf)?; 189 for &file_id in self.db.source_root(WORKSPACE).files.iter() {
190 buf.push(self.db.file_symbols(file_id)?);
191 }
160 } 192 }
161 Ok(query.search(&buf)) 193 Ok(query.search(&buf))
162 } 194 }
195 fn module_tree(&self, file_id: FileId) -> Cancelable<Arc<ModuleTree>> {
196 let source_root = self.db.file_source_root(file_id);
197 self.db.module_tree(source_root)
198 }
163 pub fn parent_module(&self, file_id: FileId) -> Cancelable<Vec<(FileId, FileSymbol)>> { 199 pub fn parent_module(&self, file_id: FileId) -> Cancelable<Vec<(FileId, FileSymbol)>> {
164 let root = self.root(file_id); 200 let module_tree = self.module_tree(file_id)?;
165 let module_tree = root.db().module_tree()?;
166 201
167 let res = module_tree.modules_for_file(file_id) 202 let res = module_tree.modules_for_file(file_id)
168 .into_iter() 203 .into_iter()
169 .filter_map(|module_id| { 204 .filter_map(|module_id| {
170 let link = module_id.parent_link(&module_tree)?; 205 let link = module_id.parent_link(&module_tree)?;
171 let file_id = link.owner(&module_tree).file_id(&module_tree); 206 let file_id = link.owner(&module_tree).file_id(&module_tree);
172 let syntax = root.db().file_syntax(file_id); 207 let syntax = self.db.file_syntax(file_id);
173 let decl = link.bind_source(&module_tree, syntax.ast()); 208 let decl = link.bind_source(&module_tree, syntax.ast());
174 209
175 let sym = FileSymbol { 210 let sym = FileSymbol {
@@ -183,8 +218,8 @@ impl AnalysisImpl {
183 Ok(res) 218 Ok(res)
184 } 219 }
185 pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { 220 pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
186 let module_tree = self.root(file_id).db().module_tree()?; 221 let module_tree = self.module_tree(file_id)?;
187 let crate_graph = &self.data.crate_graph; 222 let crate_graph = self.db.crate_graph();
188 let res = module_tree.modules_for_file(file_id) 223 let res = module_tree.modules_for_file(file_id)
189 .into_iter() 224 .into_iter()
190 .map(|it| it.root(&module_tree)) 225 .map(|it| it.root(&module_tree))
@@ -195,7 +230,7 @@ impl AnalysisImpl {
195 Ok(res) 230 Ok(res)
196 } 231 }
197 pub fn crate_root(&self, crate_id: CrateId) -> FileId { 232 pub fn crate_root(&self, crate_id: CrateId) -> FileId {
198 self.data.crate_graph.crate_roots[&crate_id] 233 self.db.crate_graph().crate_roots[&crate_id]
199 } 234 }
200 pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> { 235 pub fn completions(&self, file_id: FileId, offset: TextUnit) -> Cancelable<Option<Vec<CompletionItem>>> {
201 let mut res = Vec::new(); 236 let mut res = Vec::new();
@@ -205,8 +240,7 @@ impl AnalysisImpl {
205 res.extend(scope_based); 240 res.extend(scope_based);
206 has_completions = true; 241 has_completions = true;
207 } 242 }
208 let root = self.root(file_id); 243 if let Some(scope_based) = crate::completion::resolve_based_completion(&self.db, file_id, offset)? {
209 if let Some(scope_based) = crate::completion::resolve_based_completion(root.db(), file_id, offset)? {
210 res.extend(scope_based); 244 res.extend(scope_based);
211 has_completions = true; 245 has_completions = true;
212 } 246 }
@@ -222,9 +256,8 @@ impl AnalysisImpl {
222 file_id: FileId, 256 file_id: FileId,
223 offset: TextUnit, 257 offset: TextUnit,
224 ) -> Cancelable<Vec<(FileId, FileSymbol)>> { 258 ) -> Cancelable<Vec<(FileId, FileSymbol)>> {
225 let root = self.root(file_id); 259 let module_tree = self.module_tree(file_id)?;
226 let module_tree = root.db().module_tree()?; 260 let file = self.db.file_syntax(file_id);
227 let file = root.db().file_syntax(file_id);
228 let syntax = file.syntax(); 261 let syntax = file.syntax();
229 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) { 262 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
230 // First try to resolve the symbol locally 263 // First try to resolve the symbol locally
@@ -273,8 +306,7 @@ impl AnalysisImpl {
273 } 306 }
274 307
275 pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, TextRange)> { 308 pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, TextRange)> {
276 let root = self.root(file_id); 309 let file = self.db.file_syntax(file_id);
277 let file = root.db().file_syntax(file_id);
278 let syntax = file.syntax(); 310 let syntax = file.syntax();
279 311
280 let mut ret = vec![]; 312 let mut ret = vec![];
@@ -305,9 +337,8 @@ impl AnalysisImpl {
305 } 337 }
306 338
307 pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> { 339 pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
308 let root = self.root(file_id); 340 let module_tree = self.module_tree(file_id)?;
309 let module_tree = root.db().module_tree()?; 341 let syntax = self.db.file_syntax(file_id);
310 let syntax = root.db().file_syntax(file_id);
311 342
312 let mut res = ra_editor::diagnostics(&syntax) 343 let mut res = ra_editor::diagnostics(&syntax)
313 .into_iter() 344 .into_iter()
@@ -396,8 +427,7 @@ impl AnalysisImpl {
396 file_id: FileId, 427 file_id: FileId,
397 offset: TextUnit, 428 offset: TextUnit,
398 ) -> Cancelable<Option<(FnDescriptor, Option<usize>)>> { 429 ) -> Cancelable<Option<(FnDescriptor, Option<usize>)>> {
399 let root = self.root(file_id); 430 let file = self.db.file_syntax(file_id);
400 let file = root.db().file_syntax(file_id);
401 let syntax = file.syntax(); 431 let syntax = file.syntax();
402 432
403 // Find the calling expression and it's NameRef 433 // Find the calling expression and it's NameRef
@@ -412,9 +442,10 @@ impl AnalysisImpl {
412 442
413 // Resolve the function's NameRef (NOTE: this isn't entirely accurate). 443 // Resolve the function's NameRef (NOTE: this isn't entirely accurate).
414 let file_symbols = self.index_resolve(name_ref)?; 444 let file_symbols = self.index_resolve(name_ref)?;
415 for (_, fs) in file_symbols { 445 for (fn_fiel_id, fs) in file_symbols {
416 if fs.kind == FN_DEF { 446 if fs.kind == FN_DEF {
417 if let Some(fn_def) = find_node_at_offset(syntax, fs.node_range.start()) { 447 let fn_file = self.db.file_syntax(fn_fiel_id);
448 if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) {
418 if let Some(descriptor) = FnDescriptor::new(fn_def) { 449 if let Some(descriptor) = FnDescriptor::new(fn_def) {
419 // If we have a calling expression let's find which argument we are on 450 // If we have a calling expression let's find which argument we are on
420 let mut current_parameter = None; 451 let mut current_parameter = None;
@@ -491,13 +522,6 @@ impl AnalysisImpl {
491 } 522 }
492} 523}
493 524
494#[derive(Default, Clone, Debug)]
495struct WorldData {
496 crate_graph: CrateGraph,
497 root: WritableSourceRoot,
498 libs: Vec<ReadonlySourceRoot>,
499}
500
501impl SourceChange { 525impl SourceChange {
502 pub(crate) fn from_local_edit(file_id: FileId, label: &str, edit: LocalEdit) -> SourceChange { 526 pub(crate) fn from_local_edit(file_id: FileId, label: &str, edit: LocalEdit) -> SourceChange {
503 let file_edit = SourceFileEdit { 527 let file_edit = SourceFileEdit {
diff --git a/crates/ra_analysis/src/input.rs b/crates/ra_analysis/src/input.rs
new file mode 100644
index 000000000..e64fad40c
--- /dev/null
+++ b/crates/ra_analysis/src/input.rs
@@ -0,0 +1,79 @@
1use std::{
2 sync::Arc,
3 fmt,
4};
5
6use salsa;
7use rustc_hash::FxHashSet;
8use relative_path::RelativePath;
9use rustc_hash::FxHashMap;
10
11use crate::{symbol_index::SymbolIndex, FileResolverImp};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct FileId(pub u32);
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct CrateId(pub u32);
18
19#[derive(Debug, Clone, Default, PartialEq, Eq)]
20pub struct CrateGraph {
21 pub(crate) crate_roots: FxHashMap<CrateId, FileId>,
22}
23
24impl CrateGraph {
25 pub fn new() -> CrateGraph {
26 CrateGraph::default()
27 }
28 pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId{
29 let crate_id = CrateId(self.crate_roots.len() as u32);
30 let prev = self.crate_roots.insert(crate_id, file_id);
31 assert!(prev.is_none());
32 crate_id
33 }
34}
35
36pub trait FileResolver: fmt::Debug + Send + Sync + 'static {
37 fn file_stem(&self, file_id: FileId) -> String;
38 fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>;
39}
40
41salsa::query_group! {
42 pub(crate) trait FilesDatabase: salsa::Database {
43 fn file_text(file_id: FileId) -> Arc<String> {
44 type FileTextQuery;
45 storage input;
46 }
47 fn file_source_root(file_id: FileId) -> SourceRootId {
48 type FileSourceRootQuery;
49 storage input;
50 }
51 fn source_root(id: SourceRootId) -> Arc<SourceRoot> {
52 type SourceRootQuery;
53 storage input;
54 }
55 fn libraries() -> Arc<Vec<SourceRootId>> {
56 type LibrarieseQuery;
57 storage input;
58 }
59 fn library_symbols(id: SourceRootId) -> Arc<SymbolIndex> {
60 type LibrarySymbolsQuery;
61 storage input;
62 }
63 fn crate_graph() -> Arc<CrateGraph> {
64 type CrateGraphQuery;
65 storage input;
66 }
67 }
68}
69
70#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
71pub(crate) struct SourceRootId(pub(crate) u32);
72
73#[derive(Clone, Default, Debug, PartialEq, Eq)]
74pub(crate) struct SourceRoot {
75 pub(crate) file_resolver: FileResolverImp,
76 pub(crate) files: FxHashSet<FileId>,
77}
78
79pub(crate) const WORKSPACE: SourceRootId = SourceRootId(0);
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs
index 7078e2d31..a67cac21e 100644
--- a/crates/ra_analysis/src/lib.rs
+++ b/crates/ra_analysis/src/lib.rs
@@ -6,23 +6,30 @@ extern crate relative_path;
6extern crate rustc_hash; 6extern crate rustc_hash;
7extern crate salsa; 7extern crate salsa;
8 8
9mod input;
9mod db; 10mod db;
10mod descriptors; 11mod descriptors;
11mod imp; 12mod imp;
12mod roots;
13mod symbol_index; 13mod symbol_index;
14mod completion; 14mod completion;
15 15
16use std::{fmt::Debug, sync::Arc}; 16use std::{
17 fmt,
18 sync::Arc,
19};
17 20
18use ra_syntax::{AtomEdit, File, TextRange, TextUnit}; 21use ra_syntax::{AtomEdit, File, TextRange, TextUnit};
19use relative_path::{RelativePath, RelativePathBuf}; 22use relative_path::RelativePathBuf;
20use rustc_hash::FxHashMap; 23use rayon::prelude::*;
21 24
22use crate::imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}; 25use crate::{
26 imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp},
27 symbol_index::SymbolIndex,
28};
23 29
24pub use crate::{ 30pub use crate::{
25 descriptors::FnDescriptor, 31 descriptors::FnDescriptor,
32 input::{FileId, FileResolver, CrateGraph, CrateId}
26}; 33};
27pub use ra_editor::{ 34pub use ra_editor::{
28 CompletionItem, FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, 35 CompletionItem, FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable,
@@ -43,20 +50,52 @@ impl std::fmt::Display for Canceled {
43impl std::error::Error for Canceled { 50impl std::error::Error for Canceled {
44} 51}
45 52
46#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 53#[derive(Default)]
47pub struct FileId(pub u32); 54pub struct AnalysisChange {
48 55 files_added: Vec<(FileId, String)>,
49#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 56 files_changed: Vec<(FileId, String)>,
50pub struct CrateId(pub u32); 57 files_removed: Vec<(FileId)>,
58 libraries_added: Vec<LibraryData>,
59 crate_graph: Option<CrateGraph>,
60 file_resolver: Option<FileResolverImp>,
61}
51 62
52#[derive(Debug, Clone, Default)] 63impl fmt::Debug for AnalysisChange {
53pub struct CrateGraph { 64 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
54 pub crate_roots: FxHashMap<CrateId, FileId>, 65 fmt.debug_struct("AnalysisChange")
66 .field("files_added", &self.files_added.len())
67 .field("files_changed", &self.files_changed.len())
68 .field("files_removed", &self.files_removed.len())
69 .field("libraries_added", &self.libraries_added.len())
70 .field("crate_graph", &self.crate_graph)
71 .field("file_resolver", &self.file_resolver)
72 .finish()
73 }
55} 74}
56 75
57pub trait FileResolver: Debug + Send + Sync + 'static { 76
58 fn file_stem(&self, file_id: FileId) -> String; 77impl AnalysisChange {
59 fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>; 78 pub fn new() -> AnalysisChange {
79 AnalysisChange::default()
80 }
81 pub fn add_file(&mut self, file_id: FileId, text: String) {
82 self.files_added.push((file_id, text))
83 }
84 pub fn change_file(&mut self, file_id: FileId, new_text: String) {
85 self.files_changed.push((file_id, new_text))
86 }
87 pub fn remove_file(&mut self, file_id: FileId) {
88 self.files_removed.push(file_id)
89 }
90 pub fn add_library(&mut self, data: LibraryData) {
91 self.libraries_added.push(data)
92 }
93 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
94 self.crate_graph = Some(graph);
95 }
96 pub fn set_file_resolver(&mut self, file_resolver: Arc<FileResolver>) {
97 self.file_resolver = Some(FileResolverImp::new(file_resolver));
98 }
60} 99}
61 100
62#[derive(Debug)] 101#[derive(Debug)]
@@ -75,20 +114,8 @@ impl AnalysisHost {
75 imp: self.imp.analysis(), 114 imp: self.imp.analysis(),
76 } 115 }
77 } 116 }
78 pub fn change_file(&mut self, file_id: FileId, text: Option<String>) { 117 pub fn apply_change(&mut self, change: AnalysisChange) {
79 self.change_files(::std::iter::once((file_id, text))); 118 self.imp.apply_change(change)
80 }
81 pub fn change_files(&mut self, mut changes: impl Iterator<Item = (FileId, Option<String>)>) {
82 self.imp.change_files(&mut changes)
83 }
84 pub fn set_file_resolver(&mut self, resolver: Arc<FileResolver>) {
85 self.imp.set_file_resolver(FileResolverImp::new(resolver));
86 }
87 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
88 self.imp.set_crate_graph(graph)
89 }
90 pub fn add_library(&mut self, data: LibraryData) {
91 self.imp.add_library(data.root)
92 } 119 }
93} 120}
94 121
@@ -266,14 +293,18 @@ impl Analysis {
266 293
267#[derive(Debug)] 294#[derive(Debug)]
268pub struct LibraryData { 295pub struct LibraryData {
269 root: roots::ReadonlySourceRoot, 296 files: Vec<(FileId, String)>,
297 file_resolver: FileResolverImp,
298 symbol_index: SymbolIndex,
270} 299}
271 300
272impl LibraryData { 301impl LibraryData {
273 pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData { 302 pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData {
274 let file_resolver = FileResolverImp::new(file_resolver); 303 let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, text)| {
275 let root = roots::ReadonlySourceRoot::new(files, file_resolver); 304 let file = File::parse(text);
276 LibraryData { root } 305 (*file_id, file)
306 }));
307 LibraryData { files, file_resolver: FileResolverImp::new(file_resolver), symbol_index }
277 } 308 }
278} 309}
279 310
diff --git a/crates/ra_analysis/src/roots.rs b/crates/ra_analysis/src/roots.rs
deleted file mode 100644
index 1e9e613ac..000000000
--- a/crates/ra_analysis/src/roots.rs
+++ /dev/null
@@ -1,116 +0,0 @@
1use std::{sync::Arc};
2
3use rustc_hash::FxHashSet;
4use rayon::prelude::*;
5use salsa::Database;
6
7use crate::{
8 Cancelable,
9 db::{self, FilesDatabase, SyntaxDatabase},
10 imp::FileResolverImp,
11 symbol_index::SymbolIndex,
12 FileId,
13};
14
15pub(crate) trait SourceRoot {
16 fn contains(&self, file_id: FileId) -> bool;
17 fn db(&self) -> &db::RootDatabase;
18 fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>) -> Cancelable<()>;
19}
20
21#[derive(Default, Debug, Clone)]
22pub(crate) struct WritableSourceRoot {
23 db: db::RootDatabase,
24}
25
26impl WritableSourceRoot {
27 pub fn apply_changes(
28 &mut self,
29 changes: &mut dyn Iterator<Item = (FileId, Option<String>)>,
30 file_resolver: Option<FileResolverImp>,
31 ) {
32 let mut changed = FxHashSet::default();
33 let mut removed = FxHashSet::default();
34 for (file_id, text) in changes {
35 match text {
36 None => {
37 removed.insert(file_id);
38 }
39 Some(text) => {
40 self.db
41 .query(db::FileTextQuery)
42 .set(file_id, Arc::new(text));
43 changed.insert(file_id);
44 }
45 }
46 }
47 let file_set = self.db.file_set();
48 let mut files: FxHashSet<FileId> = file_set.files.clone();
49 for file_id in removed {
50 files.remove(&file_id);
51 }
52 files.extend(changed);
53 let resolver = file_resolver.unwrap_or_else(|| file_set.resolver.clone());
54 self.db
55 .query(db::FileSetQuery)
56 .set((), Arc::new(db::FileSet { files, resolver }));
57 }
58}
59
60impl SourceRoot for WritableSourceRoot {
61 fn contains(&self, file_id: FileId) -> bool {
62 self.db.file_set().files.contains(&file_id)
63 }
64 fn db(&self) -> &db::RootDatabase {
65 &self.db
66 }
67 fn symbols<'a>(&'a self, acc: &mut Vec<Arc<SymbolIndex>>) -> Cancelable<()> {
68 for &file_id in self.db.file_set().files.iter() {
69 let symbols = self.db.file_symbols(file_id)?;
70 acc.push(symbols)
71 }
72 Ok(())
73 }
74}
75
76#[derive(Debug, Clone)]
77pub(crate) struct ReadonlySourceRoot {
78 db: db::RootDatabase,
79 symbol_index: Arc<SymbolIndex>,
80}
81
82impl ReadonlySourceRoot {
83 pub(crate) fn new(
84 files: Vec<(FileId, String)>,
85 resolver: FileResolverImp,
86 ) -> ReadonlySourceRoot {
87 let db = db::RootDatabase::default();
88 let mut file_ids = FxHashSet::default();
89 for (file_id, text) in files {
90 file_ids.insert(file_id);
91 db.query(db::FileTextQuery).set(file_id, Arc::new(text));
92 }
93
94 db.query(db::FileSetQuery)
95 .set((), Arc::new(db::FileSet { files: file_ids, resolver }));
96 let file_set = db.file_set();
97 let symbol_index =
98 SymbolIndex::for_files(file_set.files.par_iter()
99 .map_with(db.clone(), |db, &file_id| (file_id, db.file_syntax(file_id))));
100
101 ReadonlySourceRoot { db, symbol_index: Arc::new(symbol_index) }
102 }
103}
104
105impl SourceRoot for ReadonlySourceRoot {
106 fn contains(&self, file_id: FileId) -> bool {
107 self.db.file_set().files.contains(&file_id)
108 }
109 fn db(&self) -> &db::RootDatabase {
110 &self.db
111 }
112 fn symbols(&self, acc: &mut Vec<Arc<SymbolIndex>>) -> Cancelable<()> {
113 acc.push(Arc::clone(&self.symbol_index));
114 Ok(())
115 }
116}
diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs
index e5c8d8870..5f302cbda 100644
--- a/crates/ra_analysis/src/symbol_index.rs
+++ b/crates/ra_analysis/src/symbol_index.rs
@@ -13,7 +13,7 @@ use rayon::prelude::*;
13 13
14use crate::{FileId, Query}; 14use crate::{FileId, Query};
15 15
16#[derive(Debug)] 16#[derive(Default, Debug)]
17pub(crate) struct SymbolIndex { 17pub(crate) struct SymbolIndex {
18 symbols: Vec<(FileId, FileSymbol)>, 18 symbols: Vec<(FileId, FileSymbol)>,
19 map: fst::Map, 19 map: fst::Map,