Skip to content

Commit 6cec1f3

Browse files
committed
fix(editor): prevent file editing issues when git diff views are open
Add scheme checks to ensure only file:// URIs are matched when finding editors, avoiding issues with git diffs and other schemes. Includes error logging for failed editor lookups.
1 parent 3a47c55 commit 6cec1f3

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/integrations/editor/DiffViewProvider.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,18 +509,21 @@ export class DiffViewProvider {
509509
// Listen for document open events - more efficient than scanning all tabs
510510
disposables.push(
511511
vscode.workspace.onDidOpenTextDocument(async (document) => {
512-
if (arePathsEqual(document.uri.fsPath, uri.fsPath)) {
512+
// Only match file:// scheme documents to avoid git diffs
513+
if (document.uri.scheme === "file" && arePathsEqual(document.uri.fsPath, uri.fsPath)) {
513514
// Wait a tick for the editor to be available
514515
await new Promise((r) => setTimeout(r, 0))
515516

516517
// Find the editor for this document
517-
const editor = vscode.window.visibleTextEditors.find((e) =>
518-
arePathsEqual(e.document.uri.fsPath, uri.fsPath),
518+
const editor = vscode.window.visibleTextEditors.find(
519+
(e) => e.document.uri.scheme === "file" && arePathsEqual(e.document.uri.fsPath, uri.fsPath),
519520
)
520521

521522
if (editor) {
522523
cleanup()
523524
resolve(editor)
525+
} else {
526+
console.error(`[DiffViewProvider] Failed to find valid editor for ${fileName}`)
524527
}
525528
}
526529
}),
@@ -529,10 +532,16 @@ export class DiffViewProvider {
529532
// Also listen for visible editor changes as a fallback
530533
disposables.push(
531534
vscode.window.onDidChangeVisibleTextEditors((editors) => {
532-
const editor = editors.find((e) => arePathsEqual(e.document.uri.fsPath, uri.fsPath))
535+
const editor = editors.find((e) => {
536+
const isFileScheme = e.document.uri.scheme === "file"
537+
const pathMatches = arePathsEqual(e.document.uri.fsPath, uri.fsPath)
538+
return isFileScheme && pathMatches
539+
})
533540
if (editor) {
534541
cleanup()
535542
resolve(editor)
543+
} else {
544+
console.error(`[DiffViewProvider] Failed to find valid editor for ${fileName}`)
536545
}
537546
}),
538547
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe("DiffViewProvider", () => {
187187
// Setup
188188
const mockEditor = {
189189
document: {
190-
uri: { fsPath: `${mockCwd}/test.md` },
190+
uri: { fsPath: `${mockCwd}/test.md`, scheme: "file" },
191191
getText: vi.fn().mockReturnValue(""),
192192
lineCount: 0,
193193
},
@@ -220,7 +220,7 @@ describe("DiffViewProvider", () => {
220220
vi.mocked(vscode.workspace.onDidOpenTextDocument).mockImplementation((callback) => {
221221
// Trigger the callback immediately with the document
222222
setTimeout(() => {
223-
callback({ uri: { fsPath: `${mockCwd}/test.md` } } as any)
223+
callback({ uri: { fsPath: `${mockCwd}/test.md`, scheme: "file" } } as any)
224224
}, 0)
225225
return { dispose: vi.fn() }
226226
})

0 commit comments

Comments
 (0)