Skip to content

Commit 89f9b57

Browse files
committed
feat(diffViewEditor): Add autofocus feature to preserve focus on edited files
1 parent e84dd0a commit 89f9b57

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

.changeset/honest-kings-follow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"roo-cline": patch
3+
---
4+
5+
Preserve focus onto currently edited file.

src/integrations/editor/DiffViewProvider.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from "vscode"
2+
import { TextDocumentShowOptions, ViewColumn } from "vscode"
23
import * as path from "path"
34
import * as fs from "fs/promises"
45
import * as diff from "diff"
@@ -121,11 +122,6 @@ export class DiffViewProvider {
121122
throw new Error("User closed text editor, unable to edit file...")
122123
}
123124

124-
// Place cursor at the beginning of the diff editor to keep it out of
125-
// the way of the stream animation, but do this without stealing focus
126-
const beginningOfDocument = new vscode.Position(0, 0)
127-
diffEditor.selection = new vscode.Selection(beginningOfDocument, beginningOfDocument)
128-
129125
const endLine = accumulatedLines.length
130126
// Replace all content up to the current line with accumulated lines.
131127
const edit = new vscode.WorkspaceEdit()
@@ -188,15 +184,13 @@ export class DiffViewProvider {
188184
return { newProblemsMessage: undefined, userEdits: undefined, finalContent: undefined }
189185
}
190186

191-
const absolutePath = path.resolve(this.cwd, this.relPath)
192187
const updatedDocument = this.activeDiffEditor.document
193188
const editedContent = updatedDocument.getText()
194189

195190
if (updatedDocument.isDirty) {
196191
await updatedDocument.save()
197192
}
198193

199-
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), { preview: false, preserveFocus: true })
200194
await this.closeAllDiffViews()
201195

202196
// Getting diagnostics before and after the file edit is a better approach than
@@ -500,23 +494,22 @@ export class DiffViewProvider {
500494
}
501495
}),
502496
)
503-
504-
// Pre-open the file as a text document to ensure it doesn't open in preview mode
505-
// This fixes issues with files that have custom editor associations (like markdown preview)
506-
vscode.window
507-
.showTextDocument(uri, { preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true })
508-
.then(() => {
509-
// Execute the diff command after ensuring the file is open as text
510-
return vscode.commands.executeCommand(
511-
"vscode.diff",
512-
vscode.Uri.parse(`${DIFF_VIEW_URI_SCHEME}:${fileName}`).with({
513-
query: Buffer.from(this.originalContent ?? "").toString("base64"),
514-
}),
515-
uri,
516-
`${fileName}: ${fileExists ? `${DIFF_VIEW_LABEL_CHANGES}` : "New File"} (Editable)`,
517-
{ preserveFocus: true },
518-
)
519-
})
497+
// Now execute the diff command
498+
const textShowOptions: TextDocumentShowOptions = {
499+
preview: false,
500+
viewColumn: ViewColumn.Beside,
501+
preserveFocus: true,
502+
}
503+
vscode.commands
504+
.executeCommand(
505+
"vscode.diff",
506+
vscode.Uri.parse(`${DIFF_VIEW_URI_SCHEME}:${fileName}`).with({
507+
query: Buffer.from(this.originalContent ?? "").toString("base64"),
508+
}),
509+
uri,
510+
`${fileName}: ${fileExists ? `${DIFF_VIEW_LABEL_CHANGES}` : "New File"} (Editable)`,
511+
textShowOptions,
512+
)
520513
.then(
521514
() => {
522515
// Command executed successfully, now wait for the editor to appear

0 commit comments

Comments
 (0)