aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-01-10 17:40:45 +0000
committerFlorian Diebold <[email protected]>2020-01-11 22:33:04 +0000
commit4496e2a06a91e5825f355ce730af802643e8018a (patch)
treef0f41e2d9ff5045f34b38cd3d23bae11e5d56139
parent15fc643e05bf8273e378243edbfb3be7aea7ce11 (diff)
Apply review suggestions
-rw-r--r--crates/ra_assists/src/ast_transform.rs15
-rw-r--r--crates/ra_hir/src/code_model.rs4
-rw-r--r--crates/ra_hir/src/source_binder.rs2
-rw-r--r--crates/ra_hir_def/src/db.rs7
-rw-r--r--crates/ra_hir_def/src/find_path.rs17
-rw-r--r--crates/ra_hir_def/src/item_scope.rs5
-rw-r--r--crates/ra_hir_def/src/path.rs14
-rw-r--r--crates/ra_hir_def/src/resolver.rs14
-rw-r--r--crates/ra_hir_expand/src/name.rs4
9 files changed, 35 insertions, 47 deletions
diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs
index 846661587..ace59f290 100644
--- a/crates/ra_assists/src/ast_transform.rs
+++ b/crates/ra_assists/src/ast_transform.rs
@@ -60,6 +60,8 @@ impl<'a, DB: HirDatabase> SubstituteTypeParams<'a, DB> {
60 previous: Box::new(NullTransformer), 60 previous: Box::new(NullTransformer),
61 }; 61 };
62 62
63 // FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
64 // trait ref, and then go from the types in the substs back to the syntax)
63 fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option<Vec<ast::TypeRef>> { 65 fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option<Vec<ast::TypeRef>> {
64 let target_trait = impl_block.target_trait()?; 66 let target_trait = impl_block.target_trait()?;
65 let path_type = match target_trait { 67 let path_type = match target_trait {
@@ -131,12 +133,12 @@ impl<'a, DB: HirDatabase> QualifyPaths<'a, DB> {
131 let resolution = analyzer.resolve_path(self.db, &p)?; 133 let resolution = analyzer.resolve_path(self.db, &p)?;
132 match resolution { 134 match resolution {
133 PathResolution::Def(def) => { 135 PathResolution::Def(def) => {
134 let found_path = from.find_path(self.db, def)?; 136 let found_path = from.find_use_path(self.db, def)?;
135 let args = p 137 let args = p
136 .segment() 138 .segment()
137 .and_then(|s| s.type_arg_list()) 139 .and_then(|s| s.type_arg_list())
138 .map(|arg_list| apply(self, node.with_value(arg_list))); 140 .map(|arg_list| apply(self, node.with_value(arg_list)));
139 Some(make::path_with_type_arg_list(found_path.to_ast(), args).syntax().clone()) 141 Some(make::path_with_type_arg_list(path_to_ast(found_path), args).syntax().clone())
140 } 142 }
141 PathResolution::Local(_) 143 PathResolution::Local(_)
142 | PathResolution::TypeParam(_) 144 | PathResolution::TypeParam(_)
@@ -171,8 +173,7 @@ impl<'a, DB: HirDatabase> AstTransform<'a> for QualifyPaths<'a, DB> {
171 } 173 }
172} 174}
173 175
174// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the 176fn path_to_ast(path: hir::ModPath) -> ast::Path {
175// trait ref, and then go from the types in the substs back to the syntax) 177 let parse = ast::SourceFile::parse(&path.to_string());
176// FIXME: This should be a general utility (not even just for assists) 178 parse.tree().syntax().descendants().find_map(ast::Path::cast).unwrap()
177 179}
178// FIXME: This should be a general utility (not even just for assists)
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 4da3db0d6..df9c151e5 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -228,7 +228,9 @@ impl Module {
228 Module::new(self.krate(), module_id) 228 Module::new(self.krate(), module_id)
229 } 229 }
230 230
231 pub fn find_path( 231 /// Finds a path that can be used to refer to the given item from within
232 /// this module, if possible.
233 pub fn find_use_path(
232 self, 234 self,
233 db: &impl DefDatabase, 235 db: &impl DefDatabase,
234 item: ModuleDef, 236 item: ModuleDef,
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 71339565f..a2a9d968c 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -206,7 +206,7 @@ impl SourceAnalyzer {
206 } 206 }
207 207
208 pub fn module(&self) -> Option<crate::code_model::Module> { 208 pub fn module(&self) -> Option<crate::code_model::Module> {
209 Some(crate::code_model::Module { id: self.resolver.module_id()? }) 209 Some(crate::code_model::Module { id: self.resolver.module()? })
210 } 210 }
211 211
212 fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> { 212 fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> {
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index 0c00627b5..da273eb11 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -107,13 +107,6 @@ pub trait DefDatabase: InternDatabase + AstDatabase {
107 // Remove this query completely, in favor of `Attrs::docs` method 107 // Remove this query completely, in favor of `Attrs::docs` method
108 #[salsa::invoke(Documentation::documentation_query)] 108 #[salsa::invoke(Documentation::documentation_query)]
109 fn documentation(&self, def: AttrDefId) -> Option<Documentation>; 109 fn documentation(&self, def: AttrDefId) -> Option<Documentation>;
110
111 #[salsa::invoke(crate::find_path::importable_locations_in_crate_query)]
112 fn importable_locations_in_crate(
113 &self,
114 item: crate::item_scope::ItemInNs,
115 krate: CrateId,
116 ) -> Arc<[(ModuleId, hir_expand::name::Name, crate::visibility::Visibility)]>;
117} 110}
118 111
119fn crate_def_map(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { 112fn crate_def_map(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs
index 69cdfc943..f7dc8acb7 100644
--- a/crates/ra_hir_def/src/find_path.rs
+++ b/crates/ra_hir_def/src/find_path.rs
@@ -34,7 +34,7 @@ fn find_path_inner(
34 // - if the item is already in scope, return the name under which it is 34 // - if the item is already in scope, return the name under which it is
35 let def_map = db.crate_def_map(from.krate); 35 let def_map = db.crate_def_map(from.krate);
36 let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope; 36 let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope;
37 if let Some((name, _)) = from_scope.reverse_get(item) { 37 if let Some((name, _)) = from_scope.name_of(item) {
38 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); 38 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
39 } 39 }
40 40
@@ -77,7 +77,7 @@ fn find_path_inner(
77 let prelude_def_map = db.crate_def_map(prelude_module.krate); 77 let prelude_def_map = db.crate_def_map(prelude_module.krate);
78 let prelude_scope: &crate::item_scope::ItemScope = 78 let prelude_scope: &crate::item_scope::ItemScope =
79 &prelude_def_map.modules[prelude_module.local_id].scope; 79 &prelude_def_map.modules[prelude_module.local_id].scope;
80 if let Some((name, vis)) = prelude_scope.reverse_get(item) { 80 if let Some((name, vis)) = prelude_scope.name_of(item) {
81 if vis.is_visible_from(db, from) { 81 if vis.is_visible_from(db, from) {
82 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); 82 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
83 } 83 }
@@ -146,7 +146,7 @@ fn find_importable_locations(
146 .chain(crate_graph.dependencies(from.krate).map(|dep| dep.crate_id)) 146 .chain(crate_graph.dependencies(from.krate).map(|dep| dep.crate_id))
147 { 147 {
148 result.extend( 148 result.extend(
149 db.importable_locations_in_crate(item, krate) 149 importable_locations_in_crate(db, item, krate)
150 .iter() 150 .iter()
151 .filter(|(_, _, vis)| vis.is_visible_from(db, from)) 151 .filter(|(_, _, vis)| vis.is_visible_from(db, from))
152 .map(|(m, n, _)| (*m, n.clone())), 152 .map(|(m, n, _)| (*m, n.clone())),
@@ -160,17 +160,16 @@ fn find_importable_locations(
160/// non-private `use`s. 160/// non-private `use`s.
161/// 161///
162/// Note that the crate doesn't need to be the one in which the item is defined; 162/// Note that the crate doesn't need to be the one in which the item is defined;
163/// it might be re-exported in other crates. We cache this as a query since we 163/// it might be re-exported in other crates.
164/// need to walk the whole def map for it. 164fn importable_locations_in_crate(
165pub(crate) fn importable_locations_in_crate_query(
166 db: &impl DefDatabase, 165 db: &impl DefDatabase,
167 item: ItemInNs, 166 item: ItemInNs,
168 krate: CrateId, 167 krate: CrateId,
169) -> std::sync::Arc<[(ModuleId, Name, Visibility)]> { 168) -> Vec<(ModuleId, Name, Visibility)> {
170 let def_map = db.crate_def_map(krate); 169 let def_map = db.crate_def_map(krate);
171 let mut result = Vec::new(); 170 let mut result = Vec::new();
172 for (local_id, data) in def_map.modules.iter() { 171 for (local_id, data) in def_map.modules.iter() {
173 if let Some((name, vis)) = data.scope.reverse_get(item) { 172 if let Some((name, vis)) = data.scope.name_of(item) {
174 let is_private = if let Visibility::Module(private_to) = vis { 173 let is_private = if let Visibility::Module(private_to) = vis {
175 private_to.local_id == local_id 174 private_to.local_id == local_id
176 } else { 175 } else {
@@ -192,7 +191,7 @@ pub(crate) fn importable_locations_in_crate_query(
192 result.push((ModuleId { krate, local_id }, name.clone(), vis)); 191 result.push((ModuleId { krate, local_id }, name.clone(), vis));
193 } 192 }
194 } 193 }
195 result.into() 194 result
196} 195}
197 196
198#[cfg(test)] 197#[cfg(test)]
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs
index 87c50b34f..d74a1cef2 100644
--- a/crates/ra_hir_def/src/item_scope.rs
+++ b/crates/ra_hir_def/src/item_scope.rs
@@ -104,7 +104,7 @@ impl ItemScope {
104 } 104 }
105 } 105 }
106 106
107 pub(crate) fn reverse_get(&self, item: ItemInNs) -> Option<(&Name, Visibility)> { 107 pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility)> {
108 for (name, per_ns) in &self.visible { 108 for (name, per_ns) in &self.visible {
109 if let Some(vis) = item.match_with(*per_ns) { 109 if let Some(vis) = item.match_with(*per_ns) {
110 return Some((name, vis)); 110 return Some((name, vis));
@@ -207,8 +207,7 @@ impl ItemInNs {
207 207
208 pub fn as_module_def_id(self) -> Option<ModuleDefId> { 208 pub fn as_module_def_id(self) -> Option<ModuleDefId> {
209 match self { 209 match self {
210 ItemInNs::Types(t) => Some(t), 210 ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id),
211 ItemInNs::Values(v) => Some(v),
212 ItemInNs::Macros(_) => None, 211 ItemInNs::Macros(_) => None,
213 } 212 }
214 } 213 }
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
index 7dd1939b9..9f93a5424 100644
--- a/crates/ra_hir_def/src/path.rs
+++ b/crates/ra_hir_def/src/path.rs
@@ -1,7 +1,11 @@
1//! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`. 1//! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`.
2mod lower; 2mod lower;
3 3
4use std::{fmt::Display, iter, sync::Arc}; 4use std::{
5 fmt::{self, Display},
6 iter,
7 sync::Arc,
8};
5 9
6use hir_expand::{ 10use hir_expand::{
7 hygiene::Hygiene, 11 hygiene::Hygiene,
@@ -78,12 +82,6 @@ impl ModPath {
78 } 82 }
79 self.segments.first() 83 self.segments.first()
80 } 84 }
81
82 pub fn to_ast(&self) -> ast::Path {
83 use ast::AstNode;
84 let parse = ast::SourceFile::parse(&self.to_string());
85 parse.tree().syntax().descendants().find_map(ast::Path::cast).unwrap()
86 }
87} 85}
88 86
89#[derive(Debug, Clone, PartialEq, Eq, Hash)] 87#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -255,7 +253,7 @@ impl From<Name> for ModPath {
255} 253}
256 254
257impl Display for ModPath { 255impl Display for ModPath {
258 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 256 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
259 let mut first_segment = true; 257 let mut first_segment = true;
260 let mut add_segment = |s| { 258 let mut add_segment = |s| {
261 if !first_segment { 259 if !first_segment {
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index 40d0cb588..f7bac5801 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -128,7 +128,7 @@ impl Resolver {
128 path: &ModPath, 128 path: &ModPath,
129 shadow: BuiltinShadowMode, 129 shadow: BuiltinShadowMode,
130 ) -> PerNs { 130 ) -> PerNs {
131 let (item_map, module) = match self.module() { 131 let (item_map, module) = match self.module_scope() {
132 Some(it) => it, 132 Some(it) => it,
133 None => return PerNs::none(), 133 None => return PerNs::none(),
134 }; 134 };
@@ -239,7 +239,7 @@ impl Resolver {
239 ) -> Option<Visibility> { 239 ) -> Option<Visibility> {
240 match visibility { 240 match visibility {
241 RawVisibility::Module(_) => { 241 RawVisibility::Module(_) => {
242 let (item_map, module) = match self.module() { 242 let (item_map, module) = match self.module_scope() {
243 Some(it) => it, 243 Some(it) => it,
244 None => return None, 244 None => return None,
245 }; 245 };
@@ -379,7 +379,7 @@ impl Resolver {
379 db: &impl DefDatabase, 379 db: &impl DefDatabase,
380 path: &ModPath, 380 path: &ModPath,
381 ) -> Option<MacroDefId> { 381 ) -> Option<MacroDefId> {
382 let (item_map, module) = self.module()?; 382 let (item_map, module) = self.module_scope()?;
383 item_map.resolve_path(db, module, &path, BuiltinShadowMode::Other).0.take_macros() 383 item_map.resolve_path(db, module, &path, BuiltinShadowMode::Other).0.take_macros()
384 } 384 }
385 385
@@ -403,7 +403,7 @@ impl Resolver {
403 traits 403 traits
404 } 404 }
405 405
406 fn module(&self) -> Option<(&CrateDefMap, LocalModuleId)> { 406 fn module_scope(&self) -> Option<(&CrateDefMap, LocalModuleId)> {
407 self.scopes.iter().rev().find_map(|scope| match scope { 407 self.scopes.iter().rev().find_map(|scope| match scope {
408 Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)), 408 Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),
409 409
@@ -411,13 +411,13 @@ impl Resolver {
411 }) 411 })
412 } 412 }
413 413
414 pub fn module_id(&self) -> Option<ModuleId> { 414 pub fn module(&self) -> Option<ModuleId> {
415 let (def_map, local_id) = self.module()?; 415 let (def_map, local_id) = self.module_scope()?;
416 Some(ModuleId { krate: def_map.krate, local_id }) 416 Some(ModuleId { krate: def_map.krate, local_id })
417 } 417 }
418 418
419 pub fn krate(&self) -> Option<CrateId> { 419 pub fn krate(&self) -> Option<CrateId> {
420 self.module().map(|t| t.0.krate) 420 self.module_scope().map(|t| t.0.krate)
421 } 421 }
422 422
423 pub fn where_predicates_in_scope<'a>( 423 pub fn where_predicates_in_scope<'a>(
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs
index a4d912b99..b3fa1efba 100644
--- a/crates/ra_hir_expand/src/name.rs
+++ b/crates/ra_hir_expand/src/name.rs
@@ -37,10 +37,6 @@ impl Name {
37 Name(Repr::TupleField(idx)) 37 Name(Repr::TupleField(idx))
38 } 38 }
39 39
40 pub fn for_crate_dependency(dep: &ra_db::Dependency) -> Name {
41 Name::new_text(dep.name.clone())
42 }
43
44 /// Shortcut to create inline plain text name 40 /// Shortcut to create inline plain text name
45 const fn new_inline_ascii(text: &[u8]) -> Name { 41 const fn new_inline_ascii(text: &[u8]) -> Name {
46 Name::new_text(SmolStr::new_inline_from_ascii(text.len(), text)) 42 Name::new_text(SmolStr::new_inline_from_ascii(text.len(), text))