From 34464ede3f57cbab4d6d1a67f36252cc22c02765 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 18 Mar 2021 21:36:52 +0100 Subject: Fix associated items not being appended to paths in import_assets --- crates/ide_assists/src/handlers/qualify_path.rs | 37 +++++++++++++++++++++++-- crates/ide_db/src/helpers/import_assets.rs | 6 +++- 2 files changed, 40 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ide_assists/src/handlers/qualify_path.rs b/crates/ide_assists/src/handlers/qualify_path.rs index 30b23da6c..40571e52b 100644 --- a/crates/ide_assists/src/handlers/qualify_path.rs +++ b/crates/ide_assists/src/handlers/qualify_path.rs @@ -208,8 +208,10 @@ fn label(candidate: &ImportCandidate, import: &LocatedImport) -> String { format!("Qualify as `{}`", import.import_path) } } - ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", import.import_path), - ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", import.import_path), + ImportCandidate::TraitAssocItem(_) => { + format!("Qualify with `{}`", import.import_path) + } + ImportCandidate::TraitMethod(_) => format!("Qualify with `{}`", import.import_path), } } @@ -543,6 +545,37 @@ fn main() { ); } + #[test] + fn associated_struct_const_unqualified() { + check_assist( + qualify_path, + r" + mod test_mod { + pub struct TestStruct {} + impl TestStruct { + const TEST_CONST: u8 = 42; + } + } + + fn main() { + TEST_CONST$0 + } + ", + r" + mod test_mod { + pub struct TestStruct {} + impl TestStruct { + const TEST_CONST: u8 = 42; + } + } + + fn main() { + test_mod::TestStruct::TEST_CONST + } + ", + ); + } + #[test] fn associated_trait_function() { check_assist( diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs index e03ccd351..7c8844e95 100644 --- a/crates/ide_db/src/helpers/import_assets.rs +++ b/crates/ide_db/src/helpers/import_assets.rs @@ -304,7 +304,11 @@ fn path_applicable_imports( return items_with_candidate_name .into_iter() .filter_map(|item| { - Some(LocatedImport::new(mod_path(item)?, item, item, mod_path(item))) + let mut mod_path = mod_path(item)?; + if let Some(assoc_item) = item_as_assoc(db, item) { + mod_path.push_segment(assoc_item.name(db)?); + } + Some(LocatedImport::new(mod_path.clone(), item, item, Some(mod_path))) }) .collect(); } -- cgit v1.2.3