Repository: toml-rs/toml
Pull request #1011 (2025-07-18 19:17:43)

fix(edit): Don't lose whitespace in empty arrays
Fixes #1007


Patch:
diff --git a/crates/toml/src/de/parser/array.rs b/crates/toml/src/de/parser/array.rs
index 00dd6f43..0fd9b729 100644
--- a/crates/toml/src/de/parser/array.rs
+++ b/crates/toml/src/de/parser/array.rs
@@ -25,6 +25,7 @@ pub(crate) fn on_array<'i>(
     let mut close_span = open_event.span();
 
     let mut state = State::default();
+    state.open(open_event);
     while let Some(event) = input.next_token() {
         close_span = event.span();
         match event.kind() {
@@ -90,6 +91,8 @@ struct State<'i> {
 }
 
 impl<'i> State<'i> {
+    fn open(&mut self, _open_event: &toml_parser::parser::Event) {}
+
     fn whitespace(&mut self, _event: &toml_parser::parser::Event) {}
 
     fn capture_value(&mut self, _event: &toml_parser::parser::Event, value: Spanned<DeValue<'i>>) {
diff --git a/crates/toml_edit/src/parser/array.rs b/crates/toml_edit/src/parser/array.rs
index b904c226..157b4b72 100644
--- a/crates/toml_edit/src/parser/array.rs
+++ b/crates/toml_edit/src/parser/array.rs
@@ -22,6 +22,7 @@ pub(crate) fn on_array(
     let mut result = Array::new();
 
     let mut state = State::default();
+    state.open(open_event);
     while let Some(event) = input.next_token() {
         match event.kind() {
             EventKind::StdTableOpen
@@ -86,6 +87,10 @@ struct State {
 }
 
 impl State {
+    fn open(&mut self, open_event: &toml_parser::parser::Event) {
+        self.trailing_start = Some(open_event.span().end());
+    }
+
     fn whitespace(&mut self, event: &toml_parser::parser::Event) {
         let decor = if self.is_prefix() {
             self.current_prefix.get_or_insert(event.span())


Test patch:
diff --git a/crates/toml_edit/tests/testsuite/edit.rs b/crates/toml_edit/tests/testsuite/edit.rs
index 15a90d3b..43b5bf31 100644
--- a/crates/toml_edit/tests/testsuite/edit.rs
+++ b/crates/toml_edit/tests/testsuite/edit.rs
@@ -65,6 +65,14 @@ fn assign_whitespace() {
  [ grandparent . parent ]  # table comment
  # table-value sep comment
  key . child = 'value' # key comment
+ key . inline-empty = { }  # empty inline-table comment
+ key . inline = { inline . child = 'inline-value' }  # inline-table comment
+ key . array-empty = [  # inside empty array comment
+ ] # after empty array comment
+ key . array = [ # start of array comment
+   'one' , # one comment
+   'two' , # two comment
+] # after array comment
  # table-table sep comment
  [ another . table ]  # table comment
  # final comment
@@ -183,6 +191,196 @@ DocumentMut {
                                                             },
                                                         ),
                                                     ),
+                                                    Key {
+                                                        key: "inline-empty",
+                                                        repr: Some(
+                                                            "inline-empty",
+                                                        ),
+                                                        leaf_decor: Decor {
+                                                            prefix: " ",
+                                                            suffix: " ",
+                                                        },
+                                                        dotted_decor: Decor {
+                                                            prefix: " ",
+                                                            suffix: empty,
+                                                        },
+                                                    }: Value(
+                                                        InlineTable(
+                                                            InlineTable {
+                                                                preamble: " ",
+                                                                implicit: false,
+                                                                decor: Decor {
+                                                                    prefix: " ",
+                                                                    suffix: "  # empty inline-table comment",
+                                                                },
+                                                                span: None,
+                                                                dotted: false,
+                                                                items: {},
+                                                            },
+                                                        ),
+                                                    ),
+                                                    Key {
+                                                        key: "inline",
+                                                        repr: Some(
+                                                            "inline",
+                                                        ),
+                                                        leaf_decor: Decor {
+                                                            prefix: " ",
+                                                            suffix: " ",
+                                                        },
+                                                        dotted_decor: Decor {
+                                                            prefix: " ",
+                                                            suffix: empty,
+                                                        },
+                                                    }: Value(
+                                                        InlineTable(
+                                                            InlineTable {
+                                                                preamble: empty,
+                                                                implicit: false,
+                                                                decor: Decor {
+                                                                    prefix: " ",
+                                                                    suffix: "  # inline-table comment",
+                                                                },
+                                                                span: None,
+                                                                dotted: false,
+                                                                items: {
+                                                                    Key {
+                                                                        key: "inline",
+                                                                        repr: Some(
+                                                                            "inline",
+                                                                        ),
+                                                                        leaf_decor: Decor {
+                                                                            prefix: "default",
+                                                                            suffix: "default",
+                                                                        },
+                                                                        dotted_decor: Decor {
+                                                                            prefix: empty,
+                                                                            suffix: " ",
+                                                                        },
+                                                                    }: Value(
+                                                                        InlineTable(
+                                                                            InlineTable {
+                                                                                preamble: empty,
+                                                                                implicit: true,
+                                                                                decor: Decor {
+                                                                                    prefix: "default",
+                                                                                    suffix: "default",
+                                                                                },
+                                                                                span: None,
+                                                                                dotted: true,
+                                                                                items: {
+                                                                                    Key {
+                                                                                        key: "child",
+                                                                                        repr: Some(
+                                                                                            "child",
+                                                                                        ),
+                                                                                        leaf_decor: Decor {
+                                                                                            prefix: " ",
+                                                                                            suffix: " ",
+                                                                                        },
+                                                                                        dotted_decor: Decor {
+                                                                                            prefix: " ",
+                                                                                            suffix: empty,
+                                                                                        },
+                                                                                    }: Value(
+                                                                                        String(
+                                                                                            Formatted {
+                                                                                                value: "inline-value",
+                                                                                                repr: "'inline-value'",
+                                                                                                decor: Decor {
+                                                                                                    prefix: " ",
+                                                                                                    suffix: " ",
+                                                                                                },
+                                                                                            },
+                                                                                        ),
+                                                                                    ),
+                                                                                },
+                                                                            },
+                                                                        ),
+                                                                    ),
+                                                                },
+                                                            },
+                                                        ),
+                                                    ),
+                                                    Key {
+                                                        key: "array-empty",
+                                                        repr: Some(
+                                                            "array-empty",
+                                                        ),
+                                                        leaf_decor: Decor {
+                                                            prefix: " ",
+                                                            suffix: " ",
+                                                        },
+                                                        dotted_decor: Decor {
+                                                            prefix: " ",
+                                                            suffix: empty,
+                                                        },
+                                                    }: Value(
+                                                        Array(
+                                                            Array {
+                                                                trailing: "  # inside empty array comment\n ",
+                                                                trailing_comma: false,
+                                                                decor: Decor {
+                                                                    prefix: " ",
+                                                                    suffix: " # after empty array comment",
+                                                                },
+                                                                span: None,
+                                                                values: [],
+                                                            },
+                                                        ),
+                                                    ),
+                                                    Key {
+                                                        key: "array",
+                                                        repr: Some(
+                                                            "array",
+                                                        ),
+                                                        leaf_decor: Decor {
+                                                            prefix: " ",
+                                                            suffix: " ",
+                                                        },
+                                                        dotted_decor: Decor {
+                                                            prefix: " ",
+                                                            suffix: empty,
+                                                        },
+                                                    }: Value(
+                                                        Array(
+                                                            Array {
+                                                                trailing: " # two comment\n",
+                                                                trailing_comma: true,
+                                                                decor: Decor {
+                                                                    prefix: " ",
+                                                                    suffix: " # after array comment",
+                                                                },
+                                                                span: None,
+                                                                values: [
+                                                                    Value(
+                                                                        String(
+                                                                            Formatted {
+                                                                                value: "one",
+                                                                                repr: "'one'",
+                                                                                decor: Decor {
+                                                                                    prefix: " # start of array comment\n   ",
+                                                                                    suffix: " ",
+                                                                                },
+                                                                            },
+                                                                        ),
+                                                                    ),
+                                                                    Value(
+                                                                        String(
+                                                                            Formatted {
+                                                                                value: "two",
+                                                                                repr: "'two'",
+                                                                                decor: Decor {
+                                                                                    prefix: " # one comment\n   ",
+                                                                                    suffix: " ",
+                                                                                },
+                                                                            },
+                                                                        ),
+                                                                    ),
+                                                                ],
+                                                            },
+                                                        ),
+                                                    ),
                                                 },
                                             },
                                         ),