|
| 1 | +import * as vscode from "vscode" |
| 2 | + |
| 3 | +/** |
| 4 | + * Dedicated TextDocumentContentProvider for Files Changed Overview (FCO) diff viewing. |
| 5 | + * Eliminates base64 encoding in query strings by storing content in memory and serving it on-demand. |
| 6 | + */ |
| 7 | +export class FcoTextDocumentContentProvider implements vscode.TextDocumentContentProvider { |
| 8 | + private contentStore = new Map<string, string>() |
| 9 | + private fileToUriMapping = new Map<string, { beforeUri: string; afterUri: string }>() |
| 10 | + private static instance: FcoTextDocumentContentProvider |
| 11 | + |
| 12 | + static getInstance(): FcoTextDocumentContentProvider { |
| 13 | + if (!this.instance) { |
| 14 | + this.instance = new FcoTextDocumentContentProvider() |
| 15 | + } |
| 16 | + return this.instance |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Provides text document content for FCO diff URIs. |
| 21 | + * Called by VS Code when it needs the actual content for a URI. |
| 22 | + */ |
| 23 | + provideTextDocumentContent(uri: vscode.Uri): string { |
| 24 | + const content = this.contentStore.get(uri.path) |
| 25 | + if (!content) { |
| 26 | + console.warn(`FcoTextDocumentContentProvider: No content found for URI path: ${uri.path}`) |
| 27 | + return "" |
| 28 | + } |
| 29 | + return content |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Stores before/after content for a diff session and returns clean URIs. |
| 34 | + * Uses content-based stable IDs to prevent duplicate diffs for same content. |
| 35 | + * No base64 encoding - content is stored in memory and served on-demand. |
| 36 | + */ |
| 37 | + storeDiffContent( |
| 38 | + beforeContent: string, |
| 39 | + afterContent: string, |
| 40 | + filePath?: string, |
| 41 | + ): { beforeUri: string; afterUri: string } { |
| 42 | + // Create stable ID based on file path and content hash to prevent duplicates |
| 43 | + const contentHash = this.hashContent(beforeContent + afterContent + (filePath || "")) |
| 44 | + const beforeKey = `before-${contentHash}` |
| 45 | + const afterKey = `after-${contentHash}` |
| 46 | + |
| 47 | + // Check if already exists - reuse existing URIs to prevent duplicate diffs |
| 48 | + if (this.contentStore.has(beforeKey)) { |
| 49 | + const beforeUri = `fco-diff:${beforeKey}` |
| 50 | + const afterUri = `fco-diff:${afterKey}` |
| 51 | + |
| 52 | + // Update file mapping in case filePath changed |
| 53 | + if (filePath) { |
| 54 | + this.fileToUriMapping.set(filePath, { beforeUri, afterUri }) |
| 55 | + } |
| 56 | + |
| 57 | + return { beforeUri, afterUri } |
| 58 | + } |
| 59 | + |
| 60 | + // Store new content in memory |
| 61 | + this.contentStore.set(beforeKey, beforeContent) |
| 62 | + this.contentStore.set(afterKey, afterContent) |
| 63 | + |
| 64 | + // Return clean URIs without any base64 content |
| 65 | + const beforeUri = `fco-diff:${beforeKey}` |
| 66 | + const afterUri = `fco-diff:${afterKey}` |
| 67 | + |
| 68 | + // Track file path to URI mapping for cleanup |
| 69 | + if (filePath) { |
| 70 | + this.fileToUriMapping.set(filePath, { beforeUri, afterUri }) |
| 71 | + } |
| 72 | + |
| 73 | + return { beforeUri, afterUri } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get URIs for a specific file path (for diff tab management) |
| 78 | + */ |
| 79 | + getUrisForFile(filePath: string): { beforeUri: string; afterUri: string } | undefined { |
| 80 | + return this.fileToUriMapping.get(filePath) |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Clean up all content associated with a specific file path |
| 85 | + */ |
| 86 | + cleanupFile(filePath: string): void { |
| 87 | + const uris = this.fileToUriMapping.get(filePath) |
| 88 | + if (uris) { |
| 89 | + this.cleanup([uris.beforeUri, uris.afterUri]) |
| 90 | + this.fileToUriMapping.delete(filePath) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Cleanup stored content to prevent memory leaks. |
| 96 | + * Should be called when diff tabs are closed. |
| 97 | + */ |
| 98 | + cleanup(uris: string[]): void { |
| 99 | + uris.forEach((uri) => { |
| 100 | + const key = uri.replace("fco-diff:", "") |
| 101 | + if (this.contentStore.delete(key)) { |
| 102 | + console.debug(`FcoTextDocumentContentProvider: Cleaned up content for ${key}`) |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Create a stable hash from content for consistent IDs |
| 109 | + */ |
| 110 | + private hashContent(content: string): string { |
| 111 | + // Simple hash for stable IDs - ensures same content gets same ID |
| 112 | + let hash = 0 |
| 113 | + for (let i = 0; i < content.length; i++) { |
| 114 | + const char = content.charCodeAt(i) |
| 115 | + hash = (hash << 5) - hash + char |
| 116 | + hash = hash & hash // Convert to 32-bit integer |
| 117 | + } |
| 118 | + return Math.abs(hash).toString(36) |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get total number of stored content items (for debugging/monitoring) |
| 123 | + */ |
| 124 | + getStoredContentCount(): number { |
| 125 | + return this.contentStore.size |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Clear all stored content (for testing or cleanup) |
| 130 | + */ |
| 131 | + clearAll(): void { |
| 132 | + this.contentStore.clear() |
| 133 | + this.fileToUriMapping.clear() |
| 134 | + } |
| 135 | +} |
0 commit comments