Skip to content

fix(editor): preserve folds and horizontal scroll across tab switches - #34810

Merged
OrcaWin merged 4 commits into
mainfrom
OrcaWin/editor-fold-restore
Oct 29, 2026
Merged

OrcaWin merged 4 commits into
mainfrom
OrcaWin/editor-fold-restore

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Switching away from a source tab and back restores only the selection and vertical scroll. Folded regions reopen and horizontal scroll resets to column 0, because MonacoEditor recreates the editor on tab switch (saveViewState={false}) and Orca's own snapshot keeps just selections + scrollTop.

Fix

Also snapshot Monaco's full ICodeEditorViewState (cursor, scroll, and contribution state such as folding) into an LRU cache beside diffViewStateCache, keyed by the existing view-state key plus the model URI. On restore, prefer it; otherwise fall back to the existing selection/scrollTop caches. Monaco's folding contribution validates restored folds against the current line count, so external edits don't apply stale folds.

Tests

monaco-view-state-persistence.test.ts covers restoring the full viewport, selections and folded regions after a tab remount, and checks that saved folds are not applied to another host or pane showing the same path. Removed the Playwright evidence spec from the original draft.

@OrcaWin
OrcaWin force-pushed the OrcaWin/editor-fold-restore branch from 2e9600e to fbc6be3 Compare October 29, 2026 00:37
@OrcaWin
OrcaWin marked this pull request as ready for review October 29, 2026 00:37
@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 11 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: e588bcad-5e67-4bac-be76-13933b3e7921
Commits

Reviewing files that changed from the base of the PR and between 6f41a55 and fbc6be3.

Files selected for processing: 6
  • src/renderer/src/components/editor/MonacoEditor.owner-view-state.test.tsx
  • src/renderer/src/components/editor/editor-model-lifetime-fixture.ts
  • src/renderer/src/components/editor/monaco-view-state-persistence.test.ts
  • src/renderer/src/components/editor/monaco-view-state-persistence.ts
  • src/renderer/src/components/editor/use-monaco-editor-mount.stale-cache.test.tsx
  • src/renderer/src/lib/scroll-cache.ts
  • 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: 4/5

[Medium impact] The change affects the source-code editor tab-switching workflow.

The PR appears safe to merge, with a non-blocking gap in coverage for real tab switches.

Findings

  1. P2 Real tab switches lack coverage ▶

Summary

Preserves Monaco's full view state across editor remounts.

  • Source tabs restore folds and horizontal scroll when you return.

Reviews (1) · Last reviewed commit: "refactor(editor): keep editor view state..." · Reviewed by Greptile

Comment on lines +64 to +75
const restoreViewState = vi.fn()
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: restore accesses only these stubbed editor methods.
const remountedEditor = {
getModel: sourceEditor.getModel,
restoreViewState,
setSelections,
setScrollTop,
focus
} as unknown as editor.IStandaloneCodeEditor
restoreMonacoViewState(remountedEditor, 'file.ts::tab-1')

expect(setSelections).toHaveBeenCalledWith(selections)
expect(setScrollTop).toHaveBeenCalledWith(320)
expect(restoreViewState).toHaveBeenCalledExactlyOnceWith(viewState)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Real tab switches lack coverage

This test passes a hand-written object to a mocked restoreViewState. It never checks that Monaco restores folds or horizontal scroll after a tab switch, so it would pass even if either behavior remained broken. Add a test with a real editor: fold a region, scroll horizontally, switch tabs, then return and check both.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@OrcaWin
OrcaWin merged commit 8600995 into main Oct 29, 2026
61 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