Skip to content

Commit

Permalink
feature: sort inline tables (#671)
Browse files Browse the repository at this point in the history
* order keys in inline tables

* add tests for sort keys in inline tables

* refactor vec deque instantiation

* use reorder arrays key

* remove unnecessary VecDeque init

* add reorder_inline_tables flag

* add reorderInlineTables to even-better-toml

* add reorder_inline_tables to formatter options config doc

* reinstate reorder arrays test case
  • Loading branch information
adrianEffe committed Sep 10, 2024
1 parent b536710 commit 6694969
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
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 |

0 comments on commit 6694969

Please sign in to comment.