Changesets · Branches · Tags · Compare
| Changeset | 154c34fa97990a193706a659b58cc60339a2e084f63517dcbf4a4265ebf670b9 |
|---|---|
| Author | Aditya Mitra <61635505+uinstinct@users.noreply.github.com> · 2025-09-17 04:34 +05:30 |
| Committer | GitHub <noreply@github.com> · 2025-09-16 16:04 -07:00 |
| Parents | 5f1e5b279846 |
2 files changed
modified extensions/vscode/src/diff/vertical/handler.ts +38 −0
| @@ -76,6 +76,39 @@ | ||
| 76 | 76 | this.disposables.push(disposable); |
| 77 | 77 | } |
| 78 | 78 | |
| 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 | + | |
| 79 | 112 | public get range(): vscode.Range { |
| 80 | 113 | const startLine = Math.min(this.startLine, this.endLine); |
| 81 | 114 | const endLine = Math.max(this.startLine, this.endLine); |
| @@ -296,6 +329,8 @@ | ||
| 296 | 329 | // Diff is messed up without this delay. |
| 297 | 330 | await new Promise((resolve) => setTimeout(resolve, 100)); |
| 298 | 331 | |
| 332 | + await this.ensureCurrentFileIsFocused(); | |
| 333 | + | |
| 299 | 334 | // First, we reset the original diff by rejecting all pending diff blocks |
| 300 | 335 | const blocks = this.editorToVerticalDiffCodeLens.get(this.fileUri) ?? []; |
| 301 | 336 | for (const block of blocks.reverse()) { |
| @@ -443,6 +478,7 @@ | ||
| 443 | 478 | } |
| 444 | 479 | |
| 445 | 480 | private async insertTextAboveLine(index: number, text: string) { |
| 481 | + await this.ensureCurrentFileIsFocused(); | |
| 446 | 482 | await this.editor.edit( |
| 447 | 483 | (editBuilder) => { |
| 448 | 484 | const lineCount = this.editor.document.lineCount; |
| @@ -471,6 +507,7 @@ | ||
| 471 | 507 | |
| 472 | 508 | private async deleteLinesAt(index: number, numLines = 1) { |
| 473 | 509 | const startLine = new vscode.Position(index, 0); |
| 510 | + await this.ensureCurrentFileIsFocused(); | |
| 474 | 511 | await this.editor.edit( |
| 475 | 512 | (editBuilder) => { |
| 476 | 513 | editBuilder.delete( |
| @@ -482,6 +519,7 @@ | ||
| 482 | 519 | } |
| 483 | 520 | |
| 484 | 521 | private async deleteRangeLines(ranges: vscode.Range[]) { |
| 522 | + await this.ensureCurrentFileIsFocused(); | |
| 485 | 523 | await this.editor.edit( |
| 486 | 524 | (editBuilder) => { |
| 487 | 525 | for (const range of ranges) { |
modified extensions/vscode/src/diff/vertical/manager.ts +1 −1
| @@ -51,7 +51,7 @@ | ||
| 51 | 51 | this.fileUriToHandler.get(fileUri)?.clear(false); |
| 52 | 52 | this.fileUriToHandler.delete(fileUri); |
| 53 | 53 | } |
| 54 | - const editor = vscode.window.activeTextEditor; // TODO might cause issues if user switches files | |
| 54 | + const editor = vscode.window.activeTextEditor; | |
| 55 | 55 | if (editor && URI.equal(editor.document.uri.toString(), fileUri)) { |
| 56 | 56 | const handler = new VerticalDiffHandler( |
| 57 | 57 | startLine, |