Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Newline lost when updating a table #323

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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