Skip to content

Commit

Permalink
Simplify sorting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Nov 23, 2023
1 parent 8ff7e02 commit a9c6c89
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 74 deletions.
8 changes: 1 addition & 7 deletions src/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ fn get_impl_item_ident(item: &ImplItem) -> Option<syn::Ident> {

impl VisitMut for IdentVisitor {
fn visit_file_mut(&mut self, i: &mut syn::File) {
i.items
.sort_by(|item1, item2| match (get_ident(item1), get_ident(item2)) {
(Some(ident1), Some(ident2)) => ident1.cmp(&ident2),
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
});
i.items.sort_by_cached_key(get_ident);
for it in &mut i.attrs {
self.visit_attribute_mut(it);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/generics/expected_mapping.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"PLACEHOLDER_1": "One",
"PLACEHOLDER_2": "A",
"PLACEHOLDER_3": "Test",
"PLACEHOLDER_4": "B",
"PLACEHOLDER_5": "a",
"PLACEHOLDER_6": "b",
"PLACEHOLDER_7": "superb",
"PLACEHOLDER_8": "var_c"
"PLACEHOLDER_1": "A",
"PLACEHOLDER_2": "B",
"PLACEHOLDER_3": "superb",
"PLACEHOLDER_4": "var_c",
"PLACEHOLDER_5": "One",
"PLACEHOLDER_6": "Test",
"PLACEHOLDER_7": "a",
"PLACEHOLDER_8": "b"
}
16 changes: 8 additions & 8 deletions tests/generics/expected_representation.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
struct PLACEHOLDER_1<PLACEHOLDER_2>(PLACEHOLDER_2);
struct PLACEHOLDER_3<PLACEHOLDER_2, PLACEHOLDER_4> {
PLACEHOLDER_5: PLACEHOLDER_2,
PLACEHOLDER_6: PLACEHOLDER_4,
}
impl<PLACEHOLDER_2, PLACEHOLDER_4> PLACEHOLDER_3<PLACEHOLDER_2, PLACEHOLDER_4> {
fn PLACEHOLDER_7() {
let PLACEHOLDER_8 = 3;
impl<PLACEHOLDER_1, PLACEHOLDER_2> Test<PLACEHOLDER_1, PLACEHOLDER_2> {
fn PLACEHOLDER_3() {
let PLACEHOLDER_4 = 3;
}
}
struct PLACEHOLDER_5<PLACEHOLDER_1>(PLACEHOLDER_1);
struct PLACEHOLDER_6<PLACEHOLDER_1, PLACEHOLDER_2> {
PLACEHOLDER_7: PLACEHOLDER_1,
PLACEHOLDER_8: PLACEHOLDER_2,
}
4 changes: 2 additions & 2 deletions tests/ignore_comments/expected_mapping.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"PLACEHOLDER_1": "A",
"PLACEHOLDER_2": "a",
"PLACEHOLDER_1": "a",
"PLACEHOLDER_2": "A",
"PLACEHOLDER_3": "B",
"PLACEHOLDER_4": "C"
}
20 changes: 10 additions & 10 deletions tests/ignore_comments/expected_representation.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
struct PLACEHOLDER_1 {
PLACEHOLDER_2: i32,
impl C for A {
fn PLACEHOLDER_1(&self) {
let PLACEHOLDER_1 = 10;
}
}
struct PLACEHOLDER_2 {
PLACEHOLDER_1: i32,
}
enum PLACEHOLDER_3 {
PLACEHOLDER_1,
PLACEHOLDER_2,
}
trait PLACEHOLDER_4 {
fn PLACEHOLDER_2(&self);
fn PLACEHOLDER_1(&self);
}
fn main() {
let PLACEHOLDER_2 = 10;
}
impl PLACEHOLDER_4 for PLACEHOLDER_1 {
fn PLACEHOLDER_2(&self) {
let PLACEHOLDER_2 = 10;
}
let PLACEHOLDER_1 = 10;
}
8 changes: 4 additions & 4 deletions tests/impl_blocks/expected_mapping.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"PLACEHOLDER_1": "Example",
"PLACEHOLDER_1": "answer",
"PLACEHOLDER_2": "number",
"PLACEHOLDER_3": "answer",
"PLACEHOLDER_4": "boo",
"PLACEHOLDER_5": "get_number"
"PLACEHOLDER_3": "boo",
"PLACEHOLDER_4": "get_number",
"PLACEHOLDER_5": "Example"
}
14 changes: 7 additions & 7 deletions tests/impl_blocks/expected_representation.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
struct PLACEHOLDER_1 {
PLACEHOLDER_2: i32,
}
impl PLACEHOLDER_1 {
fn PLACEHOLDER_3(&mut self) {
impl Example {
fn PLACEHOLDER_1(&mut self) {
self.PLACEHOLDER_2 += 42;
}
fn PLACEHOLDER_4() {
fn PLACEHOLDER_3() {
println!("boo! Example::boo() was called!");
}
fn PLACEHOLDER_5(&self) -> i32 {
fn PLACEHOLDER_4(&self) -> i32 {
self.PLACEHOLDER_2
}
}
struct PLACEHOLDER_5 {
PLACEHOLDER_2: i32,
}
3 changes: 2 additions & 1 deletion tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ macro_rules! test_cases {
let expected = include_str!(concat!(stringify!($name), "/expected_representation.txt"));

let mut input: syn::File = syn::parse_str(input)?;
let _ = replace(&mut input);
let mapping = replace(&mut input);

if OVERWRITE_EXPECTED_OUTPUT {
std::fs::write(concat!("tests/", stringify!($name), "/expected_representation.txt"), prettyplease::unparse(&input))?;
std::fs::write(concat!("tests/", stringify!($name), "/expected_mapping.json"), serde_json::to_string_pretty(&mapping).unwrap())?;
} else {
assert_eq!(prettyplease::unparse(&input), expected);
}
Expand Down
22 changes: 11 additions & 11 deletions tests/sort_by_ascii_file_items/expected_mapping.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"PLACEHOLDER_1": "Test",
"PLACEHOLDER_10": "superb",
"PLACEHOLDER_11": "var_c",
"PLACEHOLDER_2": "a",
"PLACEHOLDER_3": "var_a",
"PLACEHOLDER_4": "b",
"PLACEHOLDER_5": "var_b",
"PLACEHOLDER_6": "amazing",
"PLACEHOLDER_7": "var_d",
"PLACEHOLDER_8": "beautiful",
"PLACEHOLDER_9": "var_e"
"PLACEHOLDER_1": "amazing",
"PLACEHOLDER_10": "b",
"PLACEHOLDER_11": "var_b",
"PLACEHOLDER_2": "var_d",
"PLACEHOLDER_3": "beautiful",
"PLACEHOLDER_4": "var_e",
"PLACEHOLDER_5": "superb",
"PLACEHOLDER_6": "var_c",
"PLACEHOLDER_7": "Test",
"PLACEHOLDER_8": "a",
"PLACEHOLDER_9": "var_a"
}
28 changes: 14 additions & 14 deletions tests/sort_by_ascii_file_items/expected_representation.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
struct PLACEHOLDER_1(u32);
fn PLACEHOLDER_2() {
let PLACEHOLDER_3 = 1;
}
fn PLACEHOLDER_4() {
let PLACEHOLDER_5 = 2;
}
impl PLACEHOLDER_1 {
fn PLACEHOLDER_6() {
let PLACEHOLDER_7 = 1;
impl Test {
fn PLACEHOLDER_1() {
let PLACEHOLDER_2 = 1;
}
fn PLACEHOLDER_8() {
let PLACEHOLDER_9 = 2;
fn PLACEHOLDER_3() {
let PLACEHOLDER_4 = 2;
}
fn PLACEHOLDER_10() {
let PLACEHOLDER_11 = 3;
fn PLACEHOLDER_5() {
let PLACEHOLDER_6 = 3;
}
}
struct PLACEHOLDER_7(u32);
fn PLACEHOLDER_8() {
let PLACEHOLDER_9 = 1;
}
fn PLACEHOLDER_10() {
let PLACEHOLDER_11 = 2;
}
4 changes: 2 additions & 2 deletions tests/user_defined_traits/expected_representation.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
impl MyTrait for Vec {}
impl PartialEq for MyType {}
trait PLACEHOLDER_1 {}
type PLACEHOLDER_2 = u8;
impl PLACEHOLDER_1 for Vec {}
impl PartialEq for PLACEHOLDER_2 {}

0 comments on commit a9c6c89

Please sign in to comment.