fix(reseed): preserve fold state across external reseed - #292
Merged
Conversation
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.
4 tasks
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inapplyDocument. CodeMirror maps everyfoldStaterange 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 purecm/seed.tsseam) trims the longest common prefix + suffix between the live document and the snapshot, producing the smallest change that reproduces the snapshot.foldStateranges outside the real edit map through unchanged and survive; the resulting document is content-identical to the old wholesale replace.Key design points:
Textvalues (view.state.docand the LF-normalised snapshot), never onsliceDoc().sliceDoc()renders with thelineSeparatorfacet (\r\non a CRLF doc) while change positions are LF-internal — diffing the render would overshootdoc.lengthand throw / corrupt on CRLF files. Pinned by unit + integration tests.deleteuser event, so CM's isUserEvent-gated auto-clear does not fire — a fold overlapping the edit is remapped (clamped/dropped byRangeSet.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.changespayload granularity changed.foldState.Tests
cm-seed-reseed-change.test.ts(8): byte-exact reassembly across prefix/suffix/divergent/identical/surrogate/CRLF-origin inputs.editor.test.tsgroup (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+ fullpnpm test:unit(4141) + Biome all green.