Skip to content

Commit

Permalink
feat: add support for custom fs provider
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CompuIves committed Nov 8, 2023
1 parent 29f389a commit 100fc04
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions src/service-override/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,23 +458,30 @@ const fileSystemProvider = new OverlayFileSystemProvider()
fileSystemProvider.register(0, new MkdirpOnWriteInMemoryFileSystemProvider())

const extensionFileSystemProvider = new RegisteredFileSystemProvider(true)

const providers: Record<string, IFileSystemProvider> = {
user: new InMemoryFileSystemProvider(),
extension: extensionFileSystemProvider,
cache: new InMemoryFileSystemProvider(),
logs: new InMemoryFileSystemProvider(),
[Schemas.vscodeUserData]: new InMemoryFileSystemProvider(),
[Schemas.tmp]: new InMemoryFileSystemProvider(),
file: fileSystemProvider
}

class MemoryFileService extends FileService {
constructor (@ILogService logService: ILogService) {
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)
})
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)
})
}
}
}
}

Expand All @@ -484,6 +491,14 @@ export default function getServiceOverride (): IEditorOverrideServices {
}
}

/**
* Register a custom file system provider for the given scheme. This allows us to override
* the default file system provider for a given scheme.
*/
export function registerCustomProvider (scheme: string, provider: IFileSystemProvider): void {
providers[scheme] = provider
}

export function registerExtensionFile (extensionLocation: URI, filePath: string, getContent: () => Promise<string | Uint8Array>): IDisposable {
return extensionFileSystemProvider.registerFile(new RegisteredReadOnlyFile(joinPath(extensionLocation, filePath), getContent))
}
Expand Down

0 comments on commit 100fc04

Please sign in to comment.