Skip to content

Commit 4496e2a

Browse files
committed
Apply review suggestions
1 parent 15fc643 commit 4496e2a

File tree

9 files changed

+35
-47
lines changed

9 files changed

+35
-47
lines changed

crates/ra_assists/src/ast_transform.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ impl<'a, DB: HirDatabase> SubstituteTypeParams<'a, DB> {
6060
previous: Box::new(NullTransformer),
6161
};
6262

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)
6365
fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option<Vec<ast::TypeRef>> {
6466
let target_trait = impl_block.target_trait()?;
6567
let path_type = match target_trait {
@@ -131,12 +133,12 @@ impl<'a, DB: HirDatabase> QualifyPaths<'a, DB> {
131133
let resolution = analyzer.resolve_path(self.db, &p)?;
132134
match resolution {
133135
PathResolution::Def(def) => {
134-
let found_path = from.find_path(self.db, def)?;
136+
let found_path = from.find_use_path(self.db, def)?;
135137
let args = p
136138
.segment()
137139
.and_then(|s| s.type_arg_list())
138140
.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())
140142
}
141143
PathResolution::Local(_)
142144
| PathResolution::TypeParam(_)
@@ -171,8 +173,7 @@ impl<'a, DB: HirDatabase> AstTransform<'a> for QualifyPaths<'a, DB> {
171173
}
172174
}
173175

174-
// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
175-
// trait ref, and then go from the types in the substs back to the syntax)
176-
// FIXME: This should be a general utility (not even just for assists)
177-
178-
// FIXME: This should be a general utility (not even just for assists)
176+
fn path_to_ast(path: hir::ModPath) -> ast::Path {
177+
let parse = ast::SourceFile::parse(&path.to_string());
178+
parse.tree().syntax().descendants().find_map(ast::Path::cast).unwrap()
179+
}

crates/ra_hir/src/code_model.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ impl Module {
228228
Module::new(self.krate(), module_id)
229229
}
230230

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(
232234
self,
233235
db: &impl DefDatabase,
234236
item: ModuleDef,

crates/ra_hir/src/source_binder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl SourceAnalyzer {
206206
}
207207

208208
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()? })
210210
}
211211

212212
fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> {

crates/ra_hir_def/src/db.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,6 @@ pub trait DefDatabase: InternDatabase + AstDatabase {
107107
// Remove this query completely, in favor of `Attrs::docs` method
108108
#[salsa::invoke(Documentation::documentation_query)]
109109
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)]>;
117110
}
118111

