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

feature: sort inline tables #671

Merged
merged 9 commits into from
Sep 10, 2024
Merged
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
28 changes: 26 additions & 2 deletions crates/taplo/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use crate::{
syntax::{SyntaxElement, SyntaxKind::*, SyntaxNode, SyntaxToken},
util::overlaps,
};
use itertools::Itertools;
use once_cell::unsync::OnceCell;
use rowan::{GreenNode, NodeOrToken, TextRange};
use std::{
cmp,
collections::VecDeque,
iter::{repeat, FromIterator},
ops::Range,
rc::Rc,
Expand Down Expand Up @@ -109,6 +111,9 @@ create_options!(
/// Alphabetically reorder array values that are not separated by blank lines.
pub reorder_arrays: bool,

/// Alphabetically reorder inline table values.
pub reorder_inline_tables: bool,

/// The maximum amount of consecutive blank lines allowed.
pub allowed_blank_lines: usize,

Expand Down Expand Up @@ -166,6 +171,7 @@ impl Default for Options {
indent_string: " ".into(),
reorder_keys: false,
reorder_arrays: false,
reorder_inline_tables: false,
crlf: false,
}
}
Expand Down Expand Up @@ -866,6 +872,16 @@ fn format_inline_table(
formatted = "{}".into();
}

let mut sorted_children = if options.reorder_inline_tables {
Some(
node.children()
.sorted_unstable_by(|x, y| x.to_string().cmp(&y.to_string()))
.collect::<VecDeque<_>>(),
)
} else {
None
};

let mut node_index = 0;
for c in node.children_with_tokens() {
match c {
Expand All @@ -874,7 +890,16 @@ fn format_inline_table(
formatted += ", ";
}

let entry = format_entry(n, options, context);
let child = if options.reorder_inline_tables {
sorted_children
.as_mut()
.and_then(|children| children.pop_front())
.unwrap_or(n)
} else {
n
};

let entry = format_entry(child, options, context);
debug_assert!(entry.comment.is_none());
entry.write_to(&mut formatted, options);

Expand Down Expand Up @@ -915,7 +940,6 @@ fn format_inline_table(

(node.into(), formatted, comment)
}

// Check whether the array spans multiple lines in its current form.
fn is_array_multiline(node: &SyntaxNode) -> bool {
node.descendants_with_tokens().any(|n| n.kind() == NEWLINE)
Expand Down
28 changes: 28 additions & 0 deletions crates/taplo/src/tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,34 @@ very_long_inline_table = { array = ["aaaaa", "aaaaa", "aaaaa", "aaaaa", "aaaaa",
assert_format!(src, &formatted);
}

#[test]
fn test_sorted_inline_tables() {
let src = r#"
foo = { b = 2, a = 1 }

bar = [
{ a = 1, b = 2, c = 3 },
{ b = 2, a = 1, d = 4, e = 5 },
]
"#;

let expected = r#"
foo = { a = 1, b = 2 }

bar = [{ a = 1, b = 2, c = 3 }, { a = 1, b = 2, d = 4, e = 5 }]
"#;

let formatted = crate::formatter::format(
src,
formatter::Options {
reorder_inline_tables: true,
..Default::default()
},
);

assert_format!(expected, &formatted);
}

#[test]
fn test_sorted_groupings_in_array() {
let src = r#"
Expand Down
6 changes: 6 additions & 0 deletions editors/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@
"default": null,
"description": "Alphabetically reorder array values that are not separated by blank lines."
},
"evenBetterToml.formatter.reorderInlineTables": {
"scope": "resource",
"type": "boolean",
"default": null,
"description": "Alphabetically reorder inline tables."
},
"evenBetterToml.formatter.allowedBlankLines": {
"scope": "resource",
"type": "number",
Expand Down
1 change: 1 addition & 0 deletions site/site/configuration/formatter-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ In some environments (e.g., Visual Studio Code), one needs to reload the extensi
| trailing_newline | Add trailing newline to the source. | true |
| reorder_keys | Alphabetically reorder keys that are not separated by blank lines. | false |
| reorder_arrays | Alphabetically reorder array values that are not separated by blank lines. | false |
| reorder_inline_tables | Alphabetically reorder inline tables. | false |
| allowed_blank_lines | The maximum amount of consecutive blank lines allowed. | 2 |
| crlf | Use CRLF line endings. | false |
Loading