mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 07:04:22 +00:00
Merge #3745
3745: Fix merge-imports assist for wildcard imports r=matklad a=piotr-szpetkowski Refs #3728 Besides the case mentioned in issue merging two diff-prefix wildcard uses will now work as well e.g. ```rust use std::cell::*; use std::str::*; ``` will translate into: ```rust use std::{cell::*, str::*} ``` I'd also like to explore usage of the `merge-imports` for same-prefix uses to simplify redundancy, but it seems like an idea for another issue and I'm not sure if it's something that this assist should do e.g.: ```rust use std::cell::Cell; use std::cell::*; ``` into: ```rust use std::cell::*; ``` Co-authored-by: Piotr Szpetkowski <piotr.szpetkowski@pyquest.space>
This commit is contained in:
commit
a1fea0d34e
3 changed files with 36 additions and 2 deletions
|
@ -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(
|
||||
|
|
|
@ -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<ast::Path> {
|
||||
let parent = prefix.parent_path()?;
|
||||
|
|
|
@ -29,12 +29,17 @@ pub fn use_tree(
|
|||
path: ast::Path,
|
||||
use_tree_list: Option<ast::UseTreeList>,
|
||||
alias: Option<ast::Alias>,
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue