aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-03-09 09:26:46 +0000
committerKirill Bulatov <[email protected]>2020-03-09 09:26:46 +0000
commite1aa96f2c5b6cdbf0fb7f49b47209055b7a937f2 (patch)
tree3bf11b84559f60cf1aa8cf4098f2b310da20aa91
parent5cffef56e2c373f6d67b0f7b70d7ade995795c04 (diff)
Less abstract CrateData api
-rw-r--r--crates/ra_db/src/input.rs60
-rw-r--r--crates/ra_hir/src/code_model.rs13
-rw-r--r--crates/ra_hir_def/src/find_path.rs2
-rw-r--r--crates/ra_hir_def/src/lang_item.rs4
-rw-r--r--crates/ra_hir_def/src/nameres.rs3
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs7
-rw-r--r--crates/ra_hir_ty/src/traits.rs2
-rw-r--r--crates/ra_ide/src/hover.rs4
-rw-r--r--crates/ra_ide/src/lib.rs4
9 files changed, 48 insertions, 51 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index 7b9f4efe4..4069c0fed 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -104,13 +104,16 @@ impl CrateName {
104} 104}
105 105
106#[derive(Debug, Clone, PartialEq, Eq)] 106#[derive(Debug, Clone, PartialEq, Eq)]
107struct CrateData { 107pub struct CrateData {
108 file_id: FileId, 108 pub root_file_id: FileId,
109 edition: Edition, 109 pub edition: Edition,
110 declaration_name: Option<String>, 110 /// The name to display to the end user.
111 /// This actual crate name can be different in a particular dependent crate
112 /// or may even be missing for some cases, such as a dummy crate for the code snippet.
113 pub display_name: Option<String>,
111 cfg_options: CfgOptions, 114 cfg_options: CfgOptions,
112 env: Env, 115 env: Env,
113 dependencies: Vec<Dependency>, 116 pub dependencies: Vec<Dependency>,
114} 117}
115 118
116#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 119#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -135,11 +138,11 @@ impl CrateGraph {
135 &mut self, 138 &mut self,
136 file_id: FileId, 139 file_id: FileId,
137 edition: Edition, 140 edition: Edition,
138 declaration_name: Option<String>, 141 display_name: Option<String>,
139 cfg_options: CfgOptions, 142 cfg_options: CfgOptions,
140 env: Env, 143 env: Env,
141 ) -> CrateId { 144 ) -> CrateId {
142 let data = CrateData::new(file_id, edition, declaration_name, cfg_options, env); 145 let data = CrateData::new(file_id, edition, display_name, cfg_options, env);
143 let crate_id = CrateId(self.arena.len() as u32); 146 let crate_id = CrateId(self.arena.len() as u32);
144 let prev = self.arena.insert(crate_id, data); 147 let prev = self.arena.insert(crate_id, data);
145 assert!(prev.is_none()); 148 assert!(prev.is_none());
@@ -171,33 +174,17 @@ impl CrateGraph {
171 self.arena.keys().copied() 174 self.arena.keys().copied()
172 } 175 }
173 176
174 pub fn crate_root(&self, crate_id: CrateId) -> FileId { 177 pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData {
175 self.arena[&crate_id].file_id 178 &self.arena[crate_id]
176 }
177
178 pub fn edition(&self, crate_id: CrateId) -> Edition {
179 self.arena[&crate_id].edition
180 }
181
182 /// Returns a name of a crate, declared in the root project.
183 /// May be missing for some cases, such as when the crate definition was created for a code snippet.
184 ///
185 /// This should not be considered as a normal crate name, since the actual name can be different in
186 /// a particular dependent crate, where it is specified.
187 pub fn declaration_name(&self, crate_id: &CrateId) -> Option<&String> {
188 self.arena[crate_id].declaration_name.as_ref()
189 } 179 }
190 180
191 // FIXME: this only finds one crate with the given root; we could have multiple 181 // FIXME: this only finds one crate with the given root; we could have multiple
192 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { 182 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
193 let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?; 183 let (&crate_id, _) =
184 self.arena.iter().find(|(_crate_id, data)| data.root_file_id == file_id)?;
194 Some(crate_id) 185 Some(crate_id)
195 } 186 }
196 187
197 pub fn dependencies(&self, crate_id: CrateId) -> impl Iterator<Item = &Dependency> {
198 self.arena[&crate_id].dependencies.iter()
199 }
200
201 /// Extends this crate graph by adding a complete disjoint second crate 188 /// Extends this crate graph by adding a complete disjoint second crate
202 /// graph. 189 /// graph.
203 /// 190 ///
@@ -220,7 +207,7 @@ impl CrateGraph {
220 return false; 207 return false;
221 } 208 }
222 209
223 for dep in self.dependencies(from) { 210 for dep in &self.crate_data(&from).dependencies {
224 let crate_id = dep.crate_id(); 211 let crate_id = dep.crate_id();
225 if crate_id == target { 212 if crate_id == target {
226 return true; 213 return true;
@@ -242,13 +229,20 @@ impl CrateId {
242 229
243impl CrateData { 230impl CrateData {
244 fn new( 231 fn new(
245 file_id: FileId, 232 root_file_id: FileId,
246 edition: Edition, 233 edition: Edition,
247 declaration_name: Option<String>, 234 display_name: Option<String>,
248 cfg_options: CfgOptions, 235 cfg_options: CfgOptions,
249 env: Env, 236 env: Env,
250 ) -> CrateData { 237 ) -> CrateData {
251 CrateData { file_id, edition, declaration_name, dependencies: Vec::new(), cfg_options, env } 238 CrateData {
239 root_file_id,
240 edition,
241 display_name,
242 dependencies: Vec::new(),
243 cfg_options,
244 env,
245 }
252 } 246 }
253 247
254 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { 248 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
@@ -382,8 +376,8 @@ mod tests {
382 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) 376 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
383 .is_ok()); 377 .is_ok());
384 assert_eq!( 378 assert_eq!(
385 graph.dependencies(crate1).collect::<Vec<_>>(), 379 graph.crate_data(&crate1).dependencies,
386 vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] 380 vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
387 ); 381 );
388 } 382 }
389} 383}
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index dca5f27b2..9685d6982 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -55,7 +55,9 @@ pub struct CrateDependency {
55impl Crate { 55impl Crate {
56 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> { 56 pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
57 db.crate_graph() 57 db.crate_graph()
58 .dependencies(self.id) 58 .crate_data(&self.id)
59 .dependencies
60 .iter()
59 .map(|dep| { 61 .map(|dep| {
60 let krate = Crate { id: dep.crate_id() }; 62 let krate = Crate { id: dep.crate_id() };
61 let name = dep.as_name(); 63 let name = dep.as_name();
@@ -69,7 +71,9 @@ impl Crate {
69 let crate_graph = db.crate_graph(); 71 let crate_graph = db.crate_graph();
70 crate_graph 72 crate_graph
71 .iter() 73 .iter()
72 .filter(|&krate| crate_graph.dependencies(krate).any(|it| it.crate_id == self.id)) 74 .filter(|&krate| {
75 crate_graph.crate_data(&krate).dependencies.iter().any(|it| it.crate_id == self.id)
76 })
73 .map(|id| Crate { id }) 77 .map(|id| Crate { id })
74 .collect() 78 .collect()
75 } 79 }
@@ -80,12 +84,11 @@ impl Crate {
80 } 84 }
81 85
82 pub fn root_file(self, db: &impl DefDatabase) -> FileId { 86 pub fn root_file(self, db: &impl DefDatabase) -> FileId {
83 db.crate_graph().crate_root(self.id) 87 db.crate_graph().crate_data(&self.id).root_file_id
84 } 88 }
85 89
86 pub fn edition(self, db: &impl DefDatabase) -> Edition { 90 pub fn edition(self, db: &impl DefDatabase) -> Edition {
87 let crate_graph = db.crate_graph(); 91 db.crate_graph().crate_data(&self.id).edition
88 crate_graph.edition(self.id)
89 } 92 }
90 93
91 pub fn all(db: &impl DefDatabase) -> Vec<Crate> { 94 pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs
index 43b9b124a..217e19b01 100644
--- a/crates/ra_hir_def/src/find_path.rs
+++ b/crates/ra_hir_def/src/find_path.rs
@@ -176,7 +176,7 @@ fn find_importable_locations(
176 // directly (only through reexports in direct dependencies). 176 // directly (only through reexports in direct dependencies).
177 for krate in Some(from.krate) 177 for krate in Some(from.krate)
178 .into_iter() 178 .into_iter()
179 .chain(crate_graph.dependencies(from.krate).map(|dep| dep.crate_id)) 179 .chain(crate_graph.crate_data(&from.krate).dependencies.iter().map(|dep| dep.crate_id))
180 { 180 {
181 result.extend( 181 result.extend(
182 importable_locations_in_crate(db, item, krate) 182 importable_locations_in_crate(db, item, krate)
diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs
index 5a336ea1f..79e8d8038 100644
--- a/crates/ra_hir_def/src/lang_item.rs
+++ b/crates/ra_hir_def/src/lang_item.rs
@@ -117,7 +117,9 @@ impl LangItems {
117 return Some(*target); 117 return Some(*target);
118 } 118 }
119 db.crate_graph() 119 db.crate_graph()
120 .dependencies(start_crate) 120 .crate_data(&start_crate)
121 .dependencies
122 .iter()
121 .find_map(|dep| db.lang_item(dep.crate_id, item.clone())) 123 .find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
122 } 124 }
123 125
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index 628c44c99..6af0f4a8e 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -179,8 +179,7 @@ impl CrateDefMap {
179 pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { 179 pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
180 let _p = profile("crate_def_map_query"); 180 let _p = profile("crate_def_map_query");
181 let def_map = { 181 let def_map = {
182 let crate_graph = db.crate_graph(); 182 let edition = db.crate_graph().crate_data(&krate).edition;
183 let edition = crate_graph.edition(krate);
184 let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default(); 183 let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
185 let root = modules.alloc(ModuleData::default()); 184 let root = modules.alloc(ModuleData::default());
186 CrateDefMap { 185 CrateDefMap {
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 51c65a5d7..e69f89b80 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -34,7 +34,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C
34 let crate_graph = db.crate_graph(); 34 let crate_graph = db.crate_graph();
35 35
36 // populate external prelude 36 // populate external prelude
37 for dep in crate_graph.dependencies(def_map.krate) { 37 for dep in &crate_graph.crate_data(&def_map.krate).dependencies {
38 let dep_def_map = db.crate_def_map(dep.crate_id); 38 let dep_def_map = db.crate_def_map(dep.crate_id);
39 log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); 39 log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
40 def_map.extern_prelude.insert( 40 def_map.extern_prelude.insert(
@@ -128,8 +128,7 @@ where
128 DB: DefDatabase, 128 DB: DefDatabase,
129{ 129{
130 fn collect(&mut self) { 130 fn collect(&mut self) {
131 let crate_graph = self.db.crate_graph(); 131 let file_id = self.db.crate_graph().crate_data(&self.def_map.krate).root_file_id;
132 let file_id = crate_graph.crate_root(self.def_map.krate);
133 let raw_items = self.db.raw_items(file_id.into()); 132 let raw_items = self.db.raw_items(file_id.into());
134 let module_id = self.def_map.root; 133 let module_id = self.def_map.root;
135 self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; 134 self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
@@ -955,7 +954,7 @@ mod tests {
955 let krate = db.test_crate(); 954 let krate = db.test_crate();
956 955
957 let def_map = { 956 let def_map = {
958 let edition = db.crate_graph().edition(krate); 957 let edition = db.crate_graph().crate_data(&krate).edition;
959 let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default(); 958 let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
960 let root = modules.alloc(ModuleData::default()); 959 let root = modules.alloc(ModuleData::default());
961 CrateDefMap { 960 CrateDefMap {
diff --git a/crates/ra_hir_ty/src/traits.rs b/crates/ra_hir_ty/src/traits.rs
index 8de588790..6c653c4f5 100644
--- a/crates/ra_hir_ty/src/traits.rs
+++ b/crates/ra_hir_ty/src/traits.rs
@@ -47,7 +47,7 @@ pub(crate) fn impls_for_trait_query(
47 // will only ever get called for a few crates near the root of the tree (the 47 // will only ever get called for a few crates near the root of the tree (the
48 // ones the user is editing), so this may actually be a waste of memory. I'm 48 // ones the user is editing), so this may actually be a waste of memory. I'm
49 // doing it like this mainly for simplicity for now. 49 // doing it like this mainly for simplicity for now.
50 for dep in db.crate_graph().dependencies(krate) { 50 for dep in &db.crate_graph().crate_data(&krate).dependencies {
51 impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter()); 51 impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter());
52 } 52 }
53 let crate_impl_defs = db.impls_in_crate(krate); 53 let crate_impl_defs = db.impls_in_crate(krate);
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index f87054838..8b8af35fc 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -121,7 +121,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
121 121
122fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> { 122fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
123 let mod_path = def.module(db).map(|module| { 123 let mod_path = def.module(db).map(|module| {
124 once(db.crate_graph().declaration_name(&module.krate().into()).cloned()) 124 once(db.crate_graph().crate_data(&module.krate().into()).display_name.clone())
125 .chain( 125 .chain(
126 module 126 module
127 .path_to_root(db) 127 .path_to_root(db)
@@ -130,7 +130,7 @@ fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
130 .map(|it| it.name(db).map(|name| name.to_string())), 130 .map(|it| it.name(db).map(|name| name.to_string())),
131 ) 131 )
132 .chain(once(definition_owner_name(db, def))) 132 .chain(once(definition_owner_name(db, def)))
133 .filter_map(std::convert::identity) 133 .flatten()
134 .join("::") 134 .join("::")
135 }); 135 });
136 mod_path 136 mod_path
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 56bc57d5c..903624381 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -421,12 +421,12 @@ impl Analysis {
421 421
422 /// Returns the edition of the given crate. 422 /// Returns the edition of the given crate.
423 pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> { 423 pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> {
424 self.with_db(|db| db.crate_graph().edition(crate_id)) 424 self.with_db(|db| db.crate_graph().crate_data(&crate_id).edition)
425 } 425 }
426 426
427 /// Returns the root file of the given crate. 427 /// Returns the root file of the given crate.
428 pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> { 428 pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
429 self.with_db(|db| db.crate_graph().crate_root(crate_id)) 429 self.with_db(|db| db.crate_graph().crate_data(&crate_id).root_file_id)
430 } 430 }
431 431
432 /// Returns the set of possible targets to run for the current file. 432 /// Returns the set of possible targets to run for the current file.