refactor: extract focus restoration logic and improve consistency

- Created restoreEditorFocus() helper method to eliminate code duplication
- Changed preserveFocus from false to true for consistent behavior
- Added error handling with silent logging for focus restoration failures
- Added null checks for undefined activeTextEditor cases
This commit is contained in:
hannesrudolph 2025-07-02 12:19:35 -06:00
parent 4dcdc184ff
commit bd8163d9dc

View file

@ -128,12 +128,7 @@ export class DiffViewProvider {
diffEditor.selection = new vscode.Selection(beginningOfDocument, beginningOfDocument)
// Restore focus to the previously active editor if it changed
if (currentActiveEditor && vscode.window.activeTextEditor !== currentActiveEditor) {
await vscode.window.showTextDocument(currentActiveEditor.document, {
preserveFocus: false,
viewColumn: currentActiveEditor.viewColumn,
})
}
await this.restoreEditorFocus(currentActiveEditor)
const endLine = accumulatedLines.length
// Replace all content up to the current line with accumulated lines.
@ -153,12 +148,7 @@ export class DiffViewProvider {
this.scrollEditorToLine(endLine)
// Restore focus if scrolling stole it
if (currentActiveEditor && vscode.window.activeTextEditor !== currentActiveEditor) {
await vscode.window.showTextDocument(currentActiveEditor.document, {
preserveFocus: false,
viewColumn: currentActiveEditor.viewColumn,
})
}
await this.restoreEditorFocus(currentActiveEditor)
}
// Update the streamedLines with the new accumulated content.
@ -578,12 +568,7 @@ export class DiffViewProvider {
)
// Restore focus if scrolling stole it
if (currentActiveEditor && vscode.window.activeTextEditor !== currentActiveEditor) {
await vscode.window.showTextDocument(currentActiveEditor.document, {
preserveFocus: false,
viewColumn: currentActiveEditor.viewColumn,
})
}
await this.restoreEditorFocus(currentActiveEditor)
return
}
@ -619,4 +604,27 @@ export class DiffViewProvider {
this.streamedLines = []
this.preDiagnostics = []
}
/**
* Restores focus to the previously active editor if focus was stolen
* @param previousEditor The editor that should have focus
*/
private async restoreEditorFocus(previousEditor: vscode.TextEditor | undefined): Promise<void> {
if (!previousEditor) {
return
}
try {
// Check if focus has changed
if (vscode.window.activeTextEditor !== previousEditor) {
await vscode.window.showTextDocument(previousEditor.document, {
preserveFocus: true,
viewColumn: previousEditor.viewColumn,
})
}
} catch (error) {
// Silently handle errors - focus restoration is a best-effort operation
console.debug("Failed to restore editor focus:", error)
}
}
}