Skip to content

Commit

Permalink
Allow rules to target individual entries
Browse files Browse the repository at this point in the history
Fixes #608
  • Loading branch information
rmburg committed Jan 11, 2025
1 parent 18b86da commit 9f62e95
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
12 changes: 8 additions & 4 deletions crates/taplo/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,15 @@ fn format_key(node: SyntaxNode, formatted: &mut String, _options: &Options, _con
fn format_value(node: SyntaxNode, options: &Options, context: &Context) -> impl FormattedItem {
let mut value = String::new();
let mut comment = None;

let mut scoped_options = options.clone();
context.update_options(&mut scoped_options, node.text_range());

for c in node.children_with_tokens() {
match c {
NodeOrToken::Node(n) => match n.kind() {
ARRAY => {
let formatted = format_array(n, options, context);
let formatted = format_array(n, &scoped_options, context);

let c = formatted.trailing_comment();

Expand All @@ -818,10 +822,10 @@ fn format_value(node: SyntaxNode, options: &Options, context: &Context) -> impl
}

debug_assert!(value.is_empty());
formatted.write_to(&mut value, options);
formatted.write_to(&mut value, &scoped_options);
}
INLINE_TABLE => {
let formatted = format_inline_table(n, options, context);
let formatted = format_inline_table(n, &scoped_options, context);

let c = formatted.trailing_comment();

Expand All @@ -832,7 +836,7 @@ fn format_value(node: SyntaxNode, options: &Options, context: &Context) -> impl

debug_assert!(value.is_empty());

formatted.write_to(&mut value, options);
formatted.write_to(&mut value, &scoped_options);
}
_ => unreachable!(),
},
Expand Down
31 changes: 29 additions & 2 deletions crates/taplo/src/tests/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use difference::Changeset;

use crate::formatter;
use crate::formatter::Options;
use crate::formatter::{self, Options, OptionsIncomplete};

macro_rules! assert_format {
($expected:expr, $actual:expr) => {
Expand Down Expand Up @@ -1176,3 +1175,31 @@ a = "b" # comment

assert_format!(expected, &formatted);
}

#[test]
fn test_entry_rule() {
let src = r#"
[foo]
sort_me = ["3", "2", "1"]
sort_me_not = ["3", "2", "1"]
"#;

let expected = r#"
[foo]
sort_me = ["1", "2", "3"]
sort_me_not = ["3", "2", "1"]
"#;

let dom = crate::parser::parse(src).into_dom();
let scopes = [(
"foo.sort_me",
OptionsIncomplete {
reorder_arrays: Some(true),
..Default::default()
},
)];
let formatted =
crate::formatter::format_with_path_scopes(dom, Options::default(), &[], scopes).unwrap();

assert_format!(expected, &formatted);
}

0 comments on commit 9f62e95

Please sign in to comment.