Elffuss BraidHub

kikocis/continue public

fix: focus on the selected file for edit/apply when showing diffs (#7722)

Changeset154c34fa97990a193706a659b58cc60339a2e084f63517dcbf4a4265ebf670b9
AuthorAditya Mitra <61635505+uinstinct@users.noreply.github.com> · 2025-09-17 04:34 +05:30
CommitterGitHub <noreply@github.com> · 2025-09-16 16:04 -07:00
Parents5f1e5b279846

Browse the files at this changeset

2 files changed

modified extensions/vscode/src/diff/vertical/handler.ts +38 −0
@@ -76,6 +76,39 @@
7676 this.disposables.push(disposable);
7777 }
7878
79+ /** ensures the current target file is open and focused before performing edits*/
80+ private async ensureCurrentFileIsFocused() {
81+ const targetUri = this.editor.document.uri;
82+ const active = vscode.window.activeTextEditor;
83+ if (
84+ active &&
85+ URI.equal(active.document.uri.toString(), targetUri.toString())
86+ ) {
87+ this.editor = active;
88+ return;
89+ }
90+
91+ const visible = vscode.window.visibleTextEditors.find((foundEditor) =>
92+ URI.equal(foundEditor.document.uri.toString(), targetUri.toString()),
93+ );
94+ if (visible) {
95+ await vscode.window.showTextDocument(visible.document, {
96+ preview: false,
97+ preserveFocus: false,
98+ viewColumn: visible.viewColumn,
99+ });
100+ this.editor = vscode.window.activeTextEditor ?? visible;
101+ return;
102+ }
103+
104+ const doc = await vscode.workspace.openTextDocument(targetUri);
105+ const editor = await vscode.window.showTextDocument(doc, {
106+ preview: false,
107+ preserveFocus: false,
108+ });
109+ this.editor = editor;
110+ }
111+
79112 public get range(): vscode.Range {
80113 const startLine = Math.min(this.startLine, this.endLine);
81114 const endLine = Math.max(this.startLine, this.endLine);
@@ -296,6 +329,8 @@
296329 // Diff is messed up without this delay.
297330 await new Promise((resolve) => setTimeout(resolve, 100));
298331
332+ await this.ensureCurrentFileIsFocused();
333+
299334 // First, we reset the original diff by rejecting all pending diff blocks
300335 const blocks = this.editorToVerticalDiffCodeLens.get(this.fileUri) ?? [];
301336 for (const block of blocks.reverse()) {
@@ -443,6 +478,7 @@
443478 }
444479
445480 private async insertTextAboveLine(index: number, text: string) {
481+ await this.ensureCurrentFileIsFocused();
446482 await this.editor.edit(
447483 (editBuilder) => {
448484 const lineCount = this.editor.document.lineCount;
@@ -471,6 +507,7 @@
471507
472508 private async deleteLinesAt(index: number, numLines = 1) {
473509 const startLine = new vscode.Position(index, 0);
510+ await this.ensureCurrentFileIsFocused();
474511 await this.editor.edit(
475512 (editBuilder) => {
476513 editBuilder.delete(
@@ -482,6 +519,7 @@
482519 }
483520
484521 private async deleteRangeLines(ranges: vscode.Range[]) {
522+ await this.ensureCurrentFileIsFocused();
485523 await this.editor.edit(
486524 (editBuilder) => {
487525 for (const range of ranges) {
modified extensions/vscode/src/diff/vertical/manager.ts +1 −1
@@ -51,7 +51,7 @@
5151 this.fileUriToHandler.get(fileUri)?.clear(false);
5252 this.fileUriToHandler.delete(fileUri);
5353 }
54- const editor = vscode.window.activeTextEditor; // TODO might cause issues if user switches files
54+ const editor = vscode.window.activeTextEditor;
5555 if (editor && URI.equal(editor.document.uri.toString(), fileUri)) {
5656 const handler = new VerticalDiffHandler(
5757 startLine,