From e9ccbe7f3eacbbe3e39d68d9bb4c3f0552f1b8f2 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Mon, 6 Nov 2023 18:52:19 +0800 Subject: [PATCH] fix: Newline lost when updating a table (#323) --- tomlkit/container.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tomlkit/container.py b/tomlkit/container.py index 319f3c9..917a255 100644 --- a/tomlkit/container.py +++ b/tomlkit/container.py @@ -702,12 +702,18 @@ def _replace_at( if isinstance(value, Table): # Insert a cosmetic new line for tables if: # - it does not have it yet OR is not followed by one - # - it is not the last item + # - it is not the last item, or + # - The table being replaced has a newline last, _ = self._previous_item_with_index() idx = last if idx < 0 else idx has_ws = ends_with_whitespace(value) + replace_has_ws = ( + isinstance(v, Table) + and v.value.body + and isinstance(v.value.body[-1][1], Whitespace) + ) next_ws = idx < last and isinstance(self._body[idx + 1][1], Whitespace) - if idx < last and not (next_ws or has_ws): + if (idx < last or replace_has_ws) and not (next_ws or has_ws): value.append(None, Whitespace("\n")) dict.__setitem__(self, new_key.key, value.value)