fix(undo): keep per-document undo so Cmd+Z survives file switches#77
Merged
Conversation
A single reused NSTextView shared one undo manager across all open documents, and the editor wiped it via removeAllActions() on every document switch — so Cmd+Z stopped working after changing files. Vend a per-documentId UndoManager through the new undoManager(for:) delegate method; each file keeps its own stack that survives switching away and back. On switch we break undo coalescing on the outgoing document instead of clearing it, and evict undo stacks for documents no longer retained (alongside the existing scrollOffsets pruning). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
luca-chen198
added a commit
that referenced
this pull request
Jun 23, 2026
…ile switched away (#78) Per-document undo (#77) makes a document's undo stack survive switching away and back. But the stack records absolute text ranges, so if the document's file is rewritten externally while backgrounded — e.g. renaming a node rewrites the [[label]] in every file that links it — switch-back reloads the changed text and Cmd+Z replays stale ranges, corrupting the text (or silently no-op'ing when a range is out of bounds). Snapshot each document's content (storage form) on switch-away and, on switch-back, drop its undo stack when the reloaded text differs. Snapshots are pruned alongside the per-document undo managers. Also corrects the now-stale `documentId` doc comment. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
A single reused
NSTextViewis shared across all open documents, and the editor wiped its undo manager viaremoveAllActions()on every document switch — so Cmd+Z stopped working after changing files.Fix
Vend a per-
documentIdUndoManagerthrough the newundoManager(for:)delegate method, so each file keeps its own undo stack that survives switching away and back. On switch we break undo coalescing on the outgoing document instead of clearing it, and evict undo stacks for documents no longer retained (alongside the existingscrollOffsetspruning).Net diff: +40 / −1 across 3 files (no public API removed;
removeAllActions()on switch replaced by the per-document manager).Test
Verified in a Debug build: edit file A → switch to B → back to A → Cmd+Z still undoes A's edits. Also checked: multiple consecutive undo, redo, docs with
[[wiki-links]]/headings (display ≠ storage text), and no cross-document leak (undo in B never touches A).🤖 Generated with Claude Code