Skip to content

Commit

Permalink
Upgrade to toml_edit 0.21
Browse files Browse the repository at this point in the history
… and fix comments getting stripped.
  • Loading branch information
jplatte committed Dec 7, 2023
1 parent 16f5b79 commit 0f29033
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 67 deletions.
80 changes: 33 additions & 47 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ default-run = "cargo-sort"
clap = { version = "4.0.10", features = ["wrap_help", "cargo"] }
glob = "0.3"
termcolor = "1.1"
toml_edit = "0.9"
toml_edit = "0.21"

# toml_edit's dependencies
chrono = "0.4"
Expand Down
31 changes: 21 additions & 10 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use toml_edit::{Document, Item, Table, Value};
use toml_edit::{Document, Item, RawString, Table, Value};

/// The config file for formatting toml after sorting.
///
Expand Down Expand Up @@ -139,7 +139,11 @@ fn fmt_value(value: &mut Value, config: &Config) {
// Since the above variants have fmt methods we can only ever
// get here from a headed table (`[header] key = val`)
val => {
if config.space_around_eq && val.decor().prefix().map_or(true, str::is_empty)
if config.space_around_eq
&& val
.decor()
.prefix()
.map_or(true, |s| s.as_str().map_or(true, str::is_empty))
{
val.decor_mut().set_prefix(" ");
}
Expand All @@ -156,13 +160,14 @@ fn fmt_table(table: &mut Table, config: &Config) {
let blank_header_lines = table
.decor()
.prefix()
.and_then(RawString::as_str)
.unwrap_or("")
.lines()
.filter(|l| !l.starts_with('#'))
.count();
if config.allowed_blank_lines < blank_header_lines {
let dec = table.decor_mut();
dec.set_prefix(dec.prefix().unwrap_or("").replacen(
dec.set_prefix(dec.prefix().and_then(RawString::as_str).unwrap_or("").replacen(
NEWLINE_PATTERN,
"",
blank_header_lines - config.allowed_blank_lines,
Expand All @@ -172,30 +177,36 @@ fn fmt_table(table: &mut Table, config: &Config) {
let keys: Vec<_> = table.iter().map(|(k, _)| k.to_owned()).collect();
for key in keys {
let dec = table.key_decor_mut(&key).unwrap();
let blank_lines =
dec.prefix().unwrap_or("").lines().filter(|l| !l.starts_with('#')).count();
let prefix = dec.prefix().and_then(RawString::as_str).unwrap_or("");
let blank_lines = prefix.lines().filter(|l| !l.starts_with('#')).count();

// Check each item in the table for blank lines
if config.key_value_newlines {
if config.allowed_blank_lines < blank_lines {
dec.set_prefix(dec.prefix().unwrap_or("").replacen(
dec.set_prefix(prefix.replacen(
NEWLINE_PATTERN,
"",
blank_lines - config.allowed_blank_lines,
));
}
} else {
dec.set_prefix(if dec.prefix().is_some_and(|pre| pre.contains('#')) {
dec.prefix().unwrap_or("").replacen(NEWLINE_PATTERN, "", blank_lines)
dec.set_prefix(if prefix.contains('#') {
prefix.replacen(NEWLINE_PATTERN, "", blank_lines)
} else {
"".to_string()
});
}

// This is weirdly broken, inserts underscores into `[foo.bar]` table
// headers. Revisit later.
/* if config.space_around_eq && dec.suffix().map_or(true, str::is_empty) {
dec.set_suffix(format!("{}{}", dec.suffix().unwrap_or(""), ' '));
/* if config.space_around_eq
&& dec.suffix().and_then(RawString::as_str).map_or(true, str::is_empty)
{
dec.set_suffix(format!(
"{}{}",
dec.suffix().and_then(RawString::as_str).unwrap_or(""),
' '
));
} */

match table.get_mut(&key).unwrap() {
Expand Down
24 changes: 15 additions & 9 deletions src/sort.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cmp::Ordering, collections::BTreeMap, iter::FromIterator};

use toml_edit::{Array, Document, Item, Table, Value};
use toml_edit::{Array, Document, Item, Key, RawString, Table, Value};

/// Each `Matcher` field when matched to a heading or key token
/// will be matched with `.contains()`.
Expand Down Expand Up @@ -56,7 +56,7 @@ pub fn sort_toml(
let mut first_table = None;
let mut heading_order: BTreeMap<_, Vec<Heading>> = BTreeMap::new();
for (idx, (head, item)) in toml.as_table_mut().iter_mut().enumerate() {
if !matcher.heading.contains(&head) {
if !matcher.heading.contains(&head.get()) {
if !ordering.contains(&head.to_owned()) && !ordering.is_empty() {
ordering.push(head.to_owned());
}
Expand Down Expand Up @@ -161,15 +161,21 @@ fn gather_headings(table: &Table, keys: &mut Vec<Heading>, depth: usize) {
}

fn sort_by_group(table: &mut Table) {
let table_clone = table.clone();
let mut table_clone = table.clone();
table.clear();
let mut groups = BTreeMap::new();
let mut curr = 0;
for (idx, (k, v)) in table_clone.iter().enumerate() {
let decor = table.key_decor(k);
let blank_lines = decor.map_or(0, |d| {
d.prefix().unwrap_or("").lines().filter(|l| !l.starts_with('#')).count()
});
for (idx, (k, v)) in table_clone.iter_mut().enumerate() {
let blank_lines = k
.decor()
.prefix()
.and_then(RawString::as_str)
.unwrap_or("")
.lines()
.filter(|l| !l.starts_with('#'))
.count();

let k = Key::new(&*k).with_decor(k.decor().clone());

if blank_lines > 0 {
groups.entry(idx).or_insert_with(|| vec![(k, v)]);
Expand All @@ -182,7 +188,7 @@ fn sort_by_group(table: &mut Table) {
for (_, mut group) in groups {
group.sort_by(|a, b| a.0.cmp(&b.0));
for (k, v) in group {
table.insert(&k, v.clone());
table.insert_formatted(&k, v.clone());
}
}
}
Expand Down

0 comments on commit 0f29033

Please sign in to comment.