UNPKG

@codingame/monaco-vscode-extensions-service-override

Version:

VSCode public API plugged on the monaco editor - extensions service-override

210 lines (207 loc) 8.97 kB
import { __decorate, __param } from 'vscode/external/tslib/tslib.es6.js'; import { Emitter, Event } from 'vscode/vscode/vs/base/common/event'; import { DisposableMap, DisposableStore, toDisposable } from 'vscode/vscode/vs/base/common/lifecycle'; import { URI } from 'vscode/vscode/vs/base/common/uri'; import { FilePermission, FileSystemProviderErrorCode, FileType, FileOperationError, FileOperationResult, toFileSystemProviderErrorCode } from 'vscode/vscode/vs/platform/files/common/files'; import { IFileService } from 'vscode/vscode/vs/platform/files/common/files.service'; import { extHostNamedCustomer } from '../../services/extensions/common/extHostCustomers.js'; import { ExtHostContext, MainContext } from 'vscode/vscode/vs/workbench/api/common/extHost.protocol'; import { VSBuffer } from 'vscode/vscode/vs/base/common/buffer'; var MainThreadFileSystem_1; let MainThreadFileSystem = MainThreadFileSystem_1 = class MainThreadFileSystem { constructor(extHostContext, _fileService) { this._fileService = _fileService; this._fileProvider = ( new DisposableMap()); this._disposables = ( new DisposableStore()); this._proxy = ( extHostContext.getProxy(ExtHostContext.ExtHostFileSystem)); const infoProxy = ( extHostContext.getProxy(ExtHostContext.ExtHostFileSystemInfo)); for (const entry of _fileService.listCapabilities()) { infoProxy.$acceptProviderInfos(( URI.from({ scheme: entry.scheme, path: '/dummy' })), entry.capabilities); } this._disposables.add(_fileService.onDidChangeFileSystemProviderRegistrations(e => infoProxy.$acceptProviderInfos(( URI.from({ scheme: e.scheme, path: '/dummy' })), e.provider?.capabilities ?? null))); this._disposables.add(_fileService.onDidChangeFileSystemProviderCapabilities(e => infoProxy.$acceptProviderInfos(( URI.from({ scheme: e.scheme, path: '/dummy' })), e.provider.capabilities))); } dispose() { this._disposables.dispose(); this._fileProvider.dispose(); } async $registerFileSystemProvider(handle, scheme, capabilities, readonlyMessage) { this._fileProvider.set(handle, ( new RemoteFileSystemProvider( this._fileService, scheme, capabilities, readonlyMessage, handle, this._proxy ))); } $unregisterProvider(handle) { this._fileProvider.deleteAndDispose(handle); } $onFileSystemChange(handle, changes) { const fileProvider = this._fileProvider.get(handle); if (!fileProvider) { throw ( new Error('Unknown file provider')); } fileProvider.$onFileSystemChange(changes); } $stat(uri) { return this._fileService.stat(URI.revive(uri)).then(stat => { return { ctime: stat.ctime, mtime: stat.mtime, size: stat.size, permissions: stat.readonly ? FilePermission.Readonly : undefined, type: MainThreadFileSystem_1._asFileType(stat) }; }).catch(MainThreadFileSystem_1._handleError); } $readdir(uri) { return this._fileService.resolve(URI.revive(uri), { resolveMetadata: false }).then(stat => { if (!stat.isDirectory) { const err = ( new Error(stat.name)); err.name = FileSystemProviderErrorCode.FileNotADirectory; throw err; } return !stat.children ? [] : ( stat.children.map(child => [child.name, MainThreadFileSystem_1._asFileType(child)])); }).catch(MainThreadFileSystem_1._handleError); } static _asFileType(stat) { let res = 0; if (stat.isFile) { res += FileType.File; } else if (stat.isDirectory) { res += FileType.Directory; } if (stat.isSymbolicLink) { res += FileType.SymbolicLink; } return res; } $readFile(uri) { return this._fileService.readFile(URI.revive(uri)).then(file => file.value).catch(MainThreadFileSystem_1._handleError); } $writeFile(uri, content) { return this._fileService.writeFile(URI.revive(uri), content) .then(() => undefined).catch(MainThreadFileSystem_1._handleError); } $rename(source, target, opts) { return this._fileService.move(URI.revive(source), URI.revive(target), opts.overwrite) .then(() => undefined).catch(MainThreadFileSystem_1._handleError); } $copy(source, target, opts) { return this._fileService.copy(URI.revive(source), URI.revive(target), opts.overwrite) .then(() => undefined).catch(MainThreadFileSystem_1._handleError); } $mkdir(uri) { return this._fileService.createFolder(URI.revive(uri)) .then(() => undefined).catch(MainThreadFileSystem_1._handleError); } $delete(uri, opts) { return this._fileService.del(URI.revive(uri), opts).catch(MainThreadFileSystem_1._handleError); } static _handleError(err) { if (err instanceof FileOperationError) { switch (err.fileOperationResult) { case FileOperationResult.FILE_NOT_FOUND: err.name = FileSystemProviderErrorCode.FileNotFound; break; case FileOperationResult.FILE_IS_DIRECTORY: err.name = FileSystemProviderErrorCode.FileIsADirectory; break; case FileOperationResult.FILE_PERMISSION_DENIED: err.name = FileSystemProviderErrorCode.NoPermissions; break; case FileOperationResult.FILE_MOVE_CONFLICT: err.name = FileSystemProviderErrorCode.FileExists; break; } } else if (err instanceof Error) { const code = toFileSystemProviderErrorCode(err); if (code !== FileSystemProviderErrorCode.Unknown) { err.name = code; } } throw err; } $ensureActivation(scheme) { return this._fileService.activateProvider(scheme); } }; MainThreadFileSystem = MainThreadFileSystem_1 = __decorate([ extHostNamedCustomer(MainContext.MainThreadFileSystem), ( __param(1, IFileService)) ], MainThreadFileSystem); class RemoteFileSystemProvider { constructor(fileService, scheme, capabilities, readOnlyMessage, _handle, _proxy) { this.readOnlyMessage = readOnlyMessage; this._handle = _handle; this._proxy = _proxy; this._onDidChange = ( new Emitter()); this.onDidChangeFile = this._onDidChange.event; this.onDidChangeCapabilities = Event.None; this.capabilities = capabilities; this._registration = fileService.registerProvider(scheme, this); } dispose() { this._registration.dispose(); this._onDidChange.dispose(); } watch(resource, opts) { const session = Math.random(); this._proxy.$watch(this._handle, session, resource, opts); return toDisposable(() => { this._proxy.$unwatch(this._handle, session); }); } $onFileSystemChange(changes) { this._onDidChange.fire(( changes.map(RemoteFileSystemProvider._createFileChange))); } static _createFileChange(dto) { return { resource: URI.revive(dto.resource), type: dto.type }; } stat(resource) { return this._proxy.$stat(this._handle, resource).then(undefined, err => { throw err; }); } readFile(resource) { return this._proxy.$readFile(this._handle, resource).then(buffer => buffer.buffer); } writeFile(resource, content, opts) { return this._proxy.$writeFile(this._handle, resource, VSBuffer.wrap(content), opts); } delete(resource, opts) { return this._proxy.$delete(this._handle, resource, opts); } mkdir(resource) { return this._proxy.$mkdir(this._handle, resource); } readdir(resource) { return this._proxy.$readdir(this._handle, resource); } rename(resource, target, opts) { return this._proxy.$rename(this._handle, resource, target, opts); } copy(resource, target, opts) { return this._proxy.$copy(this._handle, resource, target, opts); } open(resource, opts) { return this._proxy.$open(this._handle, resource, opts); } close(fd) { return this._proxy.$close(this._handle, fd); } read(fd, pos, data, offset, length) { return this._proxy.$read(this._handle, fd, pos, length).then(readData => { data.set(readData.buffer, offset); return readData.byteLength; }); } write(fd, pos, data, offset, length) { return this._proxy.$write(this._handle, fd, pos, VSBuffer.wrap(data).slice(offset, offset + length)); } } export { MainThreadFileSystem };