Skip to content

Commit 5091b5e

Browse files
bors[bot]Veykril
andauthored
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]>
2 parents 2d79bb9 + 2620b8d commit 5091b5e

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

crates/assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl std::fmt::Display<|> for Foo {
393393
}
394394
",
395395
r"
396-
use std::fmt::{Display, nested::Debug};
396+
use std::fmt::{nested::Debug, Display};
397397
398398
impl Display for Foo {
399399
}

crates/assists/src/utils/insert_use.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,18 @@ fn recursive_merge(
200200
return None;
201201
}
202202
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+
}) {
204215
Ok(idx) => {
205216
let lhs_t = &mut use_trees[idx];
206217
let lhs_path = lhs_t.path()?;
@@ -327,11 +338,11 @@ fn path_cmp_for_sort(a: Option<ast::Path>, b: Option<ast::Path>) -> Ordering {
327338

328339
/// Path comparison func for binary searching for merging.
329340
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())) {
331342
(None, None) => Ordering::Equal,
332343
(None, Some(_)) => Ordering::Less,
333344
(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),
335346
}
336347
}
337348

@@ -801,6 +812,24 @@ use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
801812
)
802813
}
803814

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+
804833
#[test]
805834
fn merge_groups_skip_pub() {
806835
check_full(

0 commit comments

Comments
 (0)