Skip to content

Conversation

@heyseth
Copy link

@heyseth heyseth commented Oct 11, 2025

Related GitHub Issue

Closes: #8112

Description

This PR fixes the viewport jumping issue when Roo edits a file that's currently open. Previously, the editor would reset to the top of the file or scroll to the edited lines, breaking the user's workflow. Now the exact scroll position is preserved throughout the edit process.

Implementation approach:

  • Viewport capture timing: The viewport position (visibleRanges and viewColumn) is captured in the open() method before the original file tab is closed and the diff view is opened. This ensures we have the true pre-edit viewport state.
  • Storage: Two new private properties store the captured state:
    • originalViewportRanges: The visible line ranges in the editor
    • originalViewColumn: Which column/pane the editor was in
  • Restoration strategy: The viewport is restored by revealing the middle line of the original visible range using TextEditorRevealType.InCenter.
  • Coverage: The fix applies to three code paths:
    • saveChanges(): Normal diff view workflow
    • revertChanges(): When user rejects changes
    • saveDirectly(): PREVENT_FOCUS_DISRUPTION experiment path (no diff view)

Test Procedure

  1. Autosave changes viewport test:

    • Open a large file (500+ lines) in VSCode
    • Scroll to line 300
    • Ensure that write permissions are enabled for Roo
    • Use Roo to edit the file (e.g., "add a comment at line 50")
    • Expected: Editor remains scrolled to line 300
    • Result: ✅ Viewport preserved correctly
  2. Manual save changes viewport test:

    • Open a file scrolled to middle
    • Ensure that write permissions are disabled for Roo
    • Use Roo to edit the file
    • Manually accept the changes
    • Expected: File saves and viewport preserved
    • Result: ✅ Viewport preserved correctly
  3. Manual revert changes viewport test:

    • Open a file scrolled to middle
    • Ensure that write permissions are disabled for Roo
    • Use Roo to edit the file
    • Manually reject the changes
    • Expected: File reverts and viewport preserved
    • Result: ✅ Viewport preserved on revert
  4. New file creation viewport test:

    • Use Roo to create a new file
    • Expected: File opens normally (no viewport restoration)
    • Result: ✅ Works correctly
  5. PREVENT_FOCUS_DISRUPTION experiment:

    • Enable the "background editing" experimental flag
    • Open file scrolled to middle
    • Use Roo to apply a diff (uses saveDirectly() path)
    • Expected: Viewport preserved without diff view
    • Result: ✅ Viewport preserved

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

Get in Touch

Discord: @ocean.smith


Important

Preserve editor viewport position when applying diffs in DiffViewProvider by capturing and restoring viewport state.

  • Behavior:
    • Preserve viewport position in DiffViewProvider when applying diffs to active editor.
    • Capture visibleRanges and viewColumn in open() before closing original tab.
    • Restore viewport using TextEditorRevealType.InCenter in saveChanges(), revertChanges(), and saveDirectly().
  • Implementation:
    • Add originalViewportRanges and originalViewColumn properties to store viewport state.
    • Modify open(), saveChanges(), revertChanges(), and saveDirectly() to handle viewport restoration.
  • Misc:
    • Ensure viewport restoration applies to normal, revert, and experimental save paths.

This description was created by Ellipsis for fac6fba. You can customize this summary. It will automatically update as commits are pushed.

Fixes RooCodeInc#8112

When Roo edits a file that's currently open, the viewport now stays at
the user's original scroll position instead of jumping to the top.
This prevents workflow disruption during file edits.

Changes:
- Added originalViewportRanges and originalViewColumn properties to
  DiffViewProvider to capture viewport state before opening diff view
- Modified open() to capture viewport position before closing the
  original file tab
- Updated saveChanges() to restore exact viewport by revealing the
  middle line of the original visible range
- Updated revertChanges() to preserve viewport when reverting changes
- Updated saveDirectly() to capture and restore viewport for
  PREVENT_FOCUS_DISRUPTION experiment
- Modified reset() to clear viewport state

The fix uses the middle line of the original visible range with
InCenter reveal type, which accurately reconstructs the viewport.
@heyseth heyseth requested review from cte, jr and mrubens as code owners October 11, 2025 23:04
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Oct 11, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Oct 11, 2025
The setTimeout in viewport restoration introduced an unnecessary race condition when reopening editors. Since showTextDocument() already returns the
TextEditor reference, we now use that directly instead of searching for the editor again after a delay.
Removed unnecessary editor searches using vscode.window.visibleTextEditors.find() after showTextDocument() calls. Since showTextDocument() already
returns the TextEditor reference, we can use it directly for viewport restoration instead of searching for the editor again.

This change applies to both the acceptDiff() and rejectDiff() code paths, making viewport restoration more reliable and eliminating potential race
conditions where the editor might not be immediately available in the visibleTextEditors array.
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Oct 11, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Oct 28, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working PR - Needs Preliminary Review size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Status: PR [Needs Prelim Review]

Development

Successfully merging this pull request may close these issues.

[BUG] Preserve viewport when applying diffs to the active editor (avoid re-open)

2 participants