Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ide_assists/src/handlers/qualify_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ fn main() {
}

#[test]
#[ignore = "FIXME: non-trait assoc items completion is unsupported yet, see FIXME in the import_assets.rs for more details"]
fn associated_struct_const_unqualified() {
check_assist(
qualify_path,
Expand Down
35 changes: 20 additions & 15 deletions crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use hir::ModuleDef;
use ide_db::helpers::mod_path_to_ast;
use ide_db::helpers::{import_assets::NameToImport, mod_path_to_ast};
use ide_db::items_locator;
use itertools::Itertools;
use syntax::{
Expand Down Expand Up @@ -65,20 +65,25 @@ pub(crate) fn replace_derive_with_manual_impl(
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
let current_crate = current_module.krate();

let found_traits =
items_locator::with_exact_name(&ctx.sema, current_crate, trait_token.text().to_string())
.into_iter()
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
ModuleDef::Trait(trait_) => Some(trait_),
_ => None,
})
.flat_map(|trait_| {
current_module
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
.as_ref()
.map(mod_path_to_ast)
.zip(Some(trait_))
});
let found_traits = items_locator::items_with_name(
&ctx.sema,
current_crate,
NameToImport::Exact(trait_token.text().to_string()),
items_locator::AssocItemSearch::Exclude,
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT),
)
.into_iter()
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
ModuleDef::Trait(trait_) => Some(trait_),
_ => None,
})
.flat_map(|trait_| {
current_module
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
.as_ref()
.map(mod_path_to_ast)
.zip(Some(trait_))
});

let mut no_traits_found = true;
for (trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {
Expand Down
32 changes: 32 additions & 0 deletions crates/ide_completion/src/completions/flyimport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,38 @@ mod foo {

fn main() {
bar::Ass$0
}"#,
expect![[]],
)
}

#[test]
fn unqualified_assoc_items_are_omitted() {
check(
r#"
mod something {
pub trait BaseTrait {
fn test_function() -> i32;
}

pub struct Item1;
pub struct Item2;

impl BaseTrait for Item1 {
fn test_function() -> i32 {
1
}
}

impl BaseTrait for Item2 {
fn test_function() -> i32 {
2
}
}
}

fn main() {
test_f$0
}"#,
expect![[]],
)
Expand Down
28 changes: 18 additions & 10 deletions crates/ide_completion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ mod completions;
use completions::flyimport::position_for_import;
use ide_db::{
base_db::FilePosition,
helpers::{import_assets::LocatedImport, insert_use::ImportScope},
helpers::{
import_assets::{LocatedImport, NameToImport},
insert_use::ImportScope,
},
items_locator, RootDatabase,
};
use text_edit::TextEdit;
Expand Down Expand Up @@ -149,15 +152,20 @@ pub fn resolve_completion_edits(
let current_module = ctx.sema.scope(position_for_import).module()?;
let current_crate = current_module.krate();

let (import_path, item_to_import) =
items_locator::with_exact_name(&ctx.sema, current_crate, imported_name)
.into_iter()
.filter_map(|candidate| {
current_module
.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
.zip(Some(candidate))
})
.find(|(mod_path, _)| mod_path.to_string() == full_import_path)?;
let (import_path, item_to_import) = items_locator::items_with_name(
&ctx.sema,
current_crate,
NameToImport::Exact(imported_name),
items_locator::AssocItemSearch::Include,
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT),
)
.into_iter()
.filter_map(|candidate| {
current_module
.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
.zip(Some(candidate))
})
.find(|(mod_path, _)| mod_path.to_string() == full_import_path)?;
let import =
LocatedImport::new(import_path.clone(), item_to_import, item_to_import, Some(import_path));

Expand Down
Loading