4514: find_path cleanups r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-05-19 15:03:41 +00:00 committed by GitHub
commit f6e70e751a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 21 deletions

View file

@ -112,7 +112,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
#[salsa::invoke(Documentation::documentation_query)] #[salsa::invoke(Documentation::documentation_query)]
fn documentation(&self, def: AttrDefId) -> Option<Documentation>; fn documentation(&self, def: AttrDefId) -> Option<Documentation>;
#[salsa::invoke(find_path::importable_locations_in_crate)] #[salsa::invoke(find_path::importable_locations_of_query)]
fn importable_locations_of( fn importable_locations_of(
&self, &self,
item: ItemInNs, item: ItemInNs,

View file

@ -1,5 +1,11 @@
//! An algorithm to find a path to refer to a certain item. //! An algorithm to find a path to refer to a certain item.
use std::sync::Arc;
use hir_expand::name::{known, AsName, Name};
use ra_prof::profile;
use test_utils::tested_by;
use crate::{ use crate::{
db::DefDatabase, db::DefDatabase,
item_scope::ItemInNs, item_scope::ItemInNs,
@ -7,26 +13,28 @@ use crate::{
visibility::Visibility, visibility::Visibility,
CrateId, ModuleDefId, ModuleId, CrateId, ModuleDefId, ModuleId,
}; };
use hir_expand::name::{known, AsName, Name};
use std::sync::Arc; // FIXME: handle local items
use test_utils::tested_by;
/// Find a path that can be used to refer to a certain item. This can depend on
/// *from where* you're referring to the item, hence the `from` parameter.
pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
let _p = profile("find_path");
find_path_inner(db, item, from, MAX_PATH_LEN)
}
const MAX_PATH_LEN: usize = 15; const MAX_PATH_LEN: usize = 15;
impl ModPath { impl ModPath {
fn starts_with_std(&self) -> bool { fn starts_with_std(&self) -> bool {
self.segments.first().filter(|&first_segment| first_segment == &known::std).is_some() self.segments.first() == Some(&known::std)
} }
// When std library is present, paths starting with `std::` // When std library is present, paths starting with `std::`
// should be preferred over paths starting with `core::` and `alloc::` // should be preferred over paths starting with `core::` and `alloc::`
fn can_start_with_std(&self) -> bool { fn can_start_with_std(&self) -> bool {
self.segments let first_segment = self.segments.first();
.first() first_segment == Some(&known::alloc) || first_segment == Some(&known::core)
.filter(|&first_segment| {
first_segment == &known::alloc || first_segment == &known::core
})
.is_some()
} }
fn len(&self) -> usize { fn len(&self) -> usize {
@ -41,15 +49,6 @@ impl ModPath {
} }
} }
// FIXME: handle local items
/// Find a path that can be used to refer to a certain item. This can depend on
/// *from where* you're referring to the item, hence the `from` parameter.
pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
let _p = ra_prof::profile("find_path");
find_path_inner(db, item, from, MAX_PATH_LEN)
}
fn find_path_inner( fn find_path_inner(
db: &dyn DefDatabase, db: &dyn DefDatabase,
item: ItemInNs, item: ItemInNs,
@ -215,11 +214,12 @@ fn find_importable_locations(
/// ///
/// Note that the crate doesn't need to be the one in which the item is defined; /// Note that the crate doesn't need to be the one in which the item is defined;
/// it might be re-exported in other crates. /// it might be re-exported in other crates.
pub(crate) fn importable_locations_in_crate( pub(crate) fn importable_locations_of_query(
db: &dyn DefDatabase, db: &dyn DefDatabase,
item: ItemInNs, item: ItemInNs,
krate: CrateId, krate: CrateId,
) -> Arc<[(ModuleId, Name, Visibility)]> { ) -> Arc<[(ModuleId, Name, Visibility)]> {
let _p = profile("importable_locations_of_query");
let def_map = db.crate_def_map(krate); let def_map = db.crate_def_map(krate);
let mut result = Vec::new(); let mut result = Vec::new();
for (local_id, data) in def_map.modules.iter() { for (local_id, data) in def_map.modules.iter() {