Skip to content

fix(undo): keep per-document undo so Cmd+Z survives file switches#77

Merged
luca-chen198 merged 1 commit into
mainfrom
per-document-undo
Jun 22, 2026
Merged

fix(undo): keep per-document undo so Cmd+Z survives file switches#77
luca-chen198 merged 1 commit into
mainfrom
per-document-undo

Conversation

@luca-chen198

Copy link
Copy Markdown
Member

Problem

A single reused NSTextView is shared across all open documents, and the editor wiped its undo manager via removeAllActions() on every document switch — so Cmd+Z stopped working after changing files.

Fix

Vend a per-documentId UndoManager through the new undoManager(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 existing scrollOffsets pruning).

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

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 luca-chen198 merged commit 445247a into main Jun 22, 2026
1 check passed
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>
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