UNPKG

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

Version:

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

100 lines (97 loc) 4.29 kB
import { __decorate, __param } from 'vscode/external/tslib/tslib.es6.js'; import { DisposableMap } from 'vscode/vscode/vs/base/common/lifecycle'; import { revive } from 'vscode/vscode/vs/base/common/marshalling'; import { CommandsRegistry } from 'vscode/vscode/vs/platform/commands/common/commands'; import { ICommandService } from 'vscode/vscode/vs/platform/commands/common/commands.service'; import { extHostNamedCustomer } from '../../services/extensions/common/extHostCustomers.js'; import { IExtensionService } from 'vscode/vscode/vs/workbench/services/extensions/common/extensions.service'; import { SerializableObjectWithBuffers } from 'vscode/vscode/vs/workbench/services/extensions/common/proxyIdentifier'; import { ExtHostContext, MainContext } from 'vscode/vscode/vs/workbench/api/common/extHost.protocol'; import { isString } from 'vscode/vscode/vs/base/common/types'; let MainThreadCommands = class MainThreadCommands { constructor(extHostContext, _commandService, _extensionService) { this._commandService = _commandService; this._extensionService = _extensionService; this._commandRegistrations = ( new DisposableMap()); this._proxy = ( extHostContext.getProxy(ExtHostContext.ExtHostCommands)); this._generateCommandsDocumentationRegistration = CommandsRegistry.registerCommand('_generateCommandsDocumentation', () => this._generateCommandsDocumentation()); } dispose() { this._commandRegistrations.dispose(); this._generateCommandsDocumentationRegistration.dispose(); } async _generateCommandsDocumentation() { const result = await this._proxy.$getContributedCommandMetadata(); const commands = CommandsRegistry.getCommands(); for (const [id, command] of commands) { if (command.metadata) { result[id] = command.metadata; } } const all = []; for (const id in result) { all.push('`' + id + '` - ' + _generateMarkdown(result[id])); } console.log(all.join('\n')); } $registerCommand(id) { this._commandRegistrations.set(id, CommandsRegistry.registerCommand(id, (accessor, ...args) => { return this._proxy.$executeContributedCommand(id, ...args).then(result => { return revive(result); }); })); } $unregisterCommand(id) { this._commandRegistrations.deleteAndDispose(id); } $fireCommandActivationEvent(id) { const activationEvent = `onCommand:${id}`; if (!this._extensionService.activationEventIsDone(activationEvent)) { this._extensionService.activateByEvent(activationEvent); } } async $executeCommand(id, args, retry) { if (args instanceof SerializableObjectWithBuffers) { args = args.value; } for (let i = 0; i < args.length; i++) { args[i] = revive(args[i]); } if (retry && args.length > 0 && !CommandsRegistry.getCommand(id)) { await this._extensionService.activateByEvent(`onCommand:${id}`); throw ( new Error('$executeCommand:retry')); } return this._commandService.executeCommand(id, ...args); } $getCommands() { return Promise.resolve([...( CommandsRegistry.getCommands().keys())]); } }; MainThreadCommands = __decorate([ extHostNamedCustomer(MainContext.MainThreadCommands), ( __param(1, ICommandService)), ( __param(2, IExtensionService)) ], MainThreadCommands); function _generateMarkdown(description) { if (typeof description === 'string') { return description; } else { const descriptionString = isString(description.description) ? description.description : description.description.original; const parts = [descriptionString]; parts.push('\n\n'); if (description.args) { for (const arg of description.args) { parts.push(`* _${arg.name}_ - ${arg.description || ''}\n`); } } if (description.returns) { parts.push(`* _(returns)_ - ${description.returns}`); } parts.push('\n\n'); return parts.join(''); } } export { MainThreadCommands };