diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-10-13 15:59:37 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-10-13 15:59:37 +0100 |
commit | 5091b5e9739a283329b475cab84a83117eb5814a (patch) | |
tree | f8e3bc969b502075cf1157fe33fc044d204cb022 | |
parent | 2d79bb9413b3e30d15d70b49ca19fcf47a97e592 (diff) | |
parent | 2620b8d42f2d9c41ead1af518158691840141583 (diff) |
Merge #6209
6209: Fix MergeBehaviour::Full/Last not working when merging nested long paths r=matklad a=Veykril
Prior to this inserting `std::foo::bar::Baz` with a nested import containing a multi segment path like `use std::{foo::bar::Qux};` present would result in `use std::{foo::bar::Baz, foo::bar::Qux};` with Full/Last Mergebehaviour.
Co-authored-by: Lukas Wirth <[email protected]>
-rw-r--r-- | crates/assists/src/handlers/replace_qualified_name_with_use.rs | 2 | ||||
-rw-r--r-- | crates/assists/src/utils/insert_use.rs | 35 |
2 files changed, 33 insertions, 4 deletions
diff --git a/crates/assists/src/handlers/replace_qualified_name_with_use.rs b/crates/assists/src/handlers/replace_qualified_name_with_use.rs index 74afc123b..e95b971a4 100644 --- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs | |||
@@ -393,7 +393,7 @@ impl std::fmt::Display<|> for Foo { | |||
393 | } | 393 | } |
394 | ", | 394 | ", |
395 | r" | 395 | r" |
396 | use std::fmt::{Display, nested::Debug}; | 396 | use std::fmt::{nested::Debug, Display}; |
397 | 397 | ||
398 | impl Display for Foo { | 398 | impl Display for Foo { |
399 | } | 399 | } |
diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs index f6025c99a..bfd457d18 100644 --- a/crates/assists/src/utils/insert_use.rs +++ b/crates/assists/src/utils/insert_use.rs | |||
@@ -200,7 +200,18 @@ fn recursive_merge( | |||
200 | return None; | 200 | return None; |
201 | } | 201 | } |
202 | let rhs_path = rhs_t.path(); | 202 | let rhs_path = rhs_t.path(); |
203 | match use_trees.binary_search_by(|p| path_cmp_bin_search(p.path(), rhs_path.clone())) { | 203 | match use_trees.binary_search_by(|lhs_t| { |
204 | let (lhs_t, rhs_t) = match lhs_t | ||
205 | .path() | ||
206 | .zip(rhs_path.clone()) | ||
207 | .and_then(|(lhs, rhs)| common_prefix(&lhs, &rhs)) | ||
208 | { | ||
209 | Some((lhs_p, rhs_p)) => (lhs_t.split_prefix(&lhs_p), rhs_t.split_prefix(&rhs_p)), | ||
210 | None => (lhs_t.clone(), rhs_t.clone()), | ||
211 | }; | ||
212 | |||
213 | path_cmp_bin_search(lhs_t.path(), rhs_t.path()) | ||
214 | }) { | ||
204 | Ok(idx) => { | 215 | Ok(idx) => { |
205 | let lhs_t = &mut use_trees[idx]; | 216 | let lhs_t = &mut use_trees[idx]; |
206 | let lhs_path = lhs_t.path()?; | 217 | let lhs_path = lhs_t.path()?; |
@@ -327,11 +338,11 @@ fn path_cmp_for_sort(a: Option<ast::Path>, b: Option<ast::Path>) -> Ordering { | |||
327 | 338 | ||
328 | /// Path comparison func for binary searching for merging. | 339 | /// Path comparison func for binary searching for merging. |
329 | fn path_cmp_bin_search(lhs: Option<ast::Path>, rhs: Option<ast::Path>) -> Ordering { | 340 | fn path_cmp_bin_search(lhs: Option<ast::Path>, rhs: Option<ast::Path>) -> Ordering { |
330 | match (lhs, rhs) { | 341 | match (lhs.and_then(|path| path.segment()), rhs.and_then(|path| path.segment())) { |
331 | (None, None) => Ordering::Equal, | 342 | (None, None) => Ordering::Equal, |
332 | (None, Some(_)) => Ordering::Less, | 343 | (None, Some(_)) => Ordering::Less, |
333 | (Some(_), None) => Ordering::Greater, | 344 | (Some(_), None) => Ordering::Greater, |
334 | (Some(ref a), Some(ref b)) => path_cmp_short(a, b), | 345 | (Some(ref a), Some(ref b)) => path_segment_cmp(a, b), |
335 | } | 346 | } |
336 | } | 347 | } |
337 | 348 | ||
@@ -802,6 +813,24 @@ use std::foo::bar::{Qux, quux::{Fez, Fizz}};", | |||
802 | } | 813 | } |
803 | 814 | ||
804 | #[test] | 815 | #[test] |
816 | fn merge_groups_full_nested_long() { | ||
817 | check_full( | ||
818 | "std::foo::bar::Baz", | ||
819 | r"use std::{foo::bar::Qux};", | ||
820 | r"use std::{foo::bar::{Baz, Qux}};", | ||
821 | ); | ||
822 | } | ||
823 | |||
824 | #[test] | ||
825 | fn merge_groups_last_nested_long() { | ||
826 | check_full( | ||
827 | "std::foo::bar::Baz", | ||
828 | r"use std::{foo::bar::Qux};", | ||
829 | r"use std::{foo::bar::{Baz, Qux}};", | ||
830 | ); | ||
831 | } | ||
832 | |||
833 | #[test] | ||
805 | fn merge_groups_skip_pub() { | 834 | fn merge_groups_skip_pub() { |
806 | check_full( | 835 | check_full( |
807 | "std::io", | 836 | "std::io", |