Skip to content

Commit db3f0f1

Browse files
committed
improve use tree simple path comparison logic
1 parent 22ae5f4 commit db3f0f1

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

crates/ide-db/src/imports/merge_imports.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,21 @@ pub fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast
264264

265265
/// Use tree comparison func for binary searching for merging.
266266
fn use_tree_cmp_bin_search(lhs: &ast::UseTree, rhs: &ast::UseTree) -> Ordering {
267+
let lhs_is_simple_path = lhs.is_simple_path() && lhs.rename().is_none();
268+
let rhs_is_simple_path = rhs.is_simple_path() && rhs.rename().is_none();
267269
match (
268270
lhs.path().as_ref().and_then(ast::Path::first_segment),
269271
rhs.path().as_ref().and_then(ast::Path::first_segment),
270272
) {
271-
(None, None) => match (lhs.is_simple_path(), lhs.is_simple_path()) {
273+
(None, None) => match (lhs_is_simple_path, rhs_is_simple_path) {
272274
(true, true) => Ordering::Equal,
273275
(true, false) => Ordering::Less,
274276
(false, true) => Ordering::Greater,
275277
(false, false) => use_tree_cmp_by_tree_list_glob_or_alias(lhs, rhs, false),
276278
},
277-
(Some(_), None) if !rhs.is_simple_path() => Ordering::Less,
279+
(Some(_), None) if !rhs_is_simple_path => Ordering::Less,
278280
(Some(_), None) => Ordering::Greater,
279-
(None, Some(_)) if !lhs.is_simple_path() => Ordering::Greater,
281+
(None, Some(_)) if !lhs_is_simple_path => Ordering::Greater,
280282
(None, Some(_)) => Ordering::Less,
281283
(Some(ref a), Some(ref b)) => path_segment_cmp(a, b),
282284
}
@@ -289,16 +291,18 @@ fn use_tree_cmp_bin_search(lhs: &ast::UseTree, rhs: &ast::UseTree) -> Ordering {
289291
/// Example foo::{self, foo, baz, Baz, Qux, FOO_BAZ, *, {Bar}}
290292
/// Ref: <https://github.com/rust-lang/rustfmt/blob/6356fca675bd756d71f5c123cd053d17b16c573e/src/imports.rs#L83-L86>.
291293
pub(super) fn use_tree_cmp(a: &ast::UseTree, b: &ast::UseTree) -> Ordering {
294+
let a_is_simple_path = a.is_simple_path() && a.rename().is_none();
295+
let b_is_simple_path = b.is_simple_path() && b.rename().is_none();
292296
match (a.path(), b.path()) {
293-
(None, None) => match (a.is_simple_path(), b.is_simple_path()) {
297+
(None, None) => match (a_is_simple_path, b_is_simple_path) {
294298
(true, true) => Ordering::Equal,
295299
(true, false) => Ordering::Less,
296300
(false, true) => Ordering::Greater,
297301
(false, false) => use_tree_cmp_by_tree_list_glob_or_alias(a, b, true),
298302
},
299-
(Some(_), None) if !b.is_simple_path() => Ordering::Less,
303+
(Some(_), None) if !b_is_simple_path => Ordering::Less,
300304
(Some(_), None) => Ordering::Greater,
301-
(None, Some(_)) if !a.is_simple_path() => Ordering::Greater,
305+
(None, Some(_)) if !a_is_simple_path => Ordering::Greater,
302306
(None, Some(_)) => Ordering::Less,
303307
(Some(ref a_path), Some(ref b_path)) => {
304308
// cmp_by would be useful for us here but that is currently unstable
@@ -313,9 +317,9 @@ pub(super) fn use_tree_cmp(a: &ast::UseTree, b: &ast::UseTree) -> Ordering {
313317
ord => Some(ord),
314318
}
315319
}
316-
EitherOrBoth::Left(_) if b.is_simple_path() => Some(Ordering::Greater),
320+
EitherOrBoth::Left(_) if b_is_simple_path => Some(Ordering::Greater),
317321
EitherOrBoth::Left(_) => Some(Ordering::Less),
318-
EitherOrBoth::Right(_) if a.is_simple_path() => Some(Ordering::Less),
322+
EitherOrBoth::Right(_) if a_is_simple_path => Some(Ordering::Less),
319323
EitherOrBoth::Right(_) => Some(Ordering::Greater),
320324
})
321325
.unwrap_or_else(|| use_tree_cmp_by_tree_list_glob_or_alias(a, b, true))

0 commit comments

Comments
 (0)