From 61e074f016ca6df88b6f88c822a413c4f5022784 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 7 May 2022 15:23:25 +0200 Subject: [PATCH] fix: Allow auto importing starting segments of use items --- .../ide-assists/src/handlers/auto_import.rs | 52 ++++++++++++++----- .../ide-assists/src/handlers/qualify_path.rs | 52 ++++++++++++++----- crates/ide-db/src/imports/import_assets.rs | 9 +++- 3 files changed, 84 insertions(+), 29 deletions(-) diff --git a/crates/ide-assists/src/handlers/auto_import.rs b/crates/ide-assists/src/handlers/auto_import.rs index 874563c6f7..0a0dafb35e 100644 --- a/crates/ide-assists/src/handlers/auto_import.rs +++ b/crates/ide-assists/src/handlers/auto_import.rs @@ -373,19 +373,6 @@ mod baz { ); } - #[test] - fn not_applicable_in_import_statements() { - check_assist_not_applicable( - auto_import, - r" - use PubStruct$0; - - pub mod PubMod { - pub struct PubStruct; - }", - ); - } - #[test] fn function_import() { check_assist( @@ -1121,4 +1108,43 @@ struct Foo; "#, ); } + + #[test] + fn works_in_use_start() { + check_assist( + auto_import, + r#" +mod bar { + pub mod foo { + pub struct Foo; + } +} +use foo$0::Foo; +"#, + r#" +mod bar { + pub mod foo { + pub struct Foo; + } +} +use bar::foo; +use foo::Foo; +"#, + ); + } + + #[test] + fn not_applicable_in_non_start_use() { + check_assist_not_applicable( + auto_import, + r" +mod bar { + pub mod foo { + pub struct Foo; + } +} +use foo::Foo$0; +", + ); + } } diff --git a/crates/ide-assists/src/handlers/qualify_path.rs b/crates/ide-assists/src/handlers/qualify_path.rs index 5deb60f57b..8d2293d224 100644 --- a/crates/ide-assists/src/handlers/qualify_path.rs +++ b/crates/ide-assists/src/handlers/qualify_path.rs @@ -381,20 +381,6 @@ pub mod PubMod { check_assist_not_applicable(qualify_path, r#"PubStruct$0"#); } - #[test] - fn not_applicable_in_import_statements() { - check_assist_not_applicable( - qualify_path, - r#" -use PubStruct$0; - -pub mod PubMod { - pub struct PubStruct; -} -"#, - ); - } - #[test] fn qualify_function() { check_assist( @@ -1270,4 +1256,42 @@ struct Foo; "#, ); } + + #[test] + fn works_in_use_start() { + check_assist( + qualify_path, + r#" +mod bar { + pub mod foo { + pub struct Foo; + } +} +use foo$0::Foo; +"#, + r#" +mod bar { + pub mod foo { + pub struct Foo; + } +} +use bar::foo::Foo; +"#, + ); + } + + #[test] + fn not_applicable_in_non_start_use() { + check_assist_not_applicable( + qualify_path, + r" +mod bar { + pub mod foo { + pub struct Foo; + } +} +use foo::Foo$0; +", + ); + } } diff --git a/crates/ide-db/src/imports/import_assets.rs b/crates/ide-db/src/imports/import_assets.rs index f8cfe15c89..81467ab07a 100644 --- a/crates/ide-db/src/imports/import_assets.rs +++ b/crates/ide-db/src/imports/import_assets.rs @@ -114,8 +114,13 @@ impl ImportAssets { sema: &Semantics, ) -> Option { let candidate_node = fully_qualified_path.syntax().clone(); - if candidate_node.ancestors().find_map(ast::Use::cast).is_some() { - return None; + if let Some(use_tree) = candidate_node.ancestors().find_map(ast::UseTree::cast) { + // Path is inside a use tree, then only continue if it is the first segment of a use statement. + if use_tree.syntax().parent().and_then(ast::Use::cast).is_none() + || fully_qualified_path.qualifier().is_some() + { + return None; + } } Some(Self { import_candidate: ImportCandidate::for_regular_path(sema, fully_qualified_path)?,