From 52d469a588411f1e50adbdac665449228e04f565 Mon Sep 17 00:00:00 2001 From: hannesrudolph Date: Mon, 16 Jun 2025 12:41:47 -0600 Subject: [PATCH] Fix focus loss issue when AI is editing files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add preserveFocus parameter to revealRange calls in DiffViewProvider - Use workflow detection to determine when to preserve focus during scrolling - Prevents chat from losing focus when AI is editing files with diff view - Addresses daniel-lxs's reported issue where focus was still being lost This fix ensures that when the AI is actively processing (streaming, waiting for chunks, or in auto-approval mode), the diff view won't steal focus from the chat input, preventing accidental typing in the wrong window. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/integrations/editor/DiffViewProvider.ts | 33 +++++++++++++++------ 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/integrations/editor/DiffViewProvider.ts b/src/integrations/editor/DiffViewProvider.ts index 3ab0419618..2f6554c54d 100644 --- a/src/integrations/editor/DiffViewProvider.ts +++ b/src/integrations/editor/DiffViewProvider.ts @@ -11,6 +11,7 @@ import { formatResponse } from "../../core/prompts/responses" import { diagnosticsToProblemsString, getNewDiagnostics } from "../diagnostics" import { ClineSayTool } from "../../shared/ExtensionMessage" import { Task } from "../../core/task/Task" +import { isInAutomatedWorkflowFromVisibleProvider } from "../../utils/workflow-detection" import { DecorationController } from "./DecorationController" @@ -464,10 +465,17 @@ export class DiffViewProvider { if (this.activeDiffEditor) { const scrollLine = line + 4 - this.activeDiffEditor.revealRange( - new vscode.Range(scrollLine, 0, scrollLine, 0), - vscode.TextEditorRevealType.InCenter, - ) + // Check if we're in an automated workflow to preserve chat focus. + // When the AI is actively processing, we skip scrolling to prevent + // the diff view from stealing focus from the chat input. + const shouldPreserveFocus = isInAutomatedWorkflowFromVisibleProvider() + + if (!shouldPreserveFocus) { + this.activeDiffEditor.revealRange( + new vscode.Range(scrollLine, 0, scrollLine, 0), + vscode.TextEditorRevealType.InCenter, + ) + } } } @@ -481,13 +489,20 @@ export class DiffViewProvider { let lineCount = 0 + // Check if we're in an automated workflow to preserve chat focus. + // When the AI is actively processing, we skip scrolling to prevent + // the diff view from stealing focus from the chat input. + const shouldPreserveFocus = isInAutomatedWorkflowFromVisibleProvider() + for (const part of diffs) { if (part.added || part.removed) { - // Found the first diff, scroll to it. - this.activeDiffEditor.revealRange( - new vscode.Range(lineCount, 0, lineCount, 0), - vscode.TextEditorRevealType.InCenter, - ) + // Found the first diff, scroll to it only if not in automated workflow + if (!shouldPreserveFocus) { + this.activeDiffEditor.revealRange( + new vscode.Range(lineCount, 0, lineCount, 0), + vscode.TextEditorRevealType.InCenter, + ) + } return }