Skip to content

Commit 270dce5

Browse files
hassoncsdaniel-lxs
andauthored
fix(editor): prevent file editing issues when git diff views are open (#8676)
* 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. * Remove the warnings * fix(editor): enforce file:// scheme in editor lookups to prevent git diff issues --------- Co-authored-by: daniel-lxs <[email protected]>
1 parent 1a3a873 commit 270dce5

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/integrations/editor/DiffViewProvider.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export class DiffViewProvider {
5454
// If the file is already open, ensure it's not dirty before getting its
5555
// contents.
5656
if (fileExists) {
57-
const existingDocument = vscode.workspace.textDocuments.find((doc) =>
58-
arePathsEqual(doc.uri.fsPath, absolutePath),
57+
const existingDocument = vscode.workspace.textDocuments.find(
58+
(doc) => doc.uri.scheme === "file" && arePathsEqual(doc.uri.fsPath, absolutePath),
5959
)
6060

6161
if (existingDocument && existingDocument.isDirty) {
@@ -91,7 +91,10 @@ export class DiffViewProvider {
9191
.map((tg) => tg.tabs)
9292
.flat()
9393
.filter(
94-
(tab) => tab.input instanceof vscode.TabInputText && arePathsEqual(tab.input.uri.fsPath, absolutePath),
94+
(tab) =>
95+
tab.input instanceof vscode.TabInputText &&
96+
tab.input.uri.scheme === "file" &&
97+
arePathsEqual(tab.input.uri.fsPath, absolutePath),
9598
)
9699

97100
for (const tab of tabs) {
@@ -509,13 +512,14 @@ export class DiffViewProvider {
509512
// Listen for document open events - more efficient than scanning all tabs
510513
disposables.push(
511514
vscode.workspace.onDidOpenTextDocument(async (document) => {
512-
if (arePathsEqual(document.uri.fsPath, uri.fsPath)) {
515+
// Only match file:// scheme documents to avoid git diffs
516+
if (document.uri.scheme === "file" && arePathsEqual(document.uri.fsPath, uri.fsPath)) {
513517
// Wait a tick for the editor to be available
514518
await new Promise((r) => setTimeout(r, 0))
515519

516520
// Find the editor for this document
517-
const editor = vscode.window.visibleTextEditors.find((e) =>
518-
arePathsEqual(e.document.uri.fsPath, uri.fsPath),
521+
const editor = vscode.window.visibleTextEditors.find(
522+
(e) => e.document.uri.scheme === "file" && arePathsEqual(e.document.uri.fsPath, uri.fsPath),
519523
)
520524

521525
if (editor) {
@@ -529,7 +533,11 @@ export class DiffViewProvider {
529533
// Also listen for visible editor changes as a fallback
530534
disposables.push(
531535
vscode.window.onDidChangeVisibleTextEditors((editors) => {
532-
const editor = editors.find((e) => arePathsEqual(e.document.uri.fsPath, uri.fsPath))
536+
const editor = editors.find((e) => {
537+
const isFileScheme = e.document.uri.scheme === "file"
538+
const pathMatches = arePathsEqual(e.document.uri.fsPath, uri.fsPath)
539+
return isFileScheme && pathMatches
540+
})
533541
if (editor) {
534542
cleanup()
535543
resolve(editor)

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)