Skip to content

Commit e9ccbe7

Browse files
authored
fix: Newline lost when updating a table (#323)
1 parent 524995d commit e9ccbe7

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

tomlkit/container.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,18 @@ def _replace_at(
702702
if isinstance(value, Table):
703703
# Insert a cosmetic new line for tables if:
704704
# - it does not have it yet OR is not followed by one
705-
# - it is not the last item
705+
# - it is not the last item, or
706+
# - The table being replaced has a newline
706707
last, _ = self._previous_item_with_index()
707708
idx = last if idx < 0 else idx
708709
has_ws = ends_with_whitespace(value)
710+
replace_has_ws = (
711+
isinstance(v, Table)
712+
and v.value.body
713+
and isinstance(v.value.body[-1][1], Whitespace)
714+
)
709715
next_ws = idx < last and isinstance(self._body[idx + 1][1], Whitespace)
710-
if idx < last and not (next_ws or has_ws):
716+
if (idx < last or replace_has_ws) and not (next_ws or has_ws):
711717
value.append(None, Whitespace("\n"))
712718

713719
dict.__setitem__(self, new_key.key, value.value)

0 commit comments

Comments
 (0)