Skip to content

Commit

Permalink
fix: Newline lost when updating a table (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Nov 6, 2023
1 parent 524995d commit e9ccbe7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e9ccbe7

Please sign in to comment.