Skip to content

Commit 491206c

Browse files
committed
feat(diffViewProvider): check for diff view in onDidOpenTextDocument
1 parent 89f9b57 commit 491206c

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/integrations/editor/DiffViewProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ export class DiffViewProvider {
467467
// Listen for document open events - more efficient than scanning all tabs
468468
disposables.push(
469469
vscode.workspace.onDidOpenTextDocument(async (document) => {
470-
if (arePathsEqual(document.uri.fsPath, uri.fsPath)) {
470+
if (document.uri.scheme == DIFF_VIEW_URI_SCHEME && uri.fsPath.endsWith(document.fileName)) {
471471
// Wait a tick for the editor to be available
472472
await new Promise((r) => setTimeout(r, 0))
473473

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe("DiffViewProvider", () => {
103103
;(diffViewProvider as any).relPath = "test.txt"
104104
;(diffViewProvider as any).activeDiffEditor = {
105105
document: {
106-
uri: { fsPath: `${mockCwd}/test.txt` },
106+
uri: { fsPath: `${mockCwd}/test.txt`, scheme: DIFF_VIEW_URI_SCHEME },
107107
getText: vi.fn(),
108108
lineCount: 10,
109109
},
@@ -158,7 +158,8 @@ describe("DiffViewProvider", () => {
158158
// Setup
159159
const mockEditor = {
160160
document: {
161-
uri: { fsPath: `${mockCwd}/test.md` },
161+
uri: { fsPath: `${mockCwd}/test.md`, scheme: DIFF_VIEW_URI_SCHEME },
162+
fileName: `test.md`,
162163
getText: vi.fn().mockReturnValue(""),
163164
lineCount: 0,
164165
},
@@ -191,7 +192,7 @@ describe("DiffViewProvider", () => {
191192
vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => {
192193
// Trigger the callback immediately with the document
193194
setTimeout(() => {
194-
callback({ uri: { fsPath: `${mockCwd}/test.md` } } as any)
195+
callback(mockEditor.document as any)
195196
}, 0)
196197
return { dispose: vi.fn() }
197198
})
@@ -206,27 +207,21 @@ describe("DiffViewProvider", () => {
206207
await diffViewProvider.open("test.md")
207208

208209
// Verify that showTextDocument was called before executeCommand
209-
expect(callOrder).toEqual(["showTextDocument", "executeCommand"])
210-
211-
// Verify that showTextDocument was called with preview: false and preserveFocus: true
212-
expect(vscode.window.showTextDocument).toHaveBeenCalledWith(
213-
expect.objectContaining({ fsPath: `${mockCwd}/test.md` }),
214-
{ preview: false, viewColumn: vscode.ViewColumn.Active, preserveFocus: true },
215-
)
210+
expect(callOrder).toEqual(["executeCommand"])
216211

217212
// Verify that the diff command was executed
218213
expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
219214
"vscode.diff",
220215
expect.any(Object),
221216
expect.any(Object),
222217
`test.md: ${DIFF_VIEW_LABEL_CHANGES} (Editable)`,
223-
{ preserveFocus: true },
218+
{ preview: false, preserveFocus: true, viewColumn: vscode.ViewColumn.Beside },
224219
)
225220
})
226221

227222
it("should handle showTextDocument failure", async () => {
228223
// Mock showTextDocument to fail
229-
vi.mocked(vscode.window.showTextDocument).mockRejectedValue(new Error("Cannot open file"))
224+
vi.mocked(vscode.commands.executeCommand).mockRejectedValue(new Error("Cannot open file"))
230225

231226
// Mock workspace.onDidOpenTextDocument
232227
vi.mocked(vscode.workspace.onDidOpenTextDocument).mockReturnValue({ dispose: vi.fn() })

0 commit comments

Comments
 (0)