Skip to content

Commit

Permalink
implement order strategy "alphabetical"
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Mar 16, 2024
1 parent 339666d commit a7836d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions malva/src/doc_gen/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,12 @@ fn format_statements<'s>(
..
}))
) {
use crate::config::DeclarationOrder;
use crate::{config::DeclarationOrder, helpers::sort_decl};
match declaration_order {
DeclarationOrder::Alphabetical => {}
DeclarationOrder::Alphabetical => {
sortable_decls
.sort_by(|(a, _), (b, _)| sort_decl::compare_in_alphabetical(a, b));
}
DeclarationOrder::Smacss => {}
DeclarationOrder::Concentric => {}
}
Expand Down
1 change: 1 addition & 0 deletions malva/src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod sort_decl;
19 changes: 19 additions & 0 deletions malva/src/helpers/sort_decl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::cmp::Ordering;

pub fn compare_in_alphabetical(a: &str, b: &str) -> Ordering {
strip_vendor_prefix(a).cmp(strip_vendor_prefix(b))
}

pub fn strip_vendor_prefix(s: &str) -> &str {
s.strip_prefix('-')
.and_then(|s| {
let trimmed = s.trim_start_matches(|c: char| c.is_ascii_alphanumeric());
if s == trimmed {
None
} else {
Some(trimmed)
}
})
.and_then(|s| s.strip_prefix('-'))
.unwrap_or(s)
}
1 change: 1 addition & 0 deletions malva/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod config;
mod ctx;
mod doc_gen;
mod error;
mod helpers;
mod line_bounds;
mod state;

Expand Down

0 comments on commit a7836d1

Please sign in to comment.