Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/yamltrip/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,16 @@ def sync(self, *keys: KeyPart, value: Any) -> Document:
except (ValueError, KeyError):
return self.upsert(*normalized, value=value)

# Pre-convert any flow sequences that will be modified.
# This targets only the affected leaf paths, preserving sibling formatting.
# Two-phase flow-sequence handling:
#
# Phase 1 (_flow_seq_replacements) pre-converts only the flow sequences
# that will be modified. A full replace at a higher path would
# re-serialize the whole mapping with the default indentation, losing
# the document's existing indent for nested lists.
#
# Phase 2 (except PatchError below) catches cases phase 1 misses. The
# known gap is list reordering: new indices in the diff weren't in the
# original tree, so phase 1 couldn't anticipate them.
doc: Document = self
flow_patches = _flow_seq_replacements(
self._core_doc, old_value, value, normalized
Expand All @@ -488,7 +496,8 @@ def sync(self, *keys: KeyPart, value: Any) -> Document:
if _classify_patch_error(e) != _PatchErrorKind.BLOCK_SEQUENCE_EXPECTED:
raise
# Fallback: a flow sequence was missed by pre-detection (e.g. due to
# list reordering). Replace the entire synced value.
# list reordering). Full replace from original doc; phase 1 work
# is superseded.
route = _make_route(normalized)
op = Op.replace(value)
return self._apply_patches([Patch(route=route, operation=op)])
Expand Down
Loading