Skip to content

Commit

Permalink
fix: Preserve dotted-key ordering while allowing key order modifications
Browse files Browse the repository at this point in the history
Fixes #163
  • Loading branch information
mxndtaylor committed Nov 10, 2024
1 parent b2378f2 commit 670ff9a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
10 changes: 5 additions & 5 deletions crates/toml_edit/src/inline_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::iter::FromIterator;

use crate::key::Key;
use crate::repr::Decor;
use crate::table::{Iter, IterMut, KeyValuePairs, TableLike, sort_values_by_position};
use crate::table::{Iter, IterMut, KeyValuePairs, TableLike, sort_values_by_position, modify_key_position};
use crate::{InternalString, Item, KeyMut, RawString, Table, Value};

/// Type representing a TOML inline table,
Expand Down Expand Up @@ -87,14 +87,14 @@ impl InlineTable {
pub fn sort_values(&mut self) {
// Assuming standard tables have their position set and this won't negatively impact them
self.items.sort_keys();
for value in self.items.values_mut() {
modify_key_position(&mut self.items,|value| {
match value {
Item::Value(Value::InlineTable(table)) if table.is_dotted() => {
table.sort_values();
}
_ => {}
}
}
});
}

/// Sort Key/Value Pairs of the table using the using the comparison function `compare`.
Expand Down Expand Up @@ -123,14 +123,14 @@ impl InlineTable {
};

self.items.sort_by(modified_cmp);
for value in self.items.values_mut() {
modify_key_position(&mut self.items,|value| {
match value {
Item::Value(Value::InlineTable(table)) if table.is_dotted() => {
table.sort_values_by_internal(compare);
}
_ => {}
}
}
});
}

/// If a table has no key/value pairs and implicit, it will not be displayed.
Expand Down
20 changes: 15 additions & 5 deletions crates/toml_edit/src/table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::iter::FromIterator;

use indexmap::map::IndexMap;
use indexmap::map::{IndexMap, MutableKeys};

use crate::key::Key;
use crate::repr::Decor;
Expand Down Expand Up @@ -110,14 +110,14 @@ impl Table {
pub fn sort_values(&mut self) {
// Assuming standard tables have their doc_position set and this won't negatively impact them
self.items.sort_keys();
for value in self.items.values_mut() {
modify_key_position(&mut self.items,|value| {
match value {
Item::Table(table) if table.is_dotted() => {
table.sort_values();
}
_ => {}
}
}
});
}

/// Sort Key/Value Pairs of the table using the using the comparison function `compare`.
Expand All @@ -142,14 +142,14 @@ impl Table {

self.items.sort_by(modified_cmp);

for value in self.items.values_mut() {
modify_key_position(&mut self.items,|value| {
match value {
Item::Table(table) if table.is_dotted() => {
table.sort_values_by_internal(compare);
}
_ => {}
}
}
});
}

/// If a table has no key/value pairs and implicit, it will not be displayed.
Expand Down Expand Up @@ -531,6 +531,16 @@ pub(crate) fn sort_values_by_position<'s>(values: &mut Vec<(Vec<&'s Key>, &'s Va
});
}

pub(crate) fn modify_key_position<F>(items: &mut KeyValuePairs, mut recursive_step: F)
where
F: FnMut(&mut Item),
{
for (pos, (key, value)) in items.iter_mut2().enumerate() {
key.set_position(Some(pos));
recursive_step(value);
}
}

// `key1 = value1`
pub(crate) const DEFAULT_ROOT_DECOR: (&str, &str) = ("", "");
pub(crate) const DEFAULT_KEY_DECOR: (&str, &str) = ("", " ");
Expand Down
36 changes: 36 additions & 0 deletions crates/toml_edit/tests/testsuite/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,42 @@ fn test_sort_values() {
"#]]);
}

#[test]
fn test_sort_dotted_values() {
given(
r#"
[a.z]
[a]
a.b = 2
# this comment is attached to b
b = 3 # as well as this
a.a = 1
c = 4
[a.y]"#,
)
.running(|root| {
let a = root.get_mut("a").unwrap();
let a = as_table!(a);
a.sort_values();
})
.produces_display(str![[r#"
[a.z]
[a]
a.a = 1
a.b = 2
# this comment is attached to b
b = 3 # as well as this
c = 4
[a.y]
"#]]);
}

#[test]
fn test_sort_values_by() {
given(
Expand Down

0 comments on commit 670ff9a

Please sign in to comment.