From a9669a5505939c28fd97e53e1bbb1571c2408cf1 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 2 Feb 2020 10:28:16 +0200 Subject: Merge imports when auto importing --- crates/ra_assists/src/assists/auto_import.rs | 36 +++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'crates/ra_assists/src/assists') diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index 932a52bff..0d15adb87 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs @@ -79,12 +79,8 @@ pub(crate) fn auto_import( fn import_to_action(import: String, position: &SyntaxNode, anchor: &SyntaxNode) -> ActionBuilder { let mut action_builder = ActionBuilder::default(); action_builder.label(format!("Import `{}`", &import)); - auto_import_text_edit( - position, - anchor, - &[SmolStr::new(import)], - action_builder.text_edit_builder(), - ); + let import_segments = import.split("::").map(SmolStr::new).collect::>(); + auto_import_text_edit(position, anchor, &import_segments, action_builder.text_edit_builder()); action_builder } @@ -120,6 +116,34 @@ mod tests { ); } + #[test] + fn auto_imports_are_merged() { + check_assist_with_imports_locator( + auto_import, + TestImportsLocator::new, + r" + use PubMod::PubStruct1; + + PubStruct2<|> + + pub mod PubMod { + pub struct PubStruct1; + pub struct PubStruct2; + } + ", + r" + use PubMod::{PubStruct2, PubStruct1}; + + PubStruct2<|> + + pub mod PubMod { + pub struct PubStruct1; + pub struct PubStruct2; + } + ", + ); + } + #[test] fn applicable_when_found_multiple_imports() { check_assist_with_imports_locator( -- cgit v1.2.3