diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-27 18:55:05 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-27 18:55:05 +0000 |
commit | a1fea0d34ee8f3436aefd87d4c133a7ff50ffbb0 (patch) | |
tree | 9afc05a20e5e51f757317a71906dc45a97861415 | |
parent | 75f6ab208a2b3e5fea9e8b6362df3fad7308a0d5 (diff) | |
parent | f016d8b900792c8ae4fce268920edea3035b6559 (diff) |
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]>
-rw-r--r-- | crates/ra_assists/src/handlers/merge_imports.rs | 28 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 5 | ||||
-rw-r--r-- | 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 | |||
@@ -171,6 +171,34 @@ use std::{fmt::<|>{Display, Debug}}; | |||
171 | } | 171 | } |
172 | 172 | ||
173 | #[test] | 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 | |||
201 | #[test] | ||
174 | fn removes_just_enough_whitespace() { | 202 | fn removes_just_enough_whitespace() { |
175 | check_assist( | 203 | check_assist( |
176 | merge_imports, | 204 | merge_imports, |
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 { | |||
302 | Some(it) => it, | 302 | Some(it) => it, |
303 | None => return self.clone(), | 303 | None => return self.clone(), |
304 | }; | 304 | }; |
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()); | ||
306 | let nested = make::use_tree_list(iter::once(use_tree)); | 307 | 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); |
308 | 309 | ||
309 | fn split_path_prefix(prefix: &ast::Path) -> Option<ast::Path> { | 310 | fn split_path_prefix(prefix: &ast::Path) -> Option<ast::Path> { |
310 | let parent = prefix.parent_path()?; | 311 | 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( | |||
29 | path: ast::Path, | 29 | path: ast::Path, |
30 | use_tree_list: Option<ast::UseTreeList>, | 30 | use_tree_list: Option<ast::UseTreeList>, |
31 | alias: Option<ast::Alias>, | 31 | alias: Option<ast::Alias>, |
32 | add_star: bool, | ||
32 | ) -> ast::UseTree { | 33 | ) -> ast::UseTree { |
33 | let mut buf = "use ".to_string(); | 34 | let mut buf = "use ".to_string(); |
34 | buf += &path.syntax().to_string(); | 35 | buf += &path.syntax().to_string(); |
35 | if let Some(use_tree_list) = use_tree_list { | 36 | if let Some(use_tree_list) = use_tree_list { |
36 | buf += &format!("::{}", use_tree_list); | 37 | buf += &format!("::{}", use_tree_list); |
37 | } | 38 | } |
39 | if add_star { | ||
40 | buf += "::*"; | ||
41 | } | ||
42 | |||
38 | if let Some(alias) = alias { | 43 | if let Some(alias) = alias { |
39 | buf += &format!(" {}", alias); | 44 | buf += &format!(" {}", alias); |
40 | } | 45 | } |