Skip to content

Commit 54c4916

Browse files
committed
fix: preserve viewport when applying diffs to active editor
- 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
1 parent 87b45de commit 54c4916

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

src/integrations/editor/DiffViewProvider.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,15 @@ export class DiffViewProvider {
207207
await updatedDocument.save()
208208
}
209209

210-
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { preview: false, preserveFocus: true })
210+
// Check if the file is already the active editor to avoid viewport jump
211+
const activeEditor = vscode.window.activeTextEditor
212+
const isAlreadyActive = activeEditor && arePathsEqual(activeEditor.document.uri.fsPath, absolutePath)
213+
214+
if (!isAlreadyActive) {
215+
// Only show the document if it's not already the active editor
216+
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { preview: false, preserveFocus: true })
217+
}
218+
211219
await this.closeAllDiffViews()
212220

213221
// Getting diagnostics before and after the file edit is a better approach than
@@ -405,10 +413,17 @@ export class DiffViewProvider {
405413
await updatedDocument.save()
406414

407415
if (this.documentWasOpen) {
408-
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), {
409-
preview: false,
410-
preserveFocus: true,
411-
})
416+
// Check if the file is already the active editor to avoid viewport jump
417+
const activeEditor = vscode.window.activeTextEditor
418+
const isAlreadyActive = activeEditor && arePathsEqual(activeEditor.document.uri.fsPath, absolutePath)
419+
420+
if (!isAlreadyActive) {
421+
// Only show the document if it's not already the active editor
422+
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), {
423+
preview: false,
424+
preserveFocus: true,
425+
})
426+
}
412427
}
413428

414429
await this.closeAllDiffViews()
@@ -661,11 +676,17 @@ export class DiffViewProvider {
661676
// Open the document to ensure diagnostics are loaded
662677
// When openFile is false (PREVENT_FOCUS_DISRUPTION enabled), we only open in memory
663678
if (openFile) {
664-
// Show the document in the editor
665-
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), {
666-
preview: false,
667-
preserveFocus: true,
668-
})
679+
// Check if the file is already the active editor to avoid viewport jump
680+
const activeEditor = vscode.window.activeTextEditor
681+
const isAlreadyActive = activeEditor && arePathsEqual(activeEditor.document.uri.fsPath, absolutePath)
682+
683+
if (!isAlreadyActive) {
684+
// Only show the document if it's not already the active editor
685+
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), {
686+
preview: false,
687+
preserveFocus: true,
688+
})
689+
}
669690
} else {
670691
// Just open the document in memory to trigger diagnostics without showing it
671692
const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(absolutePath))

0 commit comments

Comments
 (0)