aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-09-06 12:23:31 +0100
committerGitHub <[email protected]>2019-09-06 12:23:31 +0100
commit2015e8b4d15c8be6c8fd735b0c08ccbbe24fb934 (patch)
tree5f5adc8ffd4c29d104e646057f636def67ac2549 /crates
parent3bdb456d17660f3460bbf2c38315568b2f76aaa5 (diff)
parent9ae455ea52bf0bc60476fdb3d50d05f5873040c1 (diff)
Merge #1778
1778: make source_root API more abstract r=matklad a=matklad bors r+ Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_cli/src/analysis_bench.rs5
-rw-r--r--crates/ra_db/src/input.rs16
-rw-r--r--crates/ra_db/src/lib.rs5
-rw-r--r--crates/ra_hir/src/mock.rs2
-rw-r--r--crates/ra_hir/src/nameres/mod_resolution.rs14
-rw-r--r--crates/ra_ide_api/src/change.rs4
-rw-r--r--crates/ra_ide_api/src/symbol_index.rs2
7 files changed, 31 insertions, 17 deletions
diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs
index 9e76bcebf..01b96ec58 100644
--- a/crates/ra_cli/src/analysis_bench.rs
+++ b/crates/ra_cli/src/analysis_bench.rs
@@ -34,10 +34,11 @@ pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> {
34 .iter() 34 .iter()
35 .find_map(|(source_root_id, project_root)| { 35 .find_map(|(source_root_id, project_root)| {
36 if project_root.is_member() { 36 if project_root.is_member() {
37 for (rel_path, file_id) in &db.source_root(*source_root_id).files { 37 for file_id in db.source_root(*source_root_id).walk() {
38 let rel_path = db.file_relative_path(file_id);
38 let abs_path = rel_path.to_path(project_root.path()); 39 let abs_path = rel_path.to_path(project_root.path());
39 if abs_path == path { 40 if abs_path == path {
40 return Some(*file_id); 41 return Some(file_id);
41 } 42 }
42 } 43 }
43 } 44 }
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index ad8e10c52..d1ee3c036 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -5,7 +5,7 @@
5/// Note that neither this module, nor any other part of the analyzer's core do 5/// Note that neither this module, nor any other part of the analyzer's core do
6/// actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how 6/// actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how
7/// actual IO is done and lowered to input. 7/// actual IO is done and lowered to input.
8use relative_path::RelativePathBuf; 8use relative_path::{RelativePath, RelativePathBuf};
9use rustc_hash::FxHashMap; 9use rustc_hash::FxHashMap;
10 10
11use ra_syntax::SmolStr; 11use ra_syntax::SmolStr;
@@ -36,7 +36,7 @@ pub struct SourceRoot {
36 /// Libraries are considered mostly immutable, this assumption is used to 36 /// Libraries are considered mostly immutable, this assumption is used to
37 /// optimize salsa's query structure 37 /// optimize salsa's query structure
38 pub is_library: bool, 38 pub is_library: bool,
39 pub files: FxHashMap<RelativePathBuf, FileId>, 39 files: FxHashMap<RelativePathBuf, FileId>,
40} 40}
41 41
42impl SourceRoot { 42impl SourceRoot {
@@ -46,6 +46,18 @@ impl SourceRoot {
46 pub fn new_library() -> SourceRoot { 46 pub fn new_library() -> SourceRoot {
47 SourceRoot { is_library: true, ..SourceRoot::new() } 47 SourceRoot { is_library: true, ..SourceRoot::new() }
48 } 48 }
49 pub fn file_by_relative_path(&self, path: &RelativePath) -> Option<FileId> {
50 self.files.get(path).copied()
51 }
52 pub fn insert_file(&mut self, path: RelativePathBuf, file_id: FileId) {
53 self.files.insert(path, file_id);
54 }
55 pub fn remove_file(&mut self, path: &RelativePath) {
56 self.files.remove(path);
57 }
58 pub fn walk(&self) -> impl Iterator<Item = FileId> + '_ {
59 self.files.values().copied()
60 }
49} 61}
50 62
51/// `CrateGraph` is a bit of information which turns a set of text files into a 63/// `CrateGraph` is a bit of information which turns a set of text files into a
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs
index b82d1bda0..c54791b7a 100644
--- a/crates/ra_db/src/lib.rs
+++ b/crates/ra_db/src/lib.rs
@@ -12,7 +12,7 @@ pub use crate::{
12 cancellation::Canceled, 12 cancellation::Canceled,
13 input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId}, 13 input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId},
14}; 14};
15pub use ::salsa; 15pub use salsa;
16 16
17pub trait CheckCanceled { 17pub trait CheckCanceled {
18 /// Aborts current query if there are pending changes. 18 /// Aborts current query if there are pending changes.
@@ -93,8 +93,7 @@ pub trait SourceDatabase: CheckCanceled + std::fmt::Debug {
93fn source_root_crates(db: &impl SourceDatabase, id: SourceRootId) -> Arc<Vec<CrateId>> { 93fn source_root_crates(db: &impl SourceDatabase, id: SourceRootId) -> Arc<Vec<CrateId>> {
94 let root = db.source_root(id); 94 let root = db.source_root(id);
95 let graph = db.crate_graph(); 95 let graph = db.crate_graph();
96 let res = 96 let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::<Vec<_>>();
97 root.files.values().filter_map(|&it| graph.crate_id_for_crate_root(it)).collect::<Vec<_>>();
98 Arc::new(res) 97 Arc::new(res)
99} 98}
100 99
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index 77a44a275..972f0ece5 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -157,7 +157,7 @@ impl MockDatabase {
157 self.set_file_text(file_id, text); 157 self.set_file_text(file_id, text);
158 self.set_file_relative_path(file_id, rel_path.clone()); 158 self.set_file_relative_path(file_id, rel_path.clone());
159 self.set_file_source_root(file_id, source_root_id); 159 self.set_file_source_root(file_id, source_root_id);
160 source_root.files.insert(rel_path, file_id); 160 source_root.insert_file(rel_path, file_id);
161 161
162 if is_crate_root { 162 if is_crate_root {
163 let mut crate_graph = CrateGraph::default(); 163 let mut crate_graph = CrateGraph::default();
diff --git a/crates/ra_hir/src/nameres/mod_resolution.rs b/crates/ra_hir/src/nameres/mod_resolution.rs
index 94c9946ff..de81fd422 100644
--- a/crates/ra_hir/src/nameres/mod_resolution.rs
+++ b/crates/ra_hir/src/nameres/mod_resolution.rs
@@ -120,10 +120,12 @@ enum OutOfLineMode {
120impl OutOfLineMode { 120impl OutOfLineMode {
121 pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> { 121 pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
122 match self { 122 match self {
123 OutOfLineMode::RootOrModRs { file, directory } => match source_root.files.get(file) { 123 OutOfLineMode::RootOrModRs { file, directory } => {
124 None => resolve_simple_path(source_root, directory).map_err(|_| file.clone()), 124 match source_root.file_by_relative_path(file) {
125 file_id => resolve_find_result(file_id, file), 125 None => resolve_simple_path(source_root, directory).map_err(|_| file.clone()),
126 }, 126 file_id => resolve_find_result(file_id, file),
127 }
128 }
127 OutOfLineMode::FileInDirectory(path) => resolve_simple_path(source_root, path), 129 OutOfLineMode::FileInDirectory(path) => resolve_simple_path(source_root, path),
128 OutOfLineMode::WithAttributePath(path) => resolve_simple_path(source_root, path), 130 OutOfLineMode::WithAttributePath(path) => resolve_simple_path(source_root, path),
129 } 131 }
@@ -168,11 +170,11 @@ fn resolve_simple_path(
168 source_root: Arc<SourceRoot>, 170 source_root: Arc<SourceRoot>,
169 path: &RelativePathBuf, 171 path: &RelativePathBuf,
170) -> Result<FileId, RelativePathBuf> { 172) -> Result<FileId, RelativePathBuf> {
171 resolve_find_result(source_root.files.get(path), path) 173 resolve_find_result(source_root.file_by_relative_path(path), path)
172} 174}
173 175
174fn resolve_find_result( 176fn resolve_find_result(
175 file_id: Option<&FileId>, 177 file_id: Option<FileId>,
176 path: &RelativePathBuf, 178 path: &RelativePathBuf,
177) -> Result<FileId, RelativePathBuf> { 179) -> Result<FileId, RelativePathBuf> {
178 match file_id { 180 match file_id {
diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs
index 0234c572d..89631935a 100644
--- a/crates/ra_ide_api/src/change.rs
+++ b/crates/ra_ide_api/src/change.rs
@@ -213,11 +213,11 @@ impl RootDatabase {
213 durability, 213 durability,
214 ); 214 );
215 self.set_file_source_root_with_durability(add_file.file_id, root_id, durability); 215 self.set_file_source_root_with_durability(add_file.file_id, root_id, durability);
216 source_root.files.insert(add_file.path, add_file.file_id); 216 source_root.insert_file(add_file.path, add_file.file_id);
217 } 217 }
218 for remove_file in root_change.removed { 218 for remove_file in root_change.removed {
219 self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability); 219 self.set_file_text_with_durability(remove_file.file_id, Default::default(), durability);
220 source_root.files.remove(&remove_file.path); 220 source_root.remove_file(&remove_file.path);
221 } 221 }
222 self.set_source_root_with_durability(root_id, Arc::new(source_root), durability); 222 self.set_source_root_with_durability(root_id, Arc::new(source_root), durability);
223 } 223 }
diff --git a/crates/ra_ide_api/src/symbol_index.rs b/crates/ra_ide_api/src/symbol_index.rs
index d4afddab4..a5729c368 100644
--- a/crates/ra_ide_api/src/symbol_index.rs
+++ b/crates/ra_ide_api/src/symbol_index.rs
@@ -87,7 +87,7 @@ pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol>
87 let mut files = Vec::new(); 87 let mut files = Vec::new();
88 for &root in db.local_roots().iter() { 88 for &root in db.local_roots().iter() {
89 let sr = db.source_root(root); 89 let sr = db.source_root(root);
90 files.extend(sr.files.values().copied()) 90 files.extend(sr.walk())
91 } 91 }
92 92
93 let snap = Snap(db.snapshot()); 93 let snap = Snap(db.snapshot());