Skip to content

Commit a1fea0d

Browse files
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 <[email protected]>
2 parents 75f6ab2 + f016d8b commit a1fea0d

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

crates/ra_assists/src/handlers/merge_imports.rs

+28
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,34 @@ use std::{fmt::<|>{Display, Debug}};
170170
);
171171
}
172172

173+
#[test]
174+
fn test_merge_single_wildcard_diff_prefixes() {
175+
check_assist(
176+
merge_imports,
177+
r"
178+
use std<|>::cell::*;
179+
use std::str;
180+
",
181+
r"
182+
use std<|>::{cell::*, str};
183+
",
184+
)
185+
}
186+
187+
#[test]
188+
fn test_merge_both_wildcard_diff_prefixes() {
189+
check_assist(
190+
merge_imports,
191+
r"
192+
use std<|>::cell::*;
193+
use std::str::*;
194+
",
195+
r"
196+
use std<|>::{cell::*, str::*};
197+
",
198+
)
199+
}
200+
173201
#[test]
174202
fn removes_just_enough_whitespace() {
175203
check_assist(

crates/ra_syntax/src/ast/edit.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,10 @@ impl ast::UseTree {
302302
Some(it) => it,
303303
None => return self.clone(),
304304
};
305-
let use_tree = make::use_tree(suffix.clone(), self.use_tree_list(), self.alias());
305+
let use_tree =
306+
make::use_tree(suffix.clone(), self.use_tree_list(), self.alias(), self.has_star());
306307
let nested = make::use_tree_list(iter::once(use_tree));
307-
return make::use_tree(prefix.clone(), Some(nested), None);
308+
return make::use_tree(prefix.clone(), Some(nested), None, false);
308309

309310
fn split_path_prefix(prefix: &ast::Path) -> Option<ast::Path> {
310311
let parent = prefix.parent_path()?;

crates/ra_syntax/src/ast/make.rs

+5
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ pub fn use_tree(
2929
path: ast::Path,
3030
use_tree_list: Option<ast::UseTreeList>,
3131
alias: Option<ast::Alias>,
32+
add_star: bool,
3233
) -> ast::UseTree {
3334
let mut buf = "use ".to_string();
3435
buf += &path.syntax().to_string();
3536
if let Some(use_tree_list) = use_tree_list {
3637
buf += &format!("::{}", use_tree_list);
3738
}
39+
if add_star {
40+
buf += "::*";
41+
}
42+
3843
if let Some(alias) = alias {
3944
buf += &format!(" {}", alias);
4045
}

0 commit comments

Comments
 (0)