fix(reseed): clamp stale folds that swallow an inserted sibling heading - #293
Merged
Conversation
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.
… 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.
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.
Summary
A follow-up to PR #292's "preserve folds across external reseed" policy. That policy maps overlapping folds through an external change instead of dropping them — but 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. This reconciles mapped folds against the real Markdown structure after a reseed so the new heading stays visible.
Changes
src/webview/cm/fold/index.ts: newreconcileReseedFolds(state)— walks every folded range and clamps any fold that (a) now extends past its line's currentfoldable()section end and (b) whose excess span conceals a heading boundary, back to the real foldable range (or unfolds it when the line is no longer foldable). Guarded onsyntaxTreeAvailable(an incomplete parse frontier would clamp wrong → skip, preserving today's behaviour).src/webview/editor.ts:applyDocumentcalls it after a content-changing reseed, dispatched display-only (nochanges, nohostDocumentReseedlabel — it rewrites no bytes), so the round-trip stays byte-identical and edit-sync never fires.foldable()'ssectionEndalready excludes legit nested child headings, and requiring a concealed heading (not merely over-wide) leaves benign remaps untouched — which is why the existing fold-preservation tests (r1–r6) still pass verbatim.Related
Test Plan
(r7)intest/webview/editor.test.ts: red without the fix, green with it — asserts an external reseed inserting# Newinto a collapsed# Oneclamps the fold so# Newis not concealed, landing exactly on the currentfoldable()range.pnpm compilecleanpnpm test:unit— all 4142 tests passpnpm lintclean on changed files