Skip to content

Commit 0a8547f

Browse files
committed
Cleanup for FilesChangedOverview Diffs.
- Diff cleanup added. - Listener for users closing diff window added to clean up files. - added cleanup in extension.ts - test modified for new listener added
1 parent bbbb389 commit 0a8547f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/__tests__/extension.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ vi.mock("vscode", () => ({
2727
dispose: vi.fn(),
2828
}),
2929
onDidChangeWorkspaceFolders: vi.fn(),
30+
onDidCloseTextDocument: vi.fn().mockReturnValue({
31+
dispose: vi.fn(),
32+
}),
3033
},
3134
languages: {
3235
registerCodeActionsProvider: vi.fn(),

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ export async function activate(context: vscode.ExtensionContext) {
258258
const fcoContentProvider = FcoTextDocumentContentProvider.getInstance()
259259
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider("fco-diff", fcoContentProvider))
260260

261+
// Register automatic cleanup listener for when diff documents are closed
262+
context.subscriptions.push(fcoContentProvider.registerCloseListener())
263+
261264
context.subscriptions.push(vscode.window.registerUriHandler({ handleUri }))
262265

263266
// Register code actions provider.

src/services/files-changed/FcoTextDocumentContentProvider.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,40 @@ export class FcoTextDocumentContentProvider implements vscode.TextDocumentConten
129129
this.contentStore.clear()
130130
this.fileToUriMapping.clear()
131131
}
132+
133+
/**
134+
* Register a listener to automatically clean up content when diff documents are closed.
135+
* Should be called during extension activation.
136+
*/
137+
registerCloseListener(): vscode.Disposable {
138+
return vscode.workspace.onDidCloseTextDocument((document) => {
139+
// Only handle fco-diff scheme documents
140+
if (document.uri.scheme === "fco-diff") {
141+
this.cleanupByUri(document.uri.toString())
142+
}
143+
})
144+
}
145+
146+
/**
147+
* Clean up content for a specific URI.
148+
* Called when a diff document is closed to prevent memory leaks.
149+
*/
150+
private cleanupByUri(uriString: string): void {
151+
const key = uriString.replace("fco-diff:", "")
152+
this.contentStore.delete(key)
153+
154+
// Also clean up any file mappings that reference this URI
155+
for (const [filePath, uris] of this.fileToUriMapping.entries()) {
156+
if (uris.beforeUri === uriString || uris.afterUri === uriString) {
157+
// If both before and after URIs are being removed, delete the mapping
158+
const beforeKey = uris.beforeUri.replace("fco-diff:", "")
159+
const afterKey = uris.afterUri.replace("fco-diff:", "")
160+
161+
if (!this.contentStore.has(beforeKey) && !this.contentStore.has(afterKey)) {
162+
this.fileToUriMapping.delete(filePath)
163+
}
164+
break
165+
}
166+
}
167+
}
132168
}

0 commit comments

Comments
 (0)