From 1bc1f5c7e110159cbb2ada5d180372b970f662ca Mon Sep 17 00:00:00 2001 From: rainbowatcher Date: Sun, 10 Aug 2025 13:03:43 +0800 Subject: [PATCH 1/2] fix: edit table value using value mut object instead of insert --- src/ops/set.rs | 16 +++++++++++----- tests/array_edit.test.ts | 2 +- tests/edit.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/ops/set.rs b/src/ops/set.rs index b3525a3..95b7d7d 100644 --- a/src/ops/set.rs +++ b/src/ops/set.rs @@ -52,17 +52,23 @@ pub fn set_value( } } +// insert will overwrite the decoration of the original key +// When the table is empty, only write the default decoration +// When the table has values, we need to read the existing decoration and apply it to the newly written value +// When the key to be written exists, only the value should be modified without changing the key's decoration fn insert_tablelike<'a>(table: &mut (dyn TableLike + 'a), key: &str, value: Item) { if table.is_empty() { table.insert(key, value); - } else if table.contains_key(key) { - let pre_value = table.get(key).unwrap(); + } else if let Some((mut pre_key, pre_value)) = table.get_key_value_mut(key) { let (prefix, suffix) = get_item_decor(pre_value); if let Item::Value(value) = value { - // insert function will auto foramt the key in table and inlinetable - table.insert(key, Item::Value(value.decorated(prefix, suffix))); + *pre_value = Item::Value(value.decorated(prefix, suffix)); + } else if value.is_table() && !pre_value.is_table() { + // remove space before equal sign + pre_key.leaf_decor_mut().set_suffix(""); + *pre_value = value; } else { - table.insert(key, value); + *pre_value = value; }; } else { if let Item::Value(value) = value { diff --git a/tests/array_edit.test.ts b/tests/array_edit.test.ts index f9f613a..70c2264 100644 --- a/tests/array_edit.test.ts +++ b/tests/array_edit.test.ts @@ -72,7 +72,7 @@ describe("array edit", () => { expect(edit(aot, "foo.bar.[0].age", 20, opt)).toBe(dedent` [foo] bar = [ - { name = "tom", age = 20 } + { name = "tom",age = 20 } ] `) }) diff --git a/tests/edit.test.ts b/tests/edit.test.ts index c959003..3481aac 100644 --- a/tests/edit.test.ts +++ b/tests/edit.test.ts @@ -151,6 +151,29 @@ describe("edit", () => { a = 1 b = 2 `) + + const input1 = dedent` + [foo] + # comment + bar = 1 + ` + expect(edit(input1, "foo.bar", 2, opt)).toBe(dedent` + [foo] + # comment + bar = 2 + `) + expect(edit(input1, "foo.bar", { baz: 3 }, opt)).toBe(dedent` + [foo] + # comment + bar = { baz = 3 } + `) + // TODO: This is a known bug: https://github.com/toml-rs/toml/issues/691 + // expect(edit(input1, "foo.bar", { baz: 3 }, {...opt, inline: false})).toMatchInlineSnapshot(` + // "[foo] + // # comment + // [foo.bar] + // baz = 3" + // `) }) it("set datetime", () => { @@ -310,4 +333,17 @@ describe("issue", () => { " `) }) + + it("issue#8", () => { + const toml = dedent` + [package] + # comment + rand = "1" + ` + expect(edit(toml, "package.rand", "2", opt)).toBe(dedent` + [package] + # comment + rand = "2" + `) + }) }) From 98a383cb2dd3f9148fc6ab4cf6b0e89bf8c921df Mon Sep 17 00:00:00 2001 From: rainbowatcher Date: Sun, 10 Aug 2025 13:10:22 +0800 Subject: [PATCH 2/2] style: adjust indent --- tests/edit.test.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/edit.test.ts b/tests/edit.test.ts index 3481aac..98e5c56 100644 --- a/tests/edit.test.ts +++ b/tests/edit.test.ts @@ -153,19 +153,19 @@ describe("edit", () => { `) const input1 = dedent` - [foo] - # comment - bar = 1 + [foo] + # comment + bar = 1 ` expect(edit(input1, "foo.bar", 2, opt)).toBe(dedent` - [foo] - # comment - bar = 2 + [foo] + # comment + bar = 2 `) expect(edit(input1, "foo.bar", { baz: 3 }, opt)).toBe(dedent` - [foo] - # comment - bar = { baz = 3 } + [foo] + # comment + bar = { baz = 3 } `) // TODO: This is a known bug: https://github.com/toml-rs/toml/issues/691 // expect(edit(input1, "foo.bar", { baz: 3 }, {...opt, inline: false})).toMatchInlineSnapshot(`