From f016d8b900792c8ae4fce268920edea3035b6559 Mon Sep 17 00:00:00 2001 From: Piotr Szpetkowski Date: Fri, 27 Mar 2020 17:28:25 +0100 Subject: Fix merge-imports assist for wildcard imports --- crates/ra_assists/src/handlers/merge_imports.rs | 28 +++++++++++++++++++++++++ crates/ra_syntax/src/ast/edit.rs | 5 +++-- crates/ra_syntax/src/ast/make.rs | 5 +++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 9c57d1e30..5d4b74e56 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -170,6 +170,34 @@ use std::{fmt::<|>{Display, Debug}}; ); } + #[test] + fn test_merge_single_wildcard_diff_prefixes() { + check_assist( + merge_imports, + r" +use std<|>::cell::*; +use std::str; +", + r" +use std<|>::{cell::*, str}; +", + ) + } + + #[test] + fn test_merge_both_wildcard_diff_prefixes() { + check_assist( + merge_imports, + r" +use std<|>::cell::*; +use std::str::*; +", + r" +use std<|>::{cell::*, str::*}; +", + ) + } + #[test] fn removes_just_enough_whitespace() { check_assist( diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index bdaecdc43..2304e00cf 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -302,9 +302,10 @@ impl ast::UseTree { Some(it) => it, None => return self.clone(), }; - let use_tree = make::use_tree(suffix.clone(), self.use_tree_list(), self.alias()); + let use_tree = + make::use_tree(suffix.clone(), self.use_tree_list(), self.alias(), self.has_star()); let nested = make::use_tree_list(iter::once(use_tree)); - return make::use_tree(prefix.clone(), Some(nested), None); + return make::use_tree(prefix.clone(), Some(nested), None, false); fn split_path_prefix(prefix: &ast::Path) -> Option { let parent = prefix.parent_path()?; diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 4621c9888..dbf8e6370 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -29,12 +29,17 @@ pub fn use_tree( path: ast::Path, use_tree_list: Option, alias: Option, + add_star: bool, ) -> ast::UseTree { let mut buf = "use ".to_string(); buf += &path.syntax().to_string(); if let Some(use_tree_list) = use_tree_list { buf += &format!("::{}", use_tree_list); } + if add_star { + buf += "::*"; + } + if let Some(alias) = alias { buf += &format!(" {}", alias); } -- cgit v1.2.3