Skip to content

Commit 2321243

Browse files
committed
fix(4385): re-apply changes from PR 4385
1 parent 39d6535 commit 2321243

File tree

1 file changed

+68
-30
lines changed

1 file changed

+68
-30
lines changed

src/integrations/editor/DiffViewProvider.ts

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,7 @@ export class DiffViewProvider {
349349
// Remove only the directories we created, in reverse order.
350350
for (let i = this.createdDirs.length - 1; i >= 0; i--) {
351351
await fs.rmdir(this.createdDirs[i])
352-
console.log(`Directory ${this.createdDirs[i]} has been deleted.`)
353352
}
354-
355-
console.log(`File ${absolutePath} has been deleted.`)
356353
} else {
357354
// Revert document.
358355
const edit = new vscode.WorkspaceEdit()
@@ -369,7 +366,6 @@ export class DiffViewProvider {
369366
// changes and saved during the edit.
370367
await vscode.workspace.applyEdit(edit)
371368
await updatedDocument.save()
372-
console.log(`File ${absolutePath} has been reverted to its original content.`)
373369

374370
if (this.documentWasOpen) {
375371
await vscode.window.showTextDocument(vscode.Uri.file(absolutePath), {
@@ -397,9 +393,7 @@ export class DiffViewProvider {
397393
.map((tab) =>
398394
vscode.window.tabGroups.close(tab).then(
399395
() => undefined,
400-
(err) => {
401-
console.error(`Failed to close diff tab ${tab.label}`, err)
402-
},
396+
(err) => {},
403397
),
404398
)
405399

@@ -432,31 +426,75 @@ export class DiffViewProvider {
432426

433427
// Open new diff editor.
434428
return new Promise<vscode.TextEditor>((resolve, reject) => {
435-
const fileName = path.basename(uri.fsPath)
436-
const fileExists = this.editType === "modify"
437-
438-
const disposable = vscode.window.onDidChangeActiveTextEditor((editor) => {
439-
if (editor && arePathsEqual(editor.document.uri.fsPath, uri.fsPath)) {
440-
disposable.dispose()
441-
resolve(editor)
429+
;(async () => {
430+
const fileName = path.basename(uri.fsPath)
431+
const fileExists = this.editType === "modify"
432+
let timeoutId: NodeJS.Timeout | undefined
433+
434+
const checkAndResolve = () => {
435+
for (const group of vscode.window.tabGroups.all) {
436+
for (const tab of group.tabs) {
437+
if (
438+
tab.input instanceof vscode.TabInputTextDiff &&
439+
tab.input?.original?.scheme === DIFF_VIEW_URI_SCHEME &&
440+
arePathsEqual(tab.input.modified.fsPath, uri.fsPath)
441+
) {
442+
// Found the diff editor, now try to show it to get the TextEditor instance
443+
vscode.window.showTextDocument(tab.input.modified, { preserveFocus: true }).then(
444+
(editor) => {
445+
if (timeoutId) clearTimeout(timeoutId)
446+
disposableTabGroup.dispose()
447+
resolve(editor)
448+
},
449+
(err) => {
450+
if (timeoutId) clearTimeout(timeoutId)
451+
disposableTabGroup.dispose()
452+
reject(
453+
new Error(`Failed to show diff editor after finding tab: ${err.message}`),
454+
)
455+
},
456+
)
457+
return true
458+
}
459+
}
460+
}
461+
return false
442462
}
443-
})
444-
445-
vscode.commands.executeCommand(
446-
"vscode.diff",
447-
vscode.Uri.parse(`${DIFF_VIEW_URI_SCHEME}:${fileName}`).with({
448-
query: Buffer.from(this.originalContent ?? "").toString("base64"),
449-
}),
450-
uri,
451-
`${fileName}: ${fileExists ? "Original ↔ Roo's Changes" : "New File"} (Editable)`,
452-
{ preserveFocus: true },
453-
)
454463

455-
// This may happen on very slow machines i.e. project idx.
456-
setTimeout(() => {
457-
disposable.dispose()
458-
reject(new Error("Failed to open diff editor, please try again..."))
459-
}, 10_000)
464+
// Listen for changes in tab groups, which includes tabs moving between windows
465+
const disposableTabGroup = vscode.window.tabGroups.onDidChangeTabGroups(() => {
466+
checkAndResolve()
467+
})
468+
469+
vscode.commands
470+
.executeCommand(
471+
"vscode.diff",
472+
vscode.Uri.parse(`${DIFF_VIEW_URI_SCHEME}:${fileName}`).with({
473+
query: Buffer.from(this.originalContent ?? "").toString("base64"),
474+
}),
475+
uri,
476+
`${fileName}: ${fileExists ? "Original ↔ Roo's Changes" : "New File"} (Editable)`,
477+
{ preserveFocus: true },
478+
)
479+
.then(
480+
async () => {
481+
// Give a brief moment for the editor to appear in tab groups
482+
await new Promise((r) => setTimeout(r, 100))
483+
if (!checkAndResolve()) {
484+
}
485+
},
486+
(err) => {
487+
if (timeoutId) clearTimeout(timeoutId)
488+
disposableTabGroup.dispose()
489+
reject(new Error(`Failed to open diff editor command: ${err.message}`))
490+
},
491+
)
492+
493+
timeoutId = setTimeout(() => {
494+
disposableTabGroup.dispose()
495+
reject(new Error("Failed to open diff editor, please try again..."))
496+
}, 10_000)
497+
})()
460498
})
461499
}
462500

0 commit comments

Comments
 (0)