Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/integrations/editor/DiffViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.

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
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?

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
Expand Down Expand Up @@ -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
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.

await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), {
preview: false,
preserveFocus: true,
})
}
}

await this.closeAllDiffViews()
Expand Down Expand Up @@ -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
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?

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))
Expand Down
Loading