Fix merge-imports assist for wildcard imports

This commit is contained in:
Piotr Szpetkowski 2020-03-27 17:28:25 +01:00
parent 75f6ab208a
commit f016d8b900
No known key found for this signature in database
GPG key ID: D3FEC6AE666BC5B1
3 changed files with 36 additions and 2 deletions

View file

@ -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] #[test]
fn removes_just_enough_whitespace() { fn removes_just_enough_whitespace() {
check_assist( check_assist(

View file

@ -302,9 +302,10 @@ impl ast::UseTree {
Some(it) => it, Some(it) => it,
None => return self.clone(), 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)); 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<ast::Path> { fn split_path_prefix(prefix: &ast::Path) -> Option<ast::Path> {
let parent = prefix.parent_path()?; let parent = prefix.parent_path()?;

View file

@ -29,12 +29,17 @@ pub fn use_tree(
path: ast::Path, path: ast::Path,
use_tree_list: Option<ast::UseTreeList>, use_tree_list: Option<ast::UseTreeList>,
alias: Option<ast::Alias>, alias: Option<ast::Alias>,
add_star: bool,
) -> ast::UseTree { ) -> ast::UseTree {
let mut buf = "use ".to_string(); let mut buf = "use ".to_string();
buf += &path.syntax().to_string(); buf += &path.syntax().to_string();
if let Some(use_tree_list) = use_tree_list { if let Some(use_tree_list) = use_tree_list {
buf += &format!("::{}", use_tree_list); buf += &format!("::{}", use_tree_list);
} }
if add_star {
buf += "::*";
}
if let Some(alias) = alias { if let Some(alias) = alias {
buf += &format!(" {}", alias); buf += &format!(" {}", alias);
} }