From 9af8570264e796e62f3d0a457d9f3742274f3f05 Mon Sep 17 00:00:00 2001 From: Ives van Hoorne Date: Tue, 7 Nov 2023 18:43:26 +0100 Subject: [PATCH] feat: add support for custom fs provider We're looking to add our own providers for `user` and `cache` fs so they can be persisted between sessions. With this change, we don't break the existing API but we can allow for adding custom filesystems for these providers. --- src/service-override/files.ts | 45 ++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/service-override/files.ts b/src/service-override/files.ts index eb89bdd3..2b91b517 100644 --- a/src/service-override/files.ts +++ b/src/service-override/files.ts @@ -457,30 +457,42 @@ class DelegateFileSystemProvider implements IFileSystemProvider { const fileSystemProvider = new OverlayFileSystemProvider() fileSystemProvider.register(0, new MkdirpOnWriteInMemoryFileSystemProvider()) +interface CustomFileSystemProviders { + [scheme: string]: IFileSystemProvider +} + const extensionFileSystemProvider = new RegisteredFileSystemProvider(true) class MemoryFileService extends FileService { - constructor (@ILogService logService: ILogService) { + constructor (@ILogService logService: ILogService, additionalProviders: CustomFileSystemProviders = {}) { super(logService) - const userMemoryFileSystem = new InMemoryFileSystemProvider() - - this.registerProvider('user', userMemoryFileSystem) - this.registerProvider('extension', extensionFileSystemProvider) - this.registerProvider('cache', new InMemoryFileSystemProvider()) - this.registerProvider('logs', new InMemoryFileSystemProvider()) - this.registerProvider(Schemas.vscodeUserData, new InMemoryFileSystemProvider()) - this.registerProvider(Schemas.tmp, new InMemoryFileSystemProvider()) - let fileSystemProviderDisposable = this.registerProvider('file', fileSystemProvider) - fileSystemProvider.onDidChangeOverlays(() => { - fileSystemProviderDisposable.dispose() - fileSystemProviderDisposable = this.registerProvider('file', fileSystemProvider) - }) + const defaultProviders = { + user: new InMemoryFileSystemProvider(), + extension: extensionFileSystemProvider, + cache: new InMemoryFileSystemProvider(), + logs: new InMemoryFileSystemProvider(), + [Schemas.vscodeUserData]: new InMemoryFileSystemProvider(), + [Schemas.tmp]: new InMemoryFileSystemProvider(), + file: fileSystemProvider + } + + const providers = { ...defaultProviders, ...additionalProviders } + + for (const [scheme, provider] of Object.entries(providers)) { + let disposable = this.registerProvider(scheme, provider) + if (provider instanceof OverlayFileSystemProvider) { + provider.onDidChangeOverlays(() => { + disposable.dispose() + disposable = this.registerProvider('file', fileSystemProvider) + }) + } + } } } -export default function getServiceOverride (): IEditorOverrideServices { +export default function getServiceOverride (customProviders?: CustomFileSystemProviders): IEditorOverrideServices { return { - [IFileService.toString()]: new SyncDescriptor(MemoryFileService, [], true) + [IFileService.toString()]: new SyncDescriptor(MemoryFileService, [customProviders], true) } } @@ -504,6 +516,7 @@ export function registerFileSystemOverlay (priority: number, provider: IFileSyst } export { + CustomFileSystemProviders, IFileSystemProviderWithFileReadWriteCapability, FileSystemProviderCapabilities, FileType,