From 99e7febd5fb308915f564badd404b709464d7e5b Mon Sep 17 00:00:00 2001 From: hannesrudolph Date: Mon, 30 Jun 2025 10:06:00 -0600 Subject: [PATCH 1/2] fix: preserve chat input focus during automated file editing (#4574) - Add preserveFocus: true to showTextDocument call in DiffViewProvider - Prevents focus from being stolen from chat input during auto-mode file edits - Update tests to reflect new expected behavior Fixes #4574 --- src/integrations/editor/DiffViewProvider.ts | 2 +- src/integrations/editor/__tests__/DiffViewProvider.spec.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/integrations/editor/DiffViewProvider.ts b/src/integrations/editor/DiffViewProvider.ts index b38c55c3e4..cb4d96daa8 100644 --- a/src/integrations/editor/DiffViewProvider.ts +++ b/src/integrations/editor/DiffViewProvider.ts @@ -504,7 +504,7 @@ export class DiffViewProvider { // Pre-open the file as a text document to ensure it doesn't open in preview mode // This fixes issues with files that have custom editor associations (like markdown preview) vscode.window - .showTextDocument(uri, { preview: false, viewColumn: vscode.ViewColumn.Active }) + .showTextDocument(uri, { preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }) .then(() => { // Execute the diff command after ensuring the file is open as text return vscode.commands.executeCommand( diff --git a/src/integrations/editor/__tests__/DiffViewProvider.spec.ts b/src/integrations/editor/__tests__/DiffViewProvider.spec.ts index 44b3aaba2a..68d28b9579 100644 --- a/src/integrations/editor/__tests__/DiffViewProvider.spec.ts +++ b/src/integrations/editor/__tests__/DiffViewProvider.spec.ts @@ -176,7 +176,7 @@ describe("DiffViewProvider", () => { // Mock showTextDocument to track when it's called vi.mocked(vscode.window.showTextDocument).mockImplementation(async (uri, options) => { callOrder.push("showTextDocument") - expect(options).toEqual({ preview: false, viewColumn: vscode.ViewColumn.Active }) + expect(options).toEqual({ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }) return mockEditor as any }) @@ -211,7 +211,7 @@ describe("DiffViewProvider", () => { // Verify that showTextDocument was called with preview: false expect(vscode.window.showTextDocument).toHaveBeenCalledWith( expect.objectContaining({ fsPath: `${mockCwd}/test.md` }), - { preview: false, viewColumn: vscode.ViewColumn.Active }, + { preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }, ) // Verify that the diff command was executed From 18dcd17bc9cbca51fd098c6a5a9c854434b35259 Mon Sep 17 00:00:00 2001 From: hannesrudolph Date: Mon, 30 Jun 2025 11:17:14 -0600 Subject: [PATCH 2/2] refactor: improve test resilience and code readability - Use expect.objectContaining in tests for better resilience to future option additions - Extract showTextDocument options to named constant for improved readability - Addresses Copilot review feedback on PR #5282 --- src/integrations/editor/DiffViewProvider.ts | 3 ++- .../editor/__tests__/DiffViewProvider.spec.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/integrations/editor/DiffViewProvider.ts b/src/integrations/editor/DiffViewProvider.ts index cb4d96daa8..0bb74f5348 100644 --- a/src/integrations/editor/DiffViewProvider.ts +++ b/src/integrations/editor/DiffViewProvider.ts @@ -503,8 +503,9 @@ export class DiffViewProvider { // Pre-open the file as a text document to ensure it doesn't open in preview mode // This fixes issues with files that have custom editor associations (like markdown preview) + const showOptions = { preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true } vscode.window - .showTextDocument(uri, { preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }) + .showTextDocument(uri, showOptions) .then(() => { // Execute the diff command after ensuring the file is open as text return vscode.commands.executeCommand( diff --git a/src/integrations/editor/__tests__/DiffViewProvider.spec.ts b/src/integrations/editor/__tests__/DiffViewProvider.spec.ts index 68d28b9579..4656203fb4 100644 --- a/src/integrations/editor/__tests__/DiffViewProvider.spec.ts +++ b/src/integrations/editor/__tests__/DiffViewProvider.spec.ts @@ -176,7 +176,13 @@ describe("DiffViewProvider", () => { // Mock showTextDocument to track when it's called vi.mocked(vscode.window.showTextDocument).mockImplementation(async (uri, options) => { callOrder.push("showTextDocument") - expect(options).toEqual({ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }) + expect(options).toEqual( + expect.objectContaining({ + preview: false, + viewColumn: vscode.ViewColumn.Active, + preserveFocus: true, + }), + ) return mockEditor as any }) @@ -211,7 +217,7 @@ describe("DiffViewProvider", () => { // Verify that showTextDocument was called with preview: false expect(vscode.window.showTextDocument).toHaveBeenCalledWith( expect.objectContaining({ fsPath: `${mockCwd}/test.md` }), - { preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }, + expect.objectContaining({ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true }), ) // Verify that the diff command was executed