Skip to content

Commit 011e5cf

Browse files
committed
storeDiffContent,: Cached content remained in memory
- Without accepting/rejecting, in storeDiffContent, the cached before/after content remained in memory for the entire session. 1. Add document close listener in FcoTextDocumentContentProvider: - Listen for vscode.workspace.onDidCloseTextDocument events - Filter for fco-diff scheme URIs - Extract file path from closed URIs and call cleanupFile() 2. Add tab close listener as backup: - Listen for vscode.window.tabGroups.onDidChangeTabs events - Detect when FCO diff tabs are closed - Clean up associated content 3. Update provider registration: - Register event listeners when provider is created - Properly dispose listeners when provider is disposed 4. Add helper method: - extractFilePathFromUri() to reverse-engineer file paths from hash-based URIs - Handle edge cases where URI-to-file mapping is lost
1 parent a9ab507 commit 011e5cf

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ export async function deactivate() {
364364
await bridge.disconnect()
365365
}
366366

367+
// Dispose FCO content provider to clean up event listeners
368+
const fcoContentProvider = FcoTextDocumentContentProvider.getInstance()
369+
fcoContentProvider.dispose()
370+
367371
await McpServerManager.cleanup(extensionContext)
368372
TelemetryService.instance.shutdown()
369373
TerminalRegistry.cleanup()

src/services/files-changed/FcoTextDocumentContentProvider.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export class FcoTextDocumentContentProvider implements vscode.TextDocumentConten
88
private contentStore = new Map<string, string>()
99
private fileToUriMapping = new Map<string, { beforeUri: string; afterUri: string }>()
1010
private static instance: FcoTextDocumentContentProvider
11+
private disposables: vscode.Disposable[] = []
12+
13+
constructor() {
14+
this.setupEventListeners()
15+
}
1116

1217
static getInstance(): FcoTextDocumentContentProvider {
1318
if (!this.instance) {
@@ -16,6 +21,67 @@ export class FcoTextDocumentContentProvider implements vscode.TextDocumentConten
1621
return this.instance
1722
}
1823

24+
/**
25+
* Set up event listeners for automatic cleanup when diff documents are closed
26+
*/
27+
private setupEventListeners(): void {
28+
// Listen for document close events to automatically clean up fco-diff content
29+
const onDocumentClose = vscode.workspace.onDidCloseTextDocument((document) => {
30+
if (document.uri.scheme === "fco-diff") {
31+
const filePath = this.extractFilePathFromUri(document.uri.toString())
32+
if (filePath) {
33+
this.cleanupFile(filePath)
34+
}
35+
}
36+
})
37+
this.disposables.push(onDocumentClose)
38+
39+
// Listen for tab close events as backup cleanup method
40+
const onTabChange = vscode.window.tabGroups.onDidChangeTabs((event) => {
41+
for (const tab of event.closed) {
42+
if (tab.input instanceof vscode.TabInputTextDiff) {
43+
const originalUri = tab.input.original
44+
const modifiedUri = tab.input.modified
45+
46+
// Check if either URI is an fco-diff scheme
47+
if (originalUri?.scheme === "fco-diff" || modifiedUri?.scheme === "fco-diff") {
48+
// Try to extract file path from the tab label or URI
49+
const filePath = this.extractFilePathFromTabInput(tab.input)
50+
if (filePath) {
51+
this.cleanupFile(filePath)
52+
}
53+
}
54+
}
55+
}
56+
})
57+
this.disposables.push(onTabChange)
58+
}
59+
60+
/**
61+
* Extract the original file path from an fco-diff URI
62+
* Uses reverse lookup through fileToUriMapping to find the associated file path
63+
*/
64+
private extractFilePathFromUri(uriString: string): string | undefined {
65+
// Direct lookup - find file path that maps to this URI
66+
for (const [filePath, uris] of this.fileToUriMapping.entries()) {
67+
if (uris.beforeUri === uriString || uris.afterUri === uriString) {
68+
return filePath
69+
}
70+
}
71+
return undefined
72+
}
73+
74+
/**
75+
* Extract file path from tab input for cleanup purposes
76+
*/
77+
private extractFilePathFromTabInput(tabInput: vscode.TabInputTextDiff): string | undefined {
78+
const originalUri = tabInput.original?.toString()
79+
const modifiedUri = tabInput.modified?.toString()
80+
81+
// Try to extract from either URI
82+
return this.extractFilePathFromUri(originalUri || "") || this.extractFilePathFromUri(modifiedUri || "")
83+
}
84+
1985
/**
2086
* Provides text document content for FCO diff URIs.
2187
* Called by VS Code when it needs the actual content for a URI.
@@ -129,4 +195,13 @@ export class FcoTextDocumentContentProvider implements vscode.TextDocumentConten
129195
this.contentStore.clear()
130196
this.fileToUriMapping.clear()
131197
}
198+
199+
/**
200+
* Dispose of event listeners and clean up resources
201+
*/
202+
dispose(): void {
203+
this.disposables.forEach((disposable) => disposable.dispose())
204+
this.disposables = []
205+
this.clearAll()
206+
}
132207
}

0 commit comments

Comments
 (0)