Skip to content

Commit beb8d6b

Browse files
committed
we only need to consider if the editor document is the event document
1 parent b566965 commit beb8d6b

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/services/ghost/GhostProvider.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,17 @@ export class GhostProvider {
222222

223223
// Heuristic to filter out changes far from cursor (likely external or LLM edits)
224224
const editor = vscode.window.activeTextEditor
225-
if (editor && editor.document === event.document) {
226-
const cursorPos = editor.selection.active
227-
const isNearCursor = event.contentChanges.some((change) => {
228-
const distance = Math.abs(cursorPos.line - change.range.start.line)
229-
return distance <= 2
230-
})
231-
if (!isNearCursor) {
232-
return
233-
}
225+
if (!editor || editor.document !== event.document) {
226+
return
227+
}
228+
229+
const cursorPos = editor.selection.active
230+
const isNearCursor = event.contentChanges.some((change) => {
231+
const distance = Math.abs(cursorPos.line - change.range.start.line)
232+
return distance <= 2
233+
})
234+
if (!isNearCursor) {
235+
return
234236
}
235237

236238
await this.documentStore.storeDocument({ document: event.document })

src/services/ghost/__tests__/GhostProvider.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,43 @@ console.log('test');]]></replace></change>`
434434
expect(isNearCursor).toBe(true)
435435
})
436436

437+
it("should filter out changes to non-active documents", async () => {
438+
const initialContent = `console.log('test');`
439+
const { mockDocument } = await setupTestDocument("inactive.js", initialContent)
440+
441+
// Create a different document for the active editor
442+
const activeContent = `console.log('active');`
443+
const activeUri = vscode.Uri.parse("file://active.js")
444+
mockWorkspace.addDocument(activeUri, activeContent)
445+
const activeDocument = await mockWorkspace.openTextDocument(activeUri)
446+
447+
// Mock active editor with a different document
448+
;(vscode.window as any).activeTextEditor = {
449+
document: activeDocument,
450+
selection: {
451+
active: new vscode.Position(0, 10),
452+
},
453+
}
454+
455+
// Simulate change to the non-active document
456+
const event = {
457+
document: mockDocument,
458+
contentChanges: [
459+
{
460+
range: new vscode.Range(new vscode.Position(0, 10), new vscode.Position(0, 10)),
461+
rangeLength: 0,
462+
text: "a",
463+
},
464+
],
465+
reason: undefined,
466+
}
467+
468+
// Should be filtered out - document doesn't match active editor
469+
const editor = (vscode.window as any).activeTextEditor
470+
const shouldProcess = editor && editor.document === event.document
471+
expect(shouldProcess).toBe(false)
472+
})
473+
437474
it("should allow small paste operations near cursor", async () => {
438475
const initialContent = `console.log('test');`
439476
const { mockDocument } = await setupTestDocument("paste.js", initialContent)

0 commit comments

Comments
 (0)