Skip to content

fix(editor): keep notebook cell undo across cell and tab switches - #34952

Merged
OrcaWin merged 1 commit into
mainfrom
OrcaWin/notebook-cell-undo
Oct 29, 2026
Merged

OrcaWin merged 1 commit into
mainfrom
OrcaWin/notebook-cell-undo

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​122 $\color{#cf222e}{\Huge{\mathbf{−}}}$​2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​120
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​16 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​15

Problem

A notebook cell's Monaco model was anonymous and was disposed when the cell lost focus or when the notebook view unmounted (for example on a file tab switch). After returning to a cell, Undo did nothing. Regular file tabs keep their undo history.

This PR also folds in #35008 (undo after tab switches), so both cases are handled by one mechanism.

Fix

  • Cells that have an nbformat cell id now get a named model URI (vscode-userdata:/[fileId, view scope, cell id]). Monaco's model service keeps the undo stack of a disposed model with that scheme and restores it when a model with the same URI is created again. It restores only if the text is unchanged (SHA1 check), so an external reload or Discard while the cell was inactive drops the old history. Retained history counts against Monaco's existing 20 MiB closed-file undo budget, which it shares with closed file tabs, and is evicted in LRU order.
  • vscode-userdata is used rather than vscode-notebook-cell because, in the shipped Monaco 0.55.1 ESM, only the model service checks it. The notebook-cell scheme also enables notebook-specific paths in word highlighting, inline completions and language detection.
  • Cells without an id (nbformat < 4.5) keep anonymous models, as before. Their position-based key can move to a different cell after an insert, move or delete, so restored history could otherwise land on the wrong cell.
  • If a live model already uses the URI (duplicate cell ids in one notebook, or the same cell open in two places at once), the editor falls back to an anonymous model instead of letting Monaco throw Cannot add model because it already exists.
  • The model lifecycle is otherwise unchanged from main: models are still created on activation and disposed on deactivation. The earlier approach of keeping inactive models alive was dropped.

Testing

New cases in IpynbCellEditor.external-sync.test.tsx (real Monaco models):

  • Undo restores a cell's edit after switching to another cell, and after the notebook view unmounts and remounts. Fails without the named URI.
  • Undo does nothing on a cell whose source changed while it was inactive.
  • An id-less cell that moves into an edited cell's position, with identical text, does not inherit that cell's undo. Fails if id-less cells get position-keyed URIs.
  • Activating a cell in a notebook with duplicate cell ids does not throw. Fails without the fallback.

pnpm test on the two notebook editor test files passes. ORCA_BACKGROUND_LAUNCH=1 pnpm check:changed --maxWorkers=2 passes. The app was not launched for this revision. The change is renderer-only, with no platform, remote or wire impact, and applies the same way to git and folder workspaces.

@OrcaWin
OrcaWin force-pushed the OrcaWin/notebook-cell-undo branch from 76ad7b8 to b208913 Compare October 28, 2026 23:21
@OrcaWin OrcaWin changed the title fix(editor): preserve notebook cell undo while inactive fix(editor): keep notebook cell undo across cell and tab switches Oct 28, 2026
@OrcaWin
OrcaWin marked this pull request as ready for review October 29, 2026 00:08
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 7 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b83a1787-58bf-419f-bba2-75c90f9e13a6
Commits

Reviewing files that changed from the base of the PR and between b2feed0 and 35643c5.

Files selected for processing: 4
  • src/renderer/src/components/editor/IpynbCellEditor.external-sync.test.tsx
  • src/renderer/src/components/editor/IpynbCellEditor.tsx
  • src/renderer/src/components/editor/IpynbRendering.test.tsx
  • src/renderer/src/components/editor/IpynbViewer.tsx
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Keeps notebook cell undo history across cell switches and notebook view remounts.

  • Notebook cells with stable IDs keep their undo history when reopened.

No actionable issues were found. Tests were not run during this review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Activate cell] --> B{Cell has an id?}
  B -->|No| C[Create anonymous model]
  B -->|Yes| D[Build file and view scoped URI]
  D --> E{URI already has a live model?}
  E -->|Yes| C
  E -->|No| F[Create named model]
  C --> G[Edit cell]
  F --> G
  G --> H[Dispose model when editor unmounts]
Loading

Reviews (2) · Last reviewed commit: "fix(editor): keep notebook cell undo acr..." · Reviewed by Greptile

Notebook cell models were anonymous and disposed whenever a cell lost
focus or the notebook view unmounted on a tab switch, so Undo did
nothing after returning to a cell.

Give cells that have a stable nbformat id a vscode-userdata model URI,
scoped to the view, so Monaco's model service keeps and restores the
disposed model's undo stack. Monaco restores only when the text is
unchanged and bounds retained history to its closed-file budget.
Id-less cells keep anonymous models because their position-based key
can move to a different cell, and a duplicate id falls back to an
anonymous model instead of colliding.
@OrcaWin
OrcaWin force-pushed the OrcaWin/notebook-cell-undo branch from b208913 to 35643c5 Compare October 29, 2026 00:27
@OrcaWin
OrcaWin merged commit aadde96 into main Oct 29, 2026
35 checks passed
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