Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow rules to target individual entries #732

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading