mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Draft the qualifier import resolution
This commit is contained in:
parent
c395c3311d
commit
309421c117
4 changed files with 224 additions and 43 deletions
|
@ -1,7 +1,10 @@
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
use hir::AsAssocItem;
|
use hir::AsAssocItem;
|
||||||
use ide_db::helpers::{import_assets::ImportCandidate, mod_path_to_ast};
|
use ide_db::helpers::{
|
||||||
|
import_assets::{ImportCandidate, Qualifier},
|
||||||
|
mod_path_to_ast,
|
||||||
|
};
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast,
|
ast,
|
||||||
|
@ -45,7 +48,7 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
||||||
|
|
||||||
let qualify_candidate = match candidate {
|
let qualify_candidate = match candidate {
|
||||||
ImportCandidate::Path(candidate) => {
|
ImportCandidate::Path(candidate) => {
|
||||||
if candidate.unresolved_qualifier.is_some() {
|
if !matches!(candidate.qualifier, Qualifier::Absent) {
|
||||||
cov_mark::hit!(qualify_path_qualifier_start);
|
cov_mark::hit!(qualify_path_qualifier_start);
|
||||||
let path = ast::Path::cast(syntax_under_caret)?;
|
let path = ast::Path::cast(syntax_under_caret)?;
|
||||||
let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?);
|
let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?);
|
||||||
|
@ -192,7 +195,7 @@ fn group_label(candidate: &ImportCandidate) -> GroupLabel {
|
||||||
fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String {
|
fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String {
|
||||||
match candidate {
|
match candidate {
|
||||||
ImportCandidate::Path(candidate) => {
|
ImportCandidate::Path(candidate) => {
|
||||||
if candidate.unresolved_qualifier.is_some() {
|
if !matches!(candidate.qualifier, Qualifier::Absent) {
|
||||||
format!("Qualify with `{}`", &import)
|
format!("Qualify with `{}`", &import)
|
||||||
} else {
|
} else {
|
||||||
format!("Qualify as `{}`", &import)
|
format!("Qualify as `{}`", &import)
|
||||||
|
|
|
@ -775,18 +775,92 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unresolved_qualifiers() {
|
fn unresolved_qualifier() {
|
||||||
|
check_edit(
|
||||||
|
"Item",
|
||||||
|
r#"
|
||||||
|
mod foo {
|
||||||
|
pub mod bar {
|
||||||
|
pub mod baz {
|
||||||
|
pub struct Item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
bar::baz::Ite$0
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
use foo::bar;
|
||||||
|
|
||||||
|
mod foo {
|
||||||
|
pub mod bar {
|
||||||
|
pub mod baz {
|
||||||
|
pub struct Item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
bar::baz::Item
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unresolved_assoc_item_container() {
|
||||||
|
check_edit(
|
||||||
|
"Item",
|
||||||
|
r#"
|
||||||
|
mod foo {
|
||||||
|
pub struct Item;
|
||||||
|
|
||||||
|
impl Item {
|
||||||
|
pub const TEST_ASSOC: usize = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
Item::TEST_A$0;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
use foo::Item;
|
||||||
|
|
||||||
|
mod foo {
|
||||||
|
pub struct Item;
|
||||||
|
|
||||||
|
impl Item {
|
||||||
|
pub const TEST_ASSOC: usize = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
Item::TEST_ASSOC
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unresolved_assoc_item_container_with_path() {
|
||||||
check_edit(
|
check_edit(
|
||||||
"Item",
|
"Item",
|
||||||
r#"
|
r#"
|
||||||
mod foo {
|
mod foo {
|
||||||
pub mod bar {
|
pub mod bar {
|
||||||
pub struct Item;
|
pub struct Item;
|
||||||
|
|
||||||
|
impl Item {
|
||||||
|
pub const TEST_ASSOC: usize = 3;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
bar::Ite$0
|
bar::Item::TEST_A$0;
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r#"
|
r#"
|
||||||
|
@ -795,11 +869,15 @@ use foo::bar;
|
||||||
mod foo {
|
mod foo {
|
||||||
pub mod bar {
|
pub mod bar {
|
||||||
pub struct Item;
|
pub struct Item;
|
||||||
|
|
||||||
|
impl Item {
|
||||||
|
pub const TEST_ASSOC: usize = 3;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
bar::Item
|
bar::Item::TEST_ASSOC
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
//! Look up accessible paths for items.
|
//! Look up accessible paths for items.
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{
|
use hir::{
|
||||||
AsAssocItem, AssocItem, Crate, ItemInNs, MacroDef, ModPath, Module, ModuleDef, PrefixKind,
|
AsAssocItem, AssocItem, Crate, ItemInNs, MacroDef, ModPath, Module, ModuleDef, Name,
|
||||||
Semantics,
|
PrefixKind, Semantics,
|
||||||
};
|
};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
use syntax::{ast, AstNode};
|
use syntax::{ast, AstNode};
|
||||||
|
@ -34,10 +34,16 @@ pub struct TraitImportCandidate {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PathImportCandidate {
|
pub struct PathImportCandidate {
|
||||||
pub unresolved_qualifier: Option<ast::Path>,
|
pub qualifier: Qualifier,
|
||||||
pub name: NameToImport,
|
pub name: NameToImport,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Qualifier {
|
||||||
|
Absent,
|
||||||
|
FirstSegmentUnresolved(ast::PathSegment, ast::Path),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum NameToImport {
|
pub enum NameToImport {
|
||||||
Exact(String),
|
Exact(String),
|
||||||
|
@ -162,8 +168,9 @@ impl ImportAssets {
|
||||||
let (assoc_item_search, limit) = if self.import_candidate.is_trait_candidate() {
|
let (assoc_item_search, limit) = if self.import_candidate.is_trait_candidate() {
|
||||||
(AssocItemSearch::AssocItemsOnly, None)
|
(AssocItemSearch::AssocItemsOnly, None)
|
||||||
} else {
|
} else {
|
||||||
(AssocItemSearch::Exclude, Some(DEFAULT_QUERY_SEARCH_LIMIT))
|
(AssocItemSearch::Include, Some(DEFAULT_QUERY_SEARCH_LIMIT))
|
||||||
};
|
};
|
||||||
|
|
||||||
imports_locator::find_similar_imports(
|
imports_locator::find_similar_imports(
|
||||||
sema,
|
sema,
|
||||||
current_crate,
|
current_crate,
|
||||||
|
@ -192,17 +199,16 @@ impl ImportAssets {
|
||||||
let db = sema.db;
|
let db = sema.db;
|
||||||
|
|
||||||
match &self.import_candidate {
|
match &self.import_candidate {
|
||||||
ImportCandidate::Path(path_candidate) => Box::new(path_applicable_items(
|
ImportCandidate::Path(path_candidate) => Box::new(
|
||||||
sema,
|
path_applicable_items(
|
||||||
path_candidate,
|
db,
|
||||||
unfiltered_defs
|
path_candidate,
|
||||||
.into_iter()
|
&self.module_with_candidate,
|
||||||
.map(|def| def.either(ItemInNs::from, ItemInNs::from))
|
prefixed,
|
||||||
.filter_map(move |item_to_search| {
|
unfiltered_defs,
|
||||||
get_mod_path(db, item_to_search, &self.module_with_candidate, prefixed)
|
)
|
||||||
.zip(Some(item_to_search))
|
.into_iter(),
|
||||||
}),
|
),
|
||||||
)),
|
|
||||||
ImportCandidate::TraitAssocItem(trait_candidate) => Box::new(
|
ImportCandidate::TraitAssocItem(trait_candidate) => Box::new(
|
||||||
trait_applicable_defs(db, current_crate, trait_candidate, true, unfiltered_defs)
|
trait_applicable_defs(db, current_crate, trait_candidate, true, unfiltered_defs)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -224,27 +230,110 @@ impl ImportAssets {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_applicable_items<'a>(
|
fn path_applicable_items<'a>(
|
||||||
sema: &'a Semantics<RootDatabase>,
|
db: &'a RootDatabase,
|
||||||
path_candidate: &PathImportCandidate,
|
path_candidate: &'a PathImportCandidate,
|
||||||
unfiltered_defs: impl Iterator<Item = (ModPath, ItemInNs)> + 'a,
|
module_with_candidate: &hir::Module,
|
||||||
) -> Box<dyn Iterator<Item = (ModPath, ItemInNs)> + 'a> {
|
prefixed: Option<hir::PrefixKind>,
|
||||||
let unresolved_qualifier = match &path_candidate.unresolved_qualifier {
|
unfiltered_defs: impl Iterator<Item = Either<ModuleDef, MacroDef>> + 'a,
|
||||||
Some(qualifier) => qualifier,
|
) -> FxHashSet<(ModPath, ItemInNs)> {
|
||||||
None => {
|
let applicable_items = unfiltered_defs
|
||||||
return Box::new(unfiltered_defs);
|
.filter_map(|def| {
|
||||||
|
let (assoc_original, candidate) = match def {
|
||||||
|
Either::Left(module_def) => match module_def.as_assoc_item(db) {
|
||||||
|
Some(assoc_item) => match assoc_item.container(db) {
|
||||||
|
hir::AssocItemContainer::Trait(trait_) => {
|
||||||
|
(Some(module_def), ItemInNs::from(ModuleDef::from(trait_)))
|
||||||
|
}
|
||||||
|
hir::AssocItemContainer::Impl(impl_) => (
|
||||||
|
Some(module_def),
|
||||||
|
ItemInNs::from(ModuleDef::from(impl_.target_ty(db).as_adt()?)),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
None => (None, ItemInNs::from(module_def)),
|
||||||
|
},
|
||||||
|
Either::Right(macro_def) => (None, ItemInNs::from(macro_def)),
|
||||||
|
};
|
||||||
|
Some((assoc_original, candidate))
|
||||||
|
})
|
||||||
|
.filter_map(|(assoc_original, candidate)| {
|
||||||
|
get_mod_path(db, candidate, module_with_candidate, prefixed)
|
||||||
|
.zip(Some((assoc_original, candidate)))
|
||||||
|
});
|
||||||
|
|
||||||
|
let (unresolved_first_segment, unresolved_qualifier) = match &path_candidate.qualifier {
|
||||||
|
Qualifier::Absent => {
|
||||||
|
return applicable_items
|
||||||
|
.map(|(candidate_path, (_, candidate))| (candidate_path, candidate))
|
||||||
|
.collect();
|
||||||
}
|
}
|
||||||
|
Qualifier::FirstSegmentUnresolved(first_segment, qualifier) => (first_segment, qualifier),
|
||||||
};
|
};
|
||||||
|
|
||||||
let qualifier_string = unresolved_qualifier.to_string();
|
// TODO kb need to remove turbofish from the qualifier, maybe use the segments instead?
|
||||||
Box::new(unfiltered_defs.filter(move |(candidate_path, _)| {
|
let unresolved_qualifier_string = unresolved_qualifier.to_string();
|
||||||
let mut candidate_qualifier = candidate_path.clone();
|
let unresolved_first_segment_string = unresolved_first_segment.to_string();
|
||||||
candidate_qualifier.pop_segment();
|
|
||||||
|
|
||||||
// TODO kb
|
applicable_items
|
||||||
// * take 1st segment of `unresolved_qualifier` and return it instead of the original `ItemInNs`
|
.filter(|(candidate_path, _)| {
|
||||||
// * Update `ModPath`: pop until 1st segment of `unresolved_qualifier` reached (do not rely on name comparison, nested mod names can repeat)
|
let candidate_path_string = candidate_path.to_string();
|
||||||
candidate_qualifier.to_string().ends_with(&qualifier_string)
|
candidate_path_string.contains(&unresolved_qualifier_string)
|
||||||
}))
|
&& candidate_path_string.contains(&unresolved_first_segment_string)
|
||||||
|
})
|
||||||
|
// TODO kb need to adjust the return type: I get the results rendered rather badly
|
||||||
|
.filter_map(|(candidate_path, (assoc_original, candidate))| {
|
||||||
|
if let Some(assoc_original) = assoc_original {
|
||||||
|
if item_name(db, candidate)?.to_string() == unresolved_first_segment_string {
|
||||||
|
return Some((candidate_path, ItemInNs::from(assoc_original)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let matching_module =
|
||||||
|
module_with_matching_name(db, &unresolved_first_segment_string, candidate)?;
|
||||||
|
let path = get_mod_path(
|
||||||
|
db,
|
||||||
|
ItemInNs::from(ModuleDef::from(matching_module)),
|
||||||
|
module_with_candidate,
|
||||||
|
prefixed,
|
||||||
|
)?;
|
||||||
|
Some((path, candidate))
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
|
||||||
|
match item {
|
||||||
|
ItemInNs::Types(module_def_id) => ModuleDef::from(module_def_id).name(db),
|
||||||
|
ItemInNs::Values(module_def_id) => ModuleDef::from(module_def_id).name(db),
|
||||||
|
ItemInNs::Macros(macro_def_id) => MacroDef::from(macro_def_id).name(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn item_module(db: &RootDatabase, item: ItemInNs) -> Option<Module> {
|
||||||
|
match item {
|
||||||
|
ItemInNs::Types(module_def_id) => ModuleDef::from(module_def_id).module(db),
|
||||||
|
ItemInNs::Values(module_def_id) => ModuleDef::from(module_def_id).module(db),
|
||||||
|
ItemInNs::Macros(macro_def_id) => MacroDef::from(macro_def_id).module(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn module_with_matching_name(
|
||||||
|
db: &RootDatabase,
|
||||||
|
unresolved_first_segment_string: &str,
|
||||||
|
candidate: ItemInNs,
|
||||||
|
) -> Option<Module> {
|
||||||
|
let mut current_module = item_module(db, candidate);
|
||||||
|
while let Some(module) = current_module {
|
||||||
|
match module.name(db) {
|
||||||
|
Some(module_name) => {
|
||||||
|
if module_name.to_string().as_str() == unresolved_first_segment_string {
|
||||||
|
return Some(module);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
current_module = module.parent(db);
|
||||||
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn trait_applicable_defs<'a>(
|
fn trait_applicable_defs<'a>(
|
||||||
|
@ -367,10 +456,20 @@ fn path_import_candidate(
|
||||||
) -> Option<ImportCandidate> {
|
) -> Option<ImportCandidate> {
|
||||||
Some(match qualifier {
|
Some(match qualifier {
|
||||||
Some(qualifier) => match sema.resolve_path(&qualifier) {
|
Some(qualifier) => match sema.resolve_path(&qualifier) {
|
||||||
None => ImportCandidate::Path(PathImportCandidate {
|
None => {
|
||||||
unresolved_qualifier: Some(qualifier),
|
let qualifier_start =
|
||||||
name,
|
qualifier.syntax().descendants().find_map(ast::PathSegment::cast)?;
|
||||||
}),
|
let qualifier_start_path =
|
||||||
|
qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?;
|
||||||
|
if sema.resolve_path(&qualifier_start_path).is_none() {
|
||||||
|
ImportCandidate::Path(PathImportCandidate {
|
||||||
|
qualifier: Qualifier::FirstSegmentUnresolved(qualifier_start, qualifier),
|
||||||
|
name,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
Some(hir::PathResolution::Def(hir::ModuleDef::Adt(assoc_item_path))) => {
|
Some(hir::PathResolution::Def(hir::ModuleDef::Adt(assoc_item_path))) => {
|
||||||
ImportCandidate::TraitAssocItem(TraitImportCandidate {
|
ImportCandidate::TraitAssocItem(TraitImportCandidate {
|
||||||
receiver_ty: assoc_item_path.ty(sema.db),
|
receiver_ty: assoc_item_path.ty(sema.db),
|
||||||
|
@ -379,6 +478,6 @@ fn path_import_candidate(
|
||||||
}
|
}
|
||||||
Some(_) => return None,
|
Some(_) => return None,
|
||||||
},
|
},
|
||||||
None => ImportCandidate::Path(PathImportCandidate { unresolved_qualifier: None, name }),
|
None => ImportCandidate::Path(PathImportCandidate { qualifier: Qualifier::Absent, name }),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ pub fn find_exact_imports<'a>(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum AssocItemSearch {
|
pub enum AssocItemSearch {
|
||||||
Include,
|
Include,
|
||||||
Exclude,
|
Exclude,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue