Auto merge of #16352 - davidsemakula:rustfmt-import-sort-algo, r=Veykril

internal: Follow rustfmt's algorithm for ordering imports when ordering and merging use trees

Updates use tree ordering and merging utilities to follow rustfmt's algorithm for ordering imports.
The [rustfmt implementation](6356fca675/src/imports.rs) was used as reference.
This commit is contained in:
bors 2024-01-16 11:23:03 +00:00
commit 0a8c7841e0
7 changed files with 264 additions and 119 deletions

View file

@ -986,7 +986,7 @@ fn foo() {
} }
//- /main.rs //- /main.rs
use foo::{Foo, Bool}; use foo::{Bool, Foo};
mod foo; mod foo;
@ -1662,7 +1662,7 @@ impl Foo {
} }
//- /foo.rs //- /foo.rs
use crate::{Foo, Bool}; use crate::{Bool, Foo};
fn foo() -> bool { fn foo() -> bool {
Foo::BOOL == Bool::True Foo::BOOL == Bool::True

View file

@ -179,7 +179,7 @@ use std::fmt::Debug;
use std::fmt$0::Display; use std::fmt$0::Display;
", ",
r" r"
use std::fmt::{Display, Debug}; use std::fmt::{Debug, Display};
", ",
); );
} }
@ -206,7 +206,7 @@ use std::fmt::{self, Display};
use std::{fmt, $0fmt::Display}; use std::{fmt, $0fmt::Display};
", ",
r" r"
use std::{fmt::{Display, self}}; use std::{fmt::{self, Display}};
", ",
); );
} }
@ -318,7 +318,7 @@ use std::{fmt::{Debug, Display}};
use std::{fmt::Debug, fmt$0::Display}; use std::{fmt::Debug, fmt$0::Display};
", ",
r" r"
use std::{fmt::{Display, Debug}}; use std::{fmt::{Debug, Display}};
", ",
); );
} }
@ -332,7 +332,7 @@ use std$0::{fmt::{Write, Display}};
use std::{fmt::{self, Debug}}; use std::{fmt::{self, Debug}};
", ",
r" r"
use std::{fmt::{Write, Display, self, Debug}}; use std::{fmt::{self, Debug, Display, Write}};
", ",
); );
} }
@ -346,7 +346,7 @@ use std$0::{fmt::{self, Debug}};
use std::{fmt::{Write, Display}}; use std::{fmt::{Write, Display}};
", ",
r" r"
use std::{fmt::{self, Debug, Write, Display}}; use std::{fmt::{self, Debug, Display, Write}};
", ",
); );
} }
@ -359,7 +359,7 @@ use std::{fmt::{self, Debug, Write, Display}};
use std::{fmt$0::{self, Debug}, fmt::{Write, Display}}; use std::{fmt$0::{self, Debug}, fmt::{Write, Display}};
", ",
r" r"
use std::{fmt::{self, Debug, Write, Display}}; use std::{fmt::{self, Debug, Display, Write}};
", ",
); );
} }
@ -401,7 +401,7 @@ use std$0::{fmt::*};
use std::{fmt::{self, Display}}; use std::{fmt::{self, Display}};
", ",
r" r"
use std::{fmt::{*, self, Display}}; use std::{fmt::{self, Display, *}};
", ",
) )
} }
@ -496,7 +496,7 @@ use foo::$0{
", ",
r" r"
use foo::{ use foo::{
FooBar, bar::baz, bar::baz, FooBar
}; };
", ",
) )
@ -521,7 +521,7 @@ use foo::$0*;
use foo::bar::Baz; use foo::bar::Baz;
", ",
r" r"
use foo::{*, bar::Baz}; use foo::{bar::Baz, *};
", ",
); );
} }
@ -539,7 +539,7 @@ $0use std::fmt::Result;
", ",
r" r"
use std::fmt::Error; use std::fmt::Error;
use std::fmt::{Display, Debug, Write}; use std::fmt::{Debug, Display, Write};
use std::fmt::Result; use std::fmt::Result;
", ",
); );
@ -560,7 +560,7 @@ use std::{
r" r"
use std::{ use std::{
fmt::Error, fmt::Error,
fmt::{Display, Debug, Write}, fmt::{Debug, Display, Write},
fmt::Result, fmt::Result,
};", };",
); );
@ -568,7 +568,7 @@ use std::{
check_assist( check_assist(
merge_imports, merge_imports,
r"use std::$0{fmt::Display, fmt::Debug}$0;", r"use std::$0{fmt::Display, fmt::Debug}$0;",
r"use std::{fmt::{Display, Debug}};", r"use std::{fmt::{Debug, Display}};",
); );
} }
} }

View file

@ -313,7 +313,7 @@ fn main() {
", ",
r" r"
mod std { pub mod fmt { pub trait Display {} } } mod std { pub mod fmt { pub trait Display {} } }
use std::fmt::{Display, self}; use std::fmt::{self, Display};
fn main() { fn main() {
fmt; fmt;

View file

@ -16,7 +16,7 @@ use syntax::{
use crate::{ use crate::{
imports::merge_imports::{ imports::merge_imports::{
common_prefix, eq_attrs, eq_visibility, try_merge_imports, use_tree_path_cmp, MergeBehavior, common_prefix, eq_attrs, eq_visibility, try_merge_imports, use_tree_cmp, MergeBehavior,
}, },
RootDatabase, RootDatabase,
}; };
@ -357,6 +357,8 @@ fn insert_use_(
use_item: ast::Use, use_item: ast::Use,
) { ) {
let scope_syntax = scope.as_syntax_node(); let scope_syntax = scope.as_syntax_node();
let insert_use_tree =
use_item.use_tree().expect("`use_item` should have a use tree for `insert_path`");
let group = ImportGroup::new(insert_path); let group = ImportGroup::new(insert_path);
let path_node_iter = scope_syntax let path_node_iter = scope_syntax
.children() .children()
@ -364,8 +366,7 @@ fn insert_use_(
.flat_map(|(use_, node)| { .flat_map(|(use_, node)| {
let tree = use_.use_tree()?; let tree = use_.use_tree()?;
let path = tree.path()?; let path = tree.path()?;
let has_tl = tree.use_tree_list().is_some(); Some((path, tree, node))
Some((path, has_tl, node))
}); });
if group_imports { if group_imports {
@ -381,9 +382,7 @@ fn insert_use_(
// find the element that would come directly after our new import // find the element that would come directly after our new import
let post_insert: Option<(_, _, SyntaxNode)> = group_iter let post_insert: Option<(_, _, SyntaxNode)> = group_iter
.inspect(|(.., node)| last = Some(node.clone())) .inspect(|(.., node)| last = Some(node.clone()))
.find(|&(ref path, has_tl, _)| { .find(|(_, use_tree, _)| use_tree_cmp(&insert_use_tree, use_tree) != Ordering::Greater);
use_tree_path_cmp(insert_path, false, path, has_tl) != Ordering::Greater
});
if let Some((.., node)) = post_insert { if let Some((.., node)) = post_insert {
cov_mark::hit!(insert_group); cov_mark::hit!(insert_group);

View file

@ -596,7 +596,7 @@ fn merge_groups_full() {
#[test] #[test]
fn merge_groups_long_full() { fn merge_groups_long_full() {
check_crate("std::foo::bar::Baz", r"use std::foo::bar::Qux;", r"use std::foo::bar::{Qux, Baz};") check_crate("std::foo::bar::Baz", r"use std::foo::bar::Qux;", r"use std::foo::bar::{Baz, Qux};")
} }
#[test] #[test]
@ -604,7 +604,7 @@ fn merge_groups_long_last() {
check_module( check_module(
"std::foo::bar::Baz", "std::foo::bar::Baz",
r"use std::foo::bar::Qux;", r"use std::foo::bar::Qux;",
r"use std::foo::bar::{Qux, Baz};", r"use std::foo::bar::{Baz, Qux};",
) )
} }
@ -613,7 +613,7 @@ fn merge_groups_long_full_list() {
check_crate( check_crate(
"std::foo::bar::Baz", "std::foo::bar::Baz",
r"use std::foo::bar::{Qux, Quux};", r"use std::foo::bar::{Qux, Quux};",
r"use std::foo::bar::{Qux, Quux, Baz};", r"use std::foo::bar::{Baz, Quux, Qux};",
) )
} }
@ -622,7 +622,7 @@ fn merge_groups_long_last_list() {
check_module( check_module(
"std::foo::bar::Baz", "std::foo::bar::Baz",
r"use std::foo::bar::{Qux, Quux};", r"use std::foo::bar::{Qux, Quux};",
r"use std::foo::bar::{Qux, Quux, Baz};", r"use std::foo::bar::{Baz, Quux, Qux};",
) )
} }
@ -631,7 +631,7 @@ fn merge_groups_long_full_nested() {
check_crate( check_crate(
"std::foo::bar::Baz", "std::foo::bar::Baz",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};", r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}, Baz};", r"use std::foo::bar::{quux::{Fez, Fizz}, Baz, Qux};",
) )
} }
@ -650,7 +650,7 @@ fn merge_groups_full_nested_deep() {
check_crate( check_crate(
"std::foo::bar::quux::Baz", "std::foo::bar::quux::Baz",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};", r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
r"use std::foo::bar::{Qux, quux::{Fez, Fizz, Baz}};", r"use std::foo::bar::{Qux, quux::{Baz, Fez, Fizz}};",
) )
} }
@ -659,7 +659,7 @@ fn merge_groups_full_nested_long() {
check_crate( check_crate(
"std::foo::bar::Baz", "std::foo::bar::Baz",
r"use std::{foo::bar::Qux};", r"use std::{foo::bar::Qux};",
r"use std::{foo::bar::{Qux, Baz}};", r"use std::{foo::bar::{Baz, Qux}};",
); );
} }
@ -668,7 +668,7 @@ fn merge_groups_last_nested_long() {
check_crate( check_crate(
"std::foo::bar::Baz", "std::foo::bar::Baz",
r"use std::{foo::bar::Qux};", r"use std::{foo::bar::Qux};",
r"use std::{foo::bar::{Qux, Baz}};", r"use std::{foo::bar::{Baz, Qux}};",
); );
} }
@ -733,7 +733,7 @@ fn merge_mod_into_glob() {
check_with_config( check_with_config(
"token::TokenKind", "token::TokenKind",
r"use token::TokenKind::*;", r"use token::TokenKind::*;",
r"use token::TokenKind::{*, self};", r"use token::TokenKind::{self, *};",
&InsertUseConfig { &InsertUseConfig {
granularity: ImportGranularity::Crate, granularity: ImportGranularity::Crate,
enforce_granularity: true, enforce_granularity: true,
@ -742,7 +742,6 @@ fn merge_mod_into_glob() {
skip_glob_imports: false, skip_glob_imports: false,
}, },
) )
// FIXME: have it emit `use token::TokenKind::{self, *}`?
} }
#[test] #[test]
@ -750,7 +749,7 @@ fn merge_self_glob() {
check_with_config( check_with_config(
"self", "self",
r"use self::*;", r"use self::*;",
r"use self::{*, self};", r"use self::{self, *};",
&InsertUseConfig { &InsertUseConfig {
granularity: ImportGranularity::Crate, granularity: ImportGranularity::Crate,
enforce_granularity: true, enforce_granularity: true,
@ -759,7 +758,6 @@ fn merge_self_glob() {
skip_glob_imports: false, skip_glob_imports: false,
}, },
) )
// FIXME: have it emit `use {self, *}`?
} }
#[test] #[test]
@ -769,7 +767,7 @@ fn merge_glob() {
r" r"
use syntax::{SyntaxKind::*};", use syntax::{SyntaxKind::*};",
r" r"
use syntax::{SyntaxKind::{*, self}};", use syntax::{SyntaxKind::{self, *}};",
) )
} }
@ -778,7 +776,7 @@ fn merge_glob_nested() {
check_crate( check_crate(
"foo::bar::quux::Fez", "foo::bar::quux::Fez",
r"use foo::bar::{Baz, quux::*};", r"use foo::bar::{Baz, quux::*};",
r"use foo::bar::{Baz, quux::{*, Fez}};", r"use foo::bar::{Baz, quux::{Fez, *}};",
) )
} }
@ -787,7 +785,7 @@ fn merge_nested_considers_first_segments() {
check_crate( check_crate(
"hir_ty::display::write_bounds_like_dyn_trait", "hir_ty::display::write_bounds_like_dyn_trait",
r"use hir_ty::{autoderef, display::{HirDisplayError, HirFormatter}, method_resolution};", r"use hir_ty::{autoderef, display::{HirDisplayError, HirFormatter}, method_resolution};",
r"use hir_ty::{autoderef, display::{HirDisplayError, HirFormatter, write_bounds_like_dyn_trait}, method_resolution};", r"use hir_ty::{autoderef, display::{write_bounds_like_dyn_trait, HirDisplayError, HirFormatter}, method_resolution};",
); );
} }

View file

@ -1,10 +1,15 @@
//! Handle syntactic aspects of merging UseTrees. //! Handle syntactic aspects of merging UseTrees.
use std::cmp::Ordering; use std::cmp::Ordering;
use std::iter::empty;
use itertools::{EitherOrBoth, Itertools}; use itertools::{EitherOrBoth, Itertools};
use parser::T;
use stdx::is_upper_snake_case;
use syntax::{ use syntax::{
ast::{self, AstNode, HasAttrs, HasVisibility, PathSegmentKind}, algo,
ted, ast::{self, make, AstNode, HasAttrs, HasName, HasVisibility, PathSegmentKind},
ted::{self, Position},
Direction, TokenText,
}; };
use crate::syntax_helpers::node_ext::vis_eq; use crate::syntax_helpers::node_ext::vis_eq;
@ -97,20 +102,19 @@ fn recursive_merge(lhs: &ast::UseTree, rhs: &ast::UseTree, merge: MergeBehavior)
// same as a `filter` op). // same as a `filter` op).
.map(|tree| merge.is_tree_allowed(&tree).then_some(tree)) .map(|tree| merge.is_tree_allowed(&tree).then_some(tree))
.collect::<Option<_>>()?; .collect::<Option<_>>()?;
use_trees.sort_unstable_by(|a, b| path_cmp_for_sort(a.path(), b.path())); // Sorts the use trees similar to rustfmt's algorithm for ordering imports
// (see `use_tree_cmp` doc).
use_trees.sort_unstable_by(|a, b| use_tree_cmp(a, b));
for rhs_t in rhs.use_tree_list().into_iter().flat_map(|list| list.use_trees()) { for rhs_t in rhs.use_tree_list().into_iter().flat_map(|list| list.use_trees()) {
if !merge.is_tree_allowed(&rhs_t) { if !merge.is_tree_allowed(&rhs_t) {
return None; return None;
} }
let rhs_path = rhs_t.path();
match use_trees match use_trees.binary_search_by(|lhs_t| use_tree_cmp_bin_search(lhs_t, &rhs_t)) {
.binary_search_by(|lhs_t| path_cmp_bin_search(lhs_t.path(), rhs_path.as_ref()))
{
Ok(idx) => { Ok(idx) => {
let lhs_t = &mut use_trees[idx]; let lhs_t = &mut use_trees[idx];
let lhs_path = lhs_t.path()?; let lhs_path = lhs_t.path()?;
let rhs_path = rhs_path?; let rhs_path = rhs_t.path()?;
let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?; let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?;
if lhs_prefix == lhs_path && rhs_prefix == rhs_path { if lhs_prefix == lhs_path && rhs_prefix == rhs_path {
let tree_is_self = |tree: &ast::UseTree| { let tree_is_self = |tree: &ast::UseTree| {
@ -159,9 +163,61 @@ fn recursive_merge(lhs: &ast::UseTree, rhs: &ast::UseTree, merge: MergeBehavior)
{ {
return None return None
} }
Err(idx) => { Err(insert_idx) => {
use_trees.insert(idx, rhs_t.clone()); use_trees.insert(insert_idx, rhs_t.clone());
lhs.get_or_create_use_tree_list().add_use_tree(rhs_t); match lhs.use_tree_list() {
// Creates a new use tree list with the item.
None => lhs.get_or_create_use_tree_list().add_use_tree(rhs_t),
// Recreates the use tree list with sorted items (see `use_tree_cmp` doc).
Some(use_tree_list) => {
if use_tree_list.l_curly_token().is_none() {
ted::insert_raw(
Position::first_child_of(use_tree_list.syntax()),
make::token(T!['{']),
);
}
if use_tree_list.r_curly_token().is_none() {
ted::insert_raw(
Position::last_child_of(use_tree_list.syntax()),
make::token(T!['}']),
);
}
let mut elements = Vec::new();
for (idx, tree) in use_trees.iter().enumerate() {
if idx > 0 {
elements.push(make::token(T![,]).into());
elements.push(make::tokens::single_space().into());
}
elements.push(tree.syntax().clone().into());
}
let start = use_tree_list
.l_curly_token()
.and_then(|l_curly| {
algo::non_trivia_sibling(l_curly.into(), Direction::Next)
})
.filter(|it| it.kind() != T!['}']);
let end = use_tree_list
.r_curly_token()
.and_then(|r_curly| {
algo::non_trivia_sibling(r_curly.into(), Direction::Prev)
})
.filter(|it| it.kind() != T!['{']);
if let Some((start, end)) = start.zip(end) {
// Attempt to insert elements while preserving preceding and trailing trivia.
ted::replace_all(start..=end, elements);
} else {
let new_use_tree_list = make::use_tree_list(empty()).clone_for_update();
let trees_pos = match new_use_tree_list.l_curly_token() {
Some(l_curly) => Position::after(l_curly),
None => Position::last_child_of(new_use_tree_list.syntax()),
};
ted::insert_all_raw(trees_pos, elements);
ted::replace(use_tree_list.syntax(), new_use_tree_list.syntax());
}
}
}
} }
} }
} }
@ -190,89 +246,177 @@ pub fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast
} }
} }
/// Orders paths in the following way: /// Use tree comparison func for binary searching for merging.
/// the sole self token comes first, after that come uppercase identifiers, then lowercase identifiers fn use_tree_cmp_bin_search(lhs: &ast::UseTree, rhs: &ast::UseTree) -> Ordering {
// FIXME: rustfmt sorts lowercase idents before uppercase, in general we want to have the same ordering rustfmt has let lhs_is_simple_path = lhs.is_simple_path() && lhs.rename().is_none();
// which is `self` and `super` first, then identifier imports with lowercase ones first, then glob imports and at last list imports. let rhs_is_simple_path = rhs.is_simple_path() && rhs.rename().is_none();
// Example foo::{self, foo, baz, Baz, Qux, *, {Bar}} match (
fn path_cmp_for_sort(a: Option<ast::Path>, b: Option<ast::Path>) -> Ordering { lhs.path().as_ref().and_then(ast::Path::first_segment),
match (a, b) { rhs.path().as_ref().and_then(ast::Path::first_segment),
(None, None) => Ordering::Equal, ) {
(None, Some(_)) => Ordering::Less, (None, None) => match (lhs_is_simple_path, rhs_is_simple_path) {
(Some(_), None) => Ordering::Greater,
(Some(ref a), Some(ref b)) => match (path_is_self(a), path_is_self(b)) {
(true, true) => Ordering::Equal, (true, true) => Ordering::Equal,
(true, false) => Ordering::Less, (true, false) => Ordering::Less,
(false, true) => Ordering::Greater, (false, true) => Ordering::Greater,
(false, false) => path_cmp_short(a, b), (false, false) => use_tree_cmp_by_tree_list_glob_or_alias(lhs, rhs, false),
}, },
} (Some(_), None) if !rhs_is_simple_path => Ordering::Less,
}
/// Path comparison func for binary searching for merging.
fn path_cmp_bin_search(lhs: Option<ast::Path>, rhs: Option<&ast::Path>) -> Ordering {
match (lhs.as_ref().and_then(ast::Path::first_segment), rhs.and_then(ast::Path::first_segment))
{
(None, None) => Ordering::Equal,
(None, Some(_)) => Ordering::Less,
(Some(_), None) => Ordering::Greater, (Some(_), None) => Ordering::Greater,
(Some(ref a), Some(ref b)) => path_segment_cmp(a, b), (None, Some(_)) if !lhs_is_simple_path => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
(Some(a), Some(b)) => path_segment_cmp(&a, &b),
} }
} }
/// Short circuiting comparison, if both paths are equal until one of them ends they are considered /// Orders use trees following `rustfmt`'s algorithm for ordering imports, which is `self`, `super`
/// equal /// and `crate` first, then identifier imports with lowercase ones first and upper snake case
fn path_cmp_short(a: &ast::Path, b: &ast::Path) -> Ordering { /// (e.g. UPPER_SNAKE_CASE) ones last, then glob imports, and at last list imports.
let a = a.segments(); ///
let b = b.segments(); /// Example foo::{self, foo, baz, Baz, Qux, FOO_BAZ, *, {Bar}}
// cmp_by would be useful for us here but that is currently unstable /// Ref: <https://github.com/rust-lang/rustfmt/blob/6356fca675bd756d71f5c123cd053d17b16c573e/src/imports.rs#L83-L86>.
// cmp doesn't work due the lifetimes on text's return type pub(super) fn use_tree_cmp(a: &ast::UseTree, b: &ast::UseTree) -> Ordering {
a.zip(b) let a_is_simple_path = a.is_simple_path() && a.rename().is_none();
.find_map(|(a, b)| match path_segment_cmp(&a, &b) { let b_is_simple_path = b.is_simple_path() && b.rename().is_none();
Ordering::Equal => None, match (a.path(), b.path()) {
ord => Some(ord), (None, None) => match (a_is_simple_path, b_is_simple_path) {
}) (true, true) => Ordering::Equal,
.unwrap_or(Ordering::Equal) (true, false) => Ordering::Less,
} (false, true) => Ordering::Greater,
(false, false) => use_tree_cmp_by_tree_list_glob_or_alias(a, b, true),
/// Compares two paths, if one ends earlier than the other the has_tl parameters decide which is },
/// greater as a path that has a tree list should be greater, while one that just ends without (Some(_), None) if !b_is_simple_path => Ordering::Less,
/// a tree list should be considered less. (Some(_), None) => Ordering::Greater,
pub(super) fn use_tree_path_cmp( (None, Some(_)) if !a_is_simple_path => Ordering::Greater,
a: &ast::Path, (None, Some(_)) => Ordering::Less,
a_has_tl: bool, (Some(a_path), Some(b_path)) => {
b: &ast::Path, // cmp_by would be useful for us here but that is currently unstable
b_has_tl: bool, // cmp doesn't work due the lifetimes on text's return type
) -> Ordering { a_path
let a_segments = a.segments(); .segments()
let b_segments = b.segments(); .zip_longest(b_path.segments())
// cmp_by would be useful for us here but that is currently unstable .find_map(|zipped| match zipped {
// cmp doesn't work due the lifetimes on text's return type EitherOrBoth::Both(a_segment, b_segment) => {
a_segments match path_segment_cmp(&a_segment, &b_segment) {
.zip_longest(b_segments) Ordering::Equal => None,
.find_map(|zipped| match zipped { ord => Some(ord),
EitherOrBoth::Both(ref a, ref b) => match path_segment_cmp(a, b) { }
Ordering::Equal => None, }
ord => Some(ord), EitherOrBoth::Left(_) if b_is_simple_path => Some(Ordering::Greater),
}, EitherOrBoth::Left(_) => Some(Ordering::Less),
EitherOrBoth::Left(_) if !b_has_tl => Some(Ordering::Greater), EitherOrBoth::Right(_) if a_is_simple_path => Some(Ordering::Less),
EitherOrBoth::Left(_) => Some(Ordering::Less), EitherOrBoth::Right(_) => Some(Ordering::Greater),
EitherOrBoth::Right(_) if !a_has_tl => Some(Ordering::Less), })
EitherOrBoth::Right(_) => Some(Ordering::Greater), .unwrap_or_else(|| use_tree_cmp_by_tree_list_glob_or_alias(a, b, true))
}) }
.unwrap_or(Ordering::Equal) }
} }
fn path_segment_cmp(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering { fn path_segment_cmp(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering {
let a = a.kind().and_then(|kind| match kind { match (a.kind(), b.kind()) {
PathSegmentKind::Name(name_ref) => Some(name_ref), (None, None) => Ordering::Equal,
_ => None, (Some(_), None) => Ordering::Greater,
}); (None, Some(_)) => Ordering::Less,
let b = b.kind().and_then(|kind| match kind { // self
PathSegmentKind::Name(name_ref) => Some(name_ref), (Some(PathSegmentKind::SelfKw), Some(PathSegmentKind::SelfKw)) => Ordering::Equal,
_ => None, (Some(PathSegmentKind::SelfKw), _) => Ordering::Less,
}); (_, Some(PathSegmentKind::SelfKw)) => Ordering::Greater,
a.as_ref().map(ast::NameRef::text).cmp(&b.as_ref().map(ast::NameRef::text)) // super
(Some(PathSegmentKind::SuperKw), Some(PathSegmentKind::SuperKw)) => Ordering::Equal,
(Some(PathSegmentKind::SuperKw), _) => Ordering::Less,
(_, Some(PathSegmentKind::SuperKw)) => Ordering::Greater,
// crate
(Some(PathSegmentKind::CrateKw), Some(PathSegmentKind::CrateKw)) => Ordering::Equal,
(Some(PathSegmentKind::CrateKw), _) => Ordering::Less,
(_, Some(PathSegmentKind::CrateKw)) => Ordering::Greater,
// identifiers (everything else is treated as an identifier).
_ => {
match (
a.name_ref().as_ref().map(ast::NameRef::text),
b.name_ref().as_ref().map(ast::NameRef::text),
) {
(None, None) => Ordering::Equal,
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
(Some(a_name), Some(b_name)) => {
// snake_case < CamelCase < UPPER_SNAKE_CASE
let a_text = a_name.as_str();
let b_text = b_name.as_str();
if a_text.starts_with(char::is_lowercase)
&& b_text.starts_with(char::is_uppercase)
{
return Ordering::Less;
}
if a_text.starts_with(char::is_uppercase)
&& b_text.starts_with(char::is_lowercase)
{
return Ordering::Greater;
}
if !is_upper_snake_case(a_text) && is_upper_snake_case(b_text) {
return Ordering::Less;
}
if is_upper_snake_case(a_text) && !is_upper_snake_case(b_text) {
return Ordering::Greater;
}
a_text.cmp(&b_text)
}
}
}
}
}
/// Orders for use trees with equal paths (see `use_tree_cmp` for details about use tree ordering).
///
/// If the `strict` parameter is set to true and both trees have tree lists, the tree lists are
/// ordered by calling `use_tree_cmp` on their "sub-tree" pairs until either the tie is broken
/// or tree list equality is confirmed, otherwise (i.e. if either `strict` is false or at least
/// one of the trees does *not* have tree list), this potentially recursive step is skipped,
/// and only the presence of a glob pattern or an alias is used to determine the ordering.
fn use_tree_cmp_by_tree_list_glob_or_alias(
a: &ast::UseTree,
b: &ast::UseTree,
strict: bool,
) -> Ordering {
let cmp_by_glob_or_alias = || match (a.star_token().is_some(), b.star_token().is_some()) {
(true, false) => Ordering::Greater,
(false, true) => Ordering::Less,
_ => match (a.rename(), b.rename()) {
(None, None) => Ordering::Equal,
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
(Some(a_rename), Some(b_rename)) => a_rename
.name()
.as_ref()
.map(ast::Name::text)
.as_ref()
.map_or("_", TokenText::as_str)
.cmp(
b_rename
.name()
.as_ref()
.map(ast::Name::text)
.as_ref()
.map_or("_", TokenText::as_str),
),
},
};
match (a.use_tree_list(), b.use_tree_list()) {
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
(Some(a_list), Some(b_list)) if strict => a_list
.use_trees()
.zip_longest(b_list.use_trees())
.find_map(|zipped| match zipped {
EitherOrBoth::Both(a_tree, b_tree) => match use_tree_cmp(&a_tree, &b_tree) {
Ordering::Equal => None,
ord => Some(ord),
},
EitherOrBoth::Left(_) => Some(Ordering::Greater),
EitherOrBoth::Right(_) => Some(Ordering::Less),
})
.unwrap_or_else(cmp_by_glob_or_alias),
_ => cmp_by_glob_or_alias(),
}
} }
pub fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool { pub fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -> bool {

View file

@ -171,6 +171,10 @@ pub fn char_has_case(c: char) -> bool {
c.is_lowercase() || c.is_uppercase() c.is_lowercase() || c.is_uppercase()
} }
pub fn is_upper_snake_case(s: &str) -> bool {
s.chars().all(|c| c.is_uppercase() || c == '_' || c.is_numeric())
}
pub fn replace(buf: &mut String, from: char, to: &str) { pub fn replace(buf: &mut String, from: char, to: &str) {
if !buf.contains(from) { if !buf.contains(from) {
return; return;