-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: preserve viewport when applying diffs to active editor #8114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,7 +207,15 @@ export class DiffViewProvider { | |
| await updatedDocument.save() | ||
| } | ||
|
|
||
| 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 | ||
| const activeEditor = vscode.window.activeTextEditor | ||
| const isAlreadyActive = activeEditor && arePathsEqual(activeEditor.document.uri.fsPath, absolutePath) | ||
|
|
||
| if (!isAlreadyActive) { | ||
| // Only show the document if it's not already the active editor | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { preview: false, preserveFocus: true }) | ||
| } | ||
|
|
||
| await this.closeAllDiffViews() | ||
|
|
||
| // Getting diagnostics before and after the file edit is a better approach than | ||
|
|
@@ -405,10 +413,17 @@ export class DiffViewProvider { | |
| await updatedDocument.save() | ||
|
|
||
| if (this.documentWasOpen) { | ||
| 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 | ||
| const activeEditor = vscode.window.activeTextEditor | ||
| const isAlreadyActive = activeEditor && arePathsEqual(activeEditor.document.uri.fsPath, absolutePath) | ||
|
|
||
| if (!isAlreadyActive) { | ||
| // Only show the document if it's not already the active editor | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { | ||
| preview: false, | ||
| preserveFocus: true, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| await this.closeAllDiffViews() | ||
|
|
@@ -661,11 +676,17 @@ export class DiffViewProvider { | |
| // Open the document to ensure diagnostics are loaded | ||
| // When openFile is false (PREVENT_FOCUS_DISRUPTION enabled), we only open in memory | ||
| if (openFile) { | ||
| // Show the document in the editor | ||
| 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 | ||
| const activeEditor = vscode.window.activeTextEditor | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| const isAlreadyActive = activeEditor && arePathsEqual(activeEditor.document.uri.fsPath, absolutePath) | ||
|
|
||
| if (!isAlreadyActive) { | ||
| // Only show the document if it's not already the active editor | ||
| await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { | ||
| preview: false, | ||
| preserveFocus: true, | ||
| }) | ||
| } | ||
| } else { | ||
| // Just open the document in memory to trigger diagnostics without showing it | ||
| const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(absolutePath)) | ||
|
|
||
There was a problem hiding this comment.
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.