Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Sep 18, 2025

Summary

This PR fixes the viewport jumping issue when Roo applies diffs to a file that's already open in the editor. Previously, the editor would jump to the top of the file, disrupting the user's workflow. This fix checks if the file is already the active editor before attempting to re-open it, preserving the current viewport position.

Problem

When Roo edits a file you're currently viewing, the editor jumps to the top. This breaks flow and forces constant scrolling, especially in large files.

Solution

  • Check if the target file is already the active editor before calling vscode.window.showTextDocument
  • Only re-open the document if it's not already active
  • Applied this fix to three methods: saveChanges, revertChanges, and saveDirectly

Testing

  • All existing tests pass
  • The fix preserves viewport position when applying diffs to the active editor
  • No regressions in diff application functionality

Related Issues

Implementation Notes

This implementation follows the maintainer's guidance to avoid re-opening the document when it's already active, rather than trying to capture and restore cursor position (which only exposes selection state, not viewport position).


Important

Fixes viewport jumping issue by checking if file is active before reopening in DiffViewProvider.ts.

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

- Check if file is already the active editor before re-opening
- Prevents viewport jump to top when applying diffs
- Applied fix to saveChanges, revertChanges, and saveDirectly methods
- Addresses issue #8112
@roomote roomote bot requested a review from mrubens as a code owner September 18, 2025 01:08
@roomote roomote bot requested review from cte and jr as code owners September 18, 2025 01:08
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Sep 18, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

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

Reviewing my own code is like debugging in a mirror - everything looks backwards but the bugs are still mine.

const isAlreadyActive = activeEditor && arePathsEqual(activeEditor.document.uri.fsPath, absolutePath)

if (!isAlreadyActive) {
// Only show the document if it's not already the active editor
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missing test coverage for this viewport preservation logic. The existing tests don't verify the behavior when exists and matches the target file path. Could we add a test case that ensures is NOT called when the file is already active?

const isAlreadyActive = activeEditor && arePathsEqual(activeEditor.document.uri.fsPath, absolutePath)

if (!isAlreadyActive) {
// Only show the document if it's not already the active editor
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code duplication detected. This same viewport preservation logic appears in three places (lines 210-217, 416-426, and 679-689). Consider extracting this into a helper method like:

This would make the code more maintainable and ensure consistency across all three methods.

preserveFocus: true,
})
// Check if the file is already the active editor to avoid viewport jump
const activeEditor = vscode.window.activeTextEditor
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Potential race condition here. The document is opened in memory first (line 692), then we check if it's active (line 680). If the file becomes active between these operations, the viewport could still jump. Is this an acceptable edge case, or should we check before line 692?

}

await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { preview: false, preserveFocus: true })
// Check if the file is already the active editor to avoid viewport jump
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consider adding a comment explaining why we're avoiding re-opening already active files. Something like:

This would help future maintainers understand the intent behind this check.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Sep 18, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Sep 22, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Sep 22, 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 Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

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

3 participants