Skip to content

fix(reseed): preserve fold state across external reseed - #292

Merged
mtskf merged 4 commits into
mainfrom
fix/reseed-preserves-fold-state
Jul 29, 2026
Merged

fix(reseed): preserve fold state across external reseed#292
mtskf merged 4 commits into
mainfrom
fix/reseed-preserves-fold-state

Conversation

@mtskf

@mtskf mtskf commented Jul 29, 2026

Copy link
Copy Markdown
Owner

Problem

An external reseed — the host pushing a fresh document snapshot after a formatter run, a git operation, or an edit from another editor — applied that snapshot as a wholesale {from: 0, to: doc.length} replace in applyDocument. CodeMirror maps every foldState range through the change, and a delete spanning the whole document collapses/drops all of them. Result: any external touch sprang the entire document open, losing every collapsed heading/list fold.

Fix

The reseed now applies the minimal single-span change instead of a whole-doc replace. computeReseedChange (new, in the pure cm/seed.ts seam) trims the longest common prefix + suffix between the live document and the snapshot, producing the smallest change that reproduces the snapshot. foldState ranges outside the real edit map through unchanged and survive; the resulting document is content-identical to the old wholesale replace.

Key design points:

  • Coordinate space: the diff operates on two CM Text values (view.state.doc and the LF-normalised snapshot), never on sliceDoc(). sliceDoc() renders with the lineSeparator facet (\r\n on a CRLF doc) while change positions are LF-internal — diffing the render would overshoot doc.length and throw / corrupt on CRLF files. Pinned by unit + integration tests.
  • Fold-overlap policy: the reseed carries no delete user event, so CM's isUserEvent-gated auto-clear does not fire — a fold overlapping the edit is remapped (clamped/dropped by RangeSet.map, never thrown), not sprung open. This is the intended behaviour for an external touch: a formatter editing a folded heading's body keeps it folded.
  • The reseed stays a single atomic dispatch; the update-loop guard (version acceptance, echo-ack, write lock, seeding guard, selection restore) is untouched — only the changes payload granularity changed.
  • Annotation-keyed fields (fenced-collapse DD3, callout conceal, image-paste anchors) still rebuild/discard on a real reseed by design; scope here is native foldState.

Tests

  • cm-seed-reseed-change.test.ts (8): byte-exact reassembly across prefix/suffix/divergent/identical/surrogate/CRLF-origin inputs.
  • editor.test.ts group (r1–r6): fold above / below (position-checked) / containing the edit survives; CRLF interior reseed does not throw; reseed posts no edit; EOL-only no-op reseed keeps folds. Revert-check confirmed: restoring the whole-doc replace turns r1/r2/r3/r5/r6 red.

pnpm compile + full pnpm test:unit (4141) + Biome all green.

mtskf added 4 commits July 29, 2026 18:39
computeReseedChange trims the common prefix/suffix between the live CM
document and the host snapshot (both LF-internal Text) to produce the
smallest single-span change that reproduces the snapshot. This is the
seam the reseed will use so CodeMirror maps fold ranges outside the real
edit through unchanged instead of collapsing every fold on a whole-doc
replace.
…pan change

An external reseed (formatter, git, another editor) applied the host
snapshot as a wholesale {from: 0, to: doc.length} replace. CodeMirror
maps every foldState range through the change, so a whole-doc delete
dropped ALL folds — any external touch sprang the document open.

The reseed now applies the minimal single-span change (computeReseedChange
on the live doc and the snapshot, both LF-internal Text), so foldState
ranges outside the real edit map through unchanged and survive. Folds that
overlap the edit are remapped (never thrown); the reseed carries no delete
user event, so an overlapping fold is preserved rather than sprung open —
the intended behaviour for an external touch.
The prepend test asserted only change.from and round-trip equality, which
cannot detect a non-minimal `to` (the excess is absorbed into `insert`).
Assert change.to === 0 so the suffix scan is actually pinned.
@mtskf
mtskf merged commit e794d04 into main Jul 29, 2026
1 check passed
@mtskf
mtskf deleted the fix/reseed-preserves-fold-state branch July 29, 2026 10:33
mtskf added a commit that referenced this pull request Jul 29, 2026
…ng (#293)

* fix(reseed): clamp stale folds that swallow an inserted sibling heading

The minimal-span reseed maps overlapping folds through an external change
rather than dropping them (PR #292). The trade-off is that an external insert
INTO a collapsed section can widen the mapped fold to swallow a newly-inserted
sibling heading, hiding it until the user unfolds.

After a content-changing reseed, applyDocument now reconciles native folds:
reconcileReseedFolds clamps any fold that extends past its line's current
foldable() section end AND whose excess span conceals a heading boundary back
to the real foldable range (or unfolds it). foldable()'s sectionEnd already
excludes legit child headings, and requiring a concealed heading leaves benign
over-wide remaps untouched. Guarded on syntaxTreeAvailable; dispatched
display-only (no changes), so the round-trip stays byte-identical.

* fix(reseed): release orphaned folds whose line is no longer foldable; pin child-heading clamp

reconcileReseedFolds only clamped a stale fold back to its canonical
range when the excess span concealed a heading boundary. A fold whose
own heading line lost its marker entirely (fresh foldable() === null)
had no canonical range to clamp to, but fell through the same
concealment gate and could survive indefinitely with nothing left to
show for it.

Release that fold instead, gated on the reseed's edit actually
touching the fold's own line (editedRange, threaded from
computeReseedChange's post-change span) so an unrelated edit elsewhere
in the document cannot spring open a fold that was never canonical to
begin with (e.g. a hand-applied fold over a non-foldable paragraph).

Also documents the reconcileReseedFolds contract accurately: it is
exercised indirectly via editor.ts's applyDocument, not imported
directly by any test.

* fix(reseed): release orphaned folds when the reseed deletes the section body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant