From 2620b8d42f2d9c41ead1af518158691840141583 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 12 Oct 2020 23:34:32 +0200 Subject: Fix MergeBehaviour::Full not working when merging nested long paths --- .../handlers/replace_qualified_name_with_use.rs | 2 +- crates/assists/src/utils/insert_use.rs | 35 ++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) (limited to 'crates') 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 { } ", r" -use std::fmt::{Display, nested::Debug}; +use std::fmt::{nested::Debug, Display}; impl Display for Foo { } 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( return None; } let rhs_path = rhs_t.path(); - match use_trees.binary_search_by(|p| path_cmp_bin_search(p.path(), rhs_path.clone())) { + match use_trees.binary_search_by(|lhs_t| { + let (lhs_t, rhs_t) = match lhs_t + .path() + .zip(rhs_path.clone()) + .and_then(|(lhs, rhs)| common_prefix(&lhs, &rhs)) + { + Some((lhs_p, rhs_p)) => (lhs_t.split_prefix(&lhs_p), rhs_t.split_prefix(&rhs_p)), + None => (lhs_t.clone(), rhs_t.clone()), + }; + + path_cmp_bin_search(lhs_t.path(), rhs_t.path()) + }) { Ok(idx) => { let lhs_t = &mut use_trees[idx]; let lhs_path = lhs_t.path()?; @@ -327,11 +338,11 @@ fn path_cmp_for_sort(a: Option, b: Option) -> Ordering { /// Path comparison func for binary searching for merging. fn path_cmp_bin_search(lhs: Option, rhs: Option) -> Ordering { - match (lhs, rhs) { + match (lhs.and_then(|path| path.segment()), rhs.and_then(|path| path.segment())) { (None, None) => Ordering::Equal, (None, Some(_)) => Ordering::Less, (Some(_), None) => Ordering::Greater, - (Some(ref a), Some(ref b)) => path_cmp_short(a, b), + (Some(ref a), Some(ref b)) => path_segment_cmp(a, b), } } @@ -801,6 +812,24 @@ use std::foo::bar::{Qux, quux::{Fez, Fizz}};", ) } + #[test] + fn merge_groups_full_nested_long() { + check_full( + "std::foo::bar::Baz", + r"use std::{foo::bar::Qux};", + r"use std::{foo::bar::{Baz, Qux}};", + ); + } + + #[test] + fn merge_groups_last_nested_long() { + check_full( + "std::foo::bar::Baz", + r"use std::{foo::bar::Qux};", + r"use std::{foo::bar::{Baz, Qux}};", + ); + } + #[test] fn merge_groups_skip_pub() { check_full( -- cgit v1.2.3