119112
fn crate_def_map(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {

crates/ra_hir_def/src/find_path.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn find_path_inner(
3434
// - if the item is already in scope, return the name under which it is
3535
let def_map = db.crate_def_map(from.krate);
3636
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) {
3838
return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
3939
}
4040

@@ -77,7 +77,7 @@ fn find_path_inner(
7777
let prelude_def_map = db.crate_def_map(prelude_module.krate);
7878
let prelude_scope: &crate::item_scope::ItemScope =
7979
&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) {
8181
if vis.is_visible_from(db, from) {
8282
return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
8383
}
@@ -146,7 +146,7 @@ fn find_importable_locations(
146146
.chain(crate_graph.dependencies(from.krate).map(|dep| dep.crate_id))
147147
{
148148
result.extend(
149-
db.importable_locations_in_crate(item, krate)
149+
importable_locations_in_crate(db, item, krate)
150150
.iter()
151151
.filter(|(_, _, vis)| vis.is_visible_from(db, from))
152152
.map(|(m, n, _)| (*m, n.clone())),
@@ -160,17 +160,16 @@ fn find_importable_locations(
160160
/// non-private `use`s.
161161
///
162162
/// 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
164-
/// need to walk the whole def map for it.
165-
pub(crate) fn importable_locations_in_crate_query(
163+
/// it might be re-exported in other crates.
164+
fn importable_locations_in_crate(
166165
db: &impl DefDatabase,
167166
item: ItemInNs,
168167
krate: CrateId,
169-
) -> std::sync::Arc<[(ModuleId, Name, Visibility)]> {
168+
) -> Vec<(ModuleId, Name, Visibility)> {
170169
let def_map = db.crate_def_map(krate);
171170
let mut result = Vec::new();
172171
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) {
174173
let is_private = if let Visibility::Module(private_to) = vis {
175174
private_to.local_id == local_id
176175
} else {
@@ -192,7 +191,7 @@ pub(crate) fn importable_locations_in_crate_query(
192191
result.push((ModuleId { krate, local_id }, name.clone(), vis));
193192
}
194193
}
195-
result.into()
194+
result
196195
}
197196

198197
#[cfg(test)]

crates/ra_hir_def/src/item_scope.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl ItemScope {
104104
}
105105
}
106106

107-
pub(crate) fn reverse_get(&self, item: ItemInNs) -> Option<(&Name, Visibility)> {
107+
pub(crate) fn name_of(&self, item: ItemInNs) -> Option<(&Name, Visibility)> {
108108
for (name, per_ns) in &self.visible {
109109
if let Some(vis) = item.match_with(*per_ns) {
110110
return Some((name, vis));
@@ -207,8 +207,7 @@ impl ItemInNs {
207207

208208
pub fn as_module_def_id(self) -> Option<ModuleDefId> {
209209
match self {
210-
ItemInNs::Types(t) => Some(t),
211-
ItemInNs::Values(v) => Some(v),
210+
ItemInNs::Types(id) | ItemInNs::Values(id) => Some(id),
212211
ItemInNs::Macros(_) => None,
213212
}
214213
}

crates/ra_hir_def/src/path.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
//! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`.
22
mod lower;
33

4-
use std::{fmt::Display, iter, sync::Arc};
4+
use std::{
5+
fmt::{self, Display},
6+
iter,
7+
sync::Arc,
8+
};
59

610
use hir_expand::{
711
hygiene::Hygiene,
@@ -78,12 +82,6 @@ impl ModPath {
7882
}
7983
self.segments.first()
8084
}
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-
}
8785
}
8886

8987
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -255,7 +253,7 @@ impl From<Name> for ModPath {
255253
}
256254

257255
impl 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 {
259257
let mut first_segment = true;
260258
let mut add_segment = |s| {
261259
if !first_segment {

crates/ra_hir_def/src/resolver.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl Resolver {
128128
path: &ModPath,
129129
shadow: BuiltinShadowMode,
130130
) -> PerNs {
131-
let (item_map, module) = match self.module() {
131+
let (item_map, module) = match self.module_scope() {
132132
Some(it) => it,
133133
None => return PerNs::none(),
134134
};
@@ -239,7 +239,7 @@ impl Resolver {
239239
) -> Option<Visibility> {
240240
match visibility {
241241
RawVisibility::Module(_) => {
242-
let (item_map, module) = match self.module() {
242+
let (item_map, module) = match self.module_scope() {
243243
Some(it) => it,
244244
None => return None,
245245
};
@@ -379,7 +379,7 @@ impl Resolver {
379379
db: &impl DefDatabase,
380380
path: &ModPath,
381381
) -> Option<MacroDefId> {
382-
let (item_map, module) = self.module()?;
382+
let (item_map, module) = self.module_scope()?;
383383
item_map.resolve_path(db, module, &path, BuiltinShadowMode::Other).0.take_macros()
384384
}
385385

@@ -403,21 +403,21 @@ impl Resolver {
403403
traits
404404
}
405405

406-
fn module(&self) -> Option<(&CrateDefMap, LocalModuleId)> {
406+
fn module_scope(&self) -> Option<(&CrateDefMap, LocalModuleId)> {
407407
self.scopes.iter().rev().find_map(|scope| match scope {
408408
Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),
409409

410410
_ => None,
411411
})
412412
}
413413

414-
pub fn module_id(&self) -> Option<ModuleId> {
415-
let (def_map, local_id) = self.module()?;
414+
pub fn module(&self) -> Option<ModuleId> {
415+
let (def_map, local_id) = self.module_scope()?;
416416
Some(ModuleId { krate: def_map.krate, local_id })
417417
}
418418

419419
pub fn krate(&self) -> Option<CrateId> {
420-
self.module().map(|t| t.0.krate)
420+
self.module_scope().map(|t| t.0.krate)
421421
}
422422

423423
pub fn where_predicates_in_scope<'a>(

crates/ra_hir_expand/src/name.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ impl Name {
3737
Name(Repr::TupleField(idx))
3838
}
3939

40-
pub fn for_crate_dependency(dep: &ra_db::Dependency) -> Name {
41-
Name::new_text(dep.name.clone())
42-
}
43-
4440
/// Shortcut to create inline plain text name
4541
const fn new_inline_ascii(text: &[u8]) -> Name {
4642
Name::new_text(SmolStr::new_inline_from_ascii(text.len(), text))

0 commit comments

Comments
 (0)