Skip to content

Commit

Permalink
feat: apply 2024 version sort algorithm to mods
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Oct 18, 2024
1 parent a2625bf commit 2d049af
Show file tree
Hide file tree
Showing 93 changed files with 387 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,40 @@ use std::cmp::Ordering;
use rustc_ast::{ast, attr};
use rustc_span::{Span, symbol::sym};

use crate::StyleEdition;
use crate::config::{Config, GroupImportsTactic};
use crate::imports::{UseSegmentKind, UseTree, normalize_use_trees_with_granularity};
use crate::items::{is_mod_decl, rewrite_extern_crate, rewrite_mod};
use crate::lists::{ListFormatting, ListItem, itemize_list, write_list};
use crate::rewrite::{RewriteContext, RewriteErrorExt};
use crate::shape::Shape;
use crate::sort::version_sort;
use crate::source_map::LineRangeUtils;
use crate::spanned::Spanned;
use crate::utils::{contains_skip, mk_sp};
use crate::visitor::FmtVisitor;

/// Choose the ordering between the given two items.
fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
fn compare_items(a: &ast::Item, b: &ast::Item, context: &RewriteContext<'_>) -> Ordering {
let style_edition = context.config.style_edition();
match (&a.kind, &b.kind) {
(&ast::ItemKind::Mod(..), &ast::ItemKind::Mod(..)) => {
a.ident.as_str().cmp(b.ident.as_str())
if style_edition <= StyleEdition::Edition2021 {
a.ident.as_str().cmp(b.ident.as_str())
} else {
version_sort(a.ident.as_str(), b.ident.as_str())
}
}
(&ast::ItemKind::ExternCrate(ref a_name), &ast::ItemKind::ExternCrate(ref b_name)) => {
// `extern crate foo as bar;`
// ^^^ Comparing this.
let a_orig_name = a_name.unwrap_or(a.ident.name);
let b_orig_name = b_name.unwrap_or(b.ident.name);
let result = a_orig_name.as_str().cmp(b_orig_name.as_str());
let result = if style_edition <= StyleEdition::Edition2021 {
a_orig_name.as_str().cmp(b_orig_name.as_str())
} else {
version_sort(a_orig_name.as_str(), b_orig_name.as_str())
};
if result != Ordering::Equal {
return result;
}
Expand All @@ -44,7 +55,10 @@ fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
(Some(..), None) => Ordering::Greater,
(None, Some(..)) => Ordering::Less,
(None, None) => Ordering::Equal,
(Some(..), Some(..)) => a.ident.as_str().cmp(b.ident.as_str()),
(Some(..), Some(..)) if style_edition <= StyleEdition::Edition2021 => {
a.ident.as_str().cmp(b.ident.as_str())
}
(Some(..), Some(..)) => version_sort(a.ident.as_str(), b.ident.as_str()),
}
}
_ => unreachable!(),
Expand Down Expand Up @@ -165,7 +179,7 @@ fn rewrite_reorderable_or_regroupable_items(
);

let mut item_pair_vec: Vec<_> = list_items.zip(reorderable_items.iter()).collect();
item_pair_vec.sort_by(|a, b| compare_items(a.1, b.1));
item_pair_vec.sort_by(|a, b| compare_items(a.1, b.1, context));
let item_vec: Vec<_> = item_pair_vec.into_iter().map(|pair| pair.0).collect();

wrap_reorderable_items(context, &item_vec, shape)
Expand Down
1 change: 1 addition & 0 deletions tests/source/reorder_modules/A2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/ABCD/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/ZYXW/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/ZYXW_/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/ZY_XW/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/Z_YXW/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/_ZYXW/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/_abcd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/a1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/abcd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

47 changes: 47 additions & 0 deletions tests/source/reorder_modules/disabled_style_edition_2024.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// rustfmt-style_edition: 2024
// rustfmt-reorder_modules: false

mod x86;
mod v0s;
mod v001;
mod x87;
mod zyxw;
mod A2;
mod ZYXW;
mod w5s009t;
mod u8;
mod x86_128;
mod _ZYXW;
mod ZY_XW;
mod a1;
mod v01;
mod v0u;
mod x86_64;
mod ua;
mod x86_32;
mod v9;
mod v010;
mod u_zzz;
mod v0;
mod v00;
mod v009;
mod w005s09t;
mod u32;
mod v1;
mod v00t;
mod v09;
mod u256;
mod ZY_XW;
mod _abcd;
mod ABCD;
mod Z_YXW;
mod u64;
mod abcd;
mod ZYXW_;
mod u16;
mod uz;
mod v10;
mod x64;
mod u128;
mod usize;
mod v000;
47 changes: 47 additions & 0 deletions tests/source/reorder_modules/enabled_style_edition_2015.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// rustfmt-style_edition: 2015
// rustfmt-reorder_modules: true

mod x86;
mod v0s;
mod v001;
mod x87;
mod zyxw;
mod A2;
mod ZYXW;
mod w5s009t;
mod u8;
mod x86_128;
mod _ZYXW;
mod ZY_XW;
mod a1;
mod v01;
mod v0u;
mod x86_64;
mod ua;
mod x86_32;
mod v9;
mod v010;
mod u_zzz;
mod v0;
mod v00;
mod v009;
mod w005s09t;
mod u32;
mod v1;
mod v00t;
mod v09;
mod u256;
mod ZY_XW;
mod _abcd;
mod ABCD;
mod Z_YXW;
mod u64;
mod abcd;
mod ZYXW_;
mod u16;
mod uz;
mod v10;
mod x64;
mod u128;
mod usize;
mod v000;
47 changes: 47 additions & 0 deletions tests/source/reorder_modules/enabled_style_edition_2024.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// rustfmt-style_edition: 2024
// rustfmt-reorder_modules: true

mod x86;
mod v0s;
mod v001;
mod x87;
mod zyxw;
mod A2;
mod ZYXW;
mod w5s009t;
mod u8;
mod x86_128;
mod _ZYXW;
mod ZY_XW;
mod a1;
mod v01;
mod v0u;
mod x86_64;
mod ua;
mod x86_32;
mod v9;
mod v010;
mod u_zzz;
mod v0;
mod v00;
mod v009;
mod w005s09t;
mod u32;
mod v1;
mod v00t;
mod v09;
mod u256;
mod ZY_XW;
mod _abcd;
mod ABCD;
mod Z_YXW;
mod u64;
mod abcd;
mod ZYXW_;
mod u16;
mod uz;
mod v10;
mod x64;
mod u128;
mod usize;
mod v000;
1 change: 1 addition & 0 deletions tests/source/reorder_modules/u128/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/u16/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/u256/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/u32/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/u64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/u8/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/u_zzz/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/ua/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/usize/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/uz/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v00/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v000/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v001/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v009/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v00t/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v01/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v010/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v09/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v0s/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v0u/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v10/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/v9/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/w005s09t/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/w5s009t/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/x64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/x86/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/x86_128/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/x86_32/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/x86_64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/x87/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/source/reorder_modules/zyxw/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/A2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/ABCD/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/ZYXW/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/ZYXW_/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/ZY_XW/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/Z_YXW/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/_ZYXW/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/_abcd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/a1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/target/reorder_modules/abcd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

47 changes: 47 additions & 0 deletions tests/target/reorder_modules/disabled_style_edition_2024.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// rustfmt-style_edition: 2024
// rustfmt-reorder_modules: false

mod x86;
mod v0s;
mod v001;
mod x87;
mod zyxw;
mod A2;
mod ZYXW;
mod w5s009t;
mod u8;
mod x86_128;
mod _ZYXW;
mod ZY_XW;
mod a1;
mod v01;
mod v0u;
mod x86_64;
mod ua;
mod x86_32;
mod v9;
mod v010;
mod u_zzz;
mod v0;
mod v00;
mod v009;
mod w005s09t;
mod u32;
mod v1;
mod v00t;
mod v09;
mod u256;
mod ZY_XW;
mod _abcd;
mod ABCD;
mod Z_YXW;
mod u64;
mod abcd;
mod ZYXW_;
mod u16;
mod uz;
mod v10;
mod x64;
mod u128;
mod usize;
mod v000;
Loading

0 comments on commit 2d049af

Please sign in to comment.