Skip to content

Commit 6ea564c

Browse files
committed
fix(diff-view): restore preview tabs to their original editor group
1 parent c51054c commit 6ea564c

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class DiffViewProvider {
5050
// diff-open time. Opening the diff reuses the editor group's single preview
5151
// slot and evicts these tabs; we restore any that disappeared after the diff
5252
// session ends so the user's prior tab state is reconstructed.
53-
private snapshotPreviewTabs: Array<{ uri: vscode.Uri; scrollLine: number | undefined }> = []
53+
private snapshotPreviewTabs: Array<{ uri: vscode.Uri; scrollLine: number | undefined; viewColumn: vscode.ViewColumn }> = []
5454
private taskRef: WeakRef<Task>
5555

5656
constructor(
@@ -608,13 +608,13 @@ export class DiffViewProvider {
608608
}
609609

610610
// Capture unrelated preview tabs (italicized, not-yet-edited) along with their
611-
// current scroll position. Opening the diff reuses the editor group's single
612-
// preview slot and evicts these tabs; the snapshot lets us restore them after
613-
// the diff session ends. The diff target is excluded since it is about to be
614-
// replaced by the diff view anyway.
611+
// current scroll position and editor group. Opening the diff reuses the group's
612+
// single preview slot and evicts these tabs; the snapshot lets us restore them
613+
// in the correct group after the diff session ends. The diff target is excluded
614+
// since it is about to be replaced by the diff view anyway.
615615
private captureUnrelatedPreviewTabs(
616616
diffTargetPath: string,
617-
): Array<{ uri: vscode.Uri; scrollLine: number | undefined }> {
617+
): Array<{ uri: vscode.Uri; scrollLine: number | undefined; viewColumn: vscode.ViewColumn }> {
618618
return vscode.window.tabGroups.all.flatMap((group) =>
619619
group.tabs
620620
.filter(
@@ -629,7 +629,7 @@ export class DiffViewProvider {
629629
const visibleEditor = vscode.window.visibleTextEditors.find(
630630
(e) => e.document.uri.scheme === "file" && arePathsEqual(e.document.uri.fsPath, uri.fsPath),
631631
)
632-
return { uri, scrollLine: visibleEditor?.visibleRanges?.[0]?.start.line }
632+
return { uri, scrollLine: visibleEditor?.visibleRanges?.[0]?.start.line, viewColumn: group.viewColumn }
633633
}),
634634
)
635635
}
@@ -665,6 +665,7 @@ export class DiffViewProvider {
665665
const editor = await vscode.window.showTextDocument(snapshot.uri, {
666666
preview: true,
667667
preserveFocus: true,
668+
viewColumn: snapshot.viewColumn,
668669
})
669670
if (snapshot.scrollLine !== undefined) {
670671
editor.revealRange(

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -512,19 +512,22 @@ describe("DiffViewProvider", () => {
512512
return { isPreview, input, label: fsPath }
513513
}
514514

515-
const setTabs = (tabs: any[]) => {
515+
const setTabs = (tabs: any[], viewColumn = vscode.ViewColumn.One) => {
516516
Object.defineProperty(vscode.window.tabGroups, "all", {
517-
get: () => [{ tabs }],
517+
get: () => [{ tabs, viewColumn }],
518518
configurable: true,
519519
})
520520
}
521521

522-
it("captures unrelated preview tabs with their scroll position, excluding the diff target", () => {
523-
setTabs([
524-
makePreviewTab("/mock/cwd/file-1.txt"),
525-
makePreviewTab("/mock/cwd/file-2.txt"), // diff target -- excluded
526-
makePreviewTab("/mock/cwd/file-3.txt", false), // not a preview -- excluded
527-
])
522+
it("captures unrelated preview tabs with their scroll position and group, excluding the diff target", () => {
523+
setTabs(
524+
[
525+
makePreviewTab("/mock/cwd/file-1.txt"),
526+
makePreviewTab("/mock/cwd/file-2.txt"), // diff target -- excluded
527+
makePreviewTab("/mock/cwd/file-3.txt", false), // not a preview -- excluded
528+
],
529+
vscode.ViewColumn.Two,
530+
)
528531
vi.mocked(vscode.window).visibleTextEditors = [
529532
{
530533
document: { uri: { fsPath: "/mock/cwd/file-1.txt", scheme: "file" } },
@@ -537,22 +540,23 @@ describe("DiffViewProvider", () => {
537540
expect(snapshot).toHaveLength(1)
538541
expect(snapshot[0].uri.fsPath).toBe("/mock/cwd/file-1.txt")
539542
expect(snapshot[0].scrollLine).toBe(12)
543+
expect(snapshot[0].viewColumn).toBe(vscode.ViewColumn.Two)
540544
})
541545

542-
it("restores an evicted preview tab in preview mode and reapplies its scroll position", async () => {
546+
it("restores an evicted preview tab in its original group and reapplies its scroll position", async () => {
543547
const revealRange = vi.fn()
544548
vi.mocked(vscode.window.showTextDocument).mockResolvedValue({ revealRange } as any)
545549
// The captured file is no longer open (evicted by the diff).
546550
setTabs([])
547551
;(diffViewProvider as any).snapshotPreviewTabs = [
548-
{ uri: { fsPath: "/mock/cwd/file-1.txt", scheme: "file" }, scrollLine: 12 },
552+
{ uri: { fsPath: "/mock/cwd/file-1.txt", scheme: "file" }, scrollLine: 12, viewColumn: vscode.ViewColumn.Two },
549553
]
550554

551555
await (diffViewProvider as any).restorePreviewTabs()
552556

553557
expect(vscode.window.showTextDocument).toHaveBeenCalledWith(
554558
{ fsPath: "/mock/cwd/file-1.txt", scheme: "file" },
555-
{ preview: true, preserveFocus: true },
559+
{ preview: true, preserveFocus: true, viewColumn: vscode.ViewColumn.Two },
556560
)
557561
expect(revealRange).toHaveBeenCalledWith(
558562
expect.objectContaining({ start: { line: 12, character: 0 } }),
@@ -564,7 +568,7 @@ describe("DiffViewProvider", () => {
564568
it("does not restore a preview tab that is still open", async () => {
565569
setTabs([makePreviewTab("/mock/cwd/file-1.txt")])
566570
;(diffViewProvider as any).snapshotPreviewTabs = [
567-
{ uri: { fsPath: "/mock/cwd/file-1.txt", scheme: "file" }, scrollLine: 0 },
571+
{ uri: { fsPath: "/mock/cwd/file-1.txt", scheme: "file" }, scrollLine: 0, viewColumn: vscode.ViewColumn.One },
568572
]
569573

570574
await (diffViewProvider as any).restorePreviewTabs()
@@ -577,7 +581,7 @@ describe("DiffViewProvider", () => {
577581
const fs = await import("fs/promises")
578582
vi.mocked(fs.access).mockRejectedValueOnce(new Error("ENOENT"))
579583
;(diffViewProvider as any).snapshotPreviewTabs = [
580-
{ uri: { fsPath: "/mock/cwd/deleted.txt", scheme: "file" }, scrollLine: 0 },
584+
{ uri: { fsPath: "/mock/cwd/deleted.txt", scheme: "file" }, scrollLine: 0, viewColumn: vscode.ViewColumn.One },
581585
]
582586

583587
await (diffViewProvider as any).restorePreviewTabs()

0 commit comments

Comments
 (0)