Path: blob/main/src/vs/workbench/api/common/extHostCommands.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45/* eslint-disable local/code-no-native-private */67import { validateConstraint } from '../../../base/common/types.js';8import { ICommandMetadata } from '../../../platform/commands/common/commands.js';9import * as extHostTypes from './extHostTypes.js';10import * as extHostTypeConverter from './extHostTypeConverters.js';11import { cloneAndChange } from '../../../base/common/objects.js';12import { MainContext, MainThreadCommandsShape, ExtHostCommandsShape, ICommandDto, ICommandMetadataDto, MainThreadTelemetryShape } from './extHost.protocol.js';13import { isNonEmptyArray } from '../../../base/common/arrays.js';14import * as languages from '../../../editor/common/languages.js';15import type * as vscode from 'vscode';16import { ILogService } from '../../../platform/log/common/log.js';17import { revive } from '../../../base/common/marshalling.js';18import { IRange, Range } from '../../../editor/common/core/range.js';19import { IPosition, Position } from '../../../editor/common/core/position.js';20import { URI } from '../../../base/common/uri.js';21import { DisposableStore, toDisposable } from '../../../base/common/lifecycle.js';22import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';23import { IExtHostRpcService } from './extHostRpcService.js';24import { ISelection } from '../../../editor/common/core/selection.js';25import { TestItemImpl } from './extHostTestItem.js';26import { VSBuffer } from '../../../base/common/buffer.js';27import { SerializableObjectWithBuffers } from '../../services/extensions/common/proxyIdentifier.js';28import { toErrorMessage } from '../../../base/common/errorMessage.js';29import { StopWatch } from '../../../base/common/stopwatch.js';30import { IExtensionDescription } from '../../../platform/extensions/common/extensions.js';31import { TelemetryTrustedValue } from '../../../platform/telemetry/common/telemetryUtils.js';32import { IExtHostTelemetry } from './extHostTelemetry.js';33import { generateUuid } from '../../../base/common/uuid.js';34import { isCancellationError } from '../../../base/common/errors.js';3536interface CommandHandler {37callback: Function;38thisArg: any;39metadata?: ICommandMetadata;40extension?: IExtensionDescription;41}4243export interface ArgumentProcessor {44processArgument(arg: any, extension: IExtensionDescription | undefined): any;45}4647export class ExtHostCommands implements ExtHostCommandsShape {4849readonly _serviceBrand: undefined;5051#proxy: MainThreadCommandsShape;5253private readonly _commands = new Map<string, CommandHandler>();54private readonly _apiCommands = new Map<string, ApiCommand>();55#telemetry: MainThreadTelemetryShape;5657private readonly _logService: ILogService;58readonly #extHostTelemetry: IExtHostTelemetry;59private readonly _argumentProcessors: ArgumentProcessor[];6061readonly converter: CommandsConverter;6263constructor(64@IExtHostRpcService extHostRpc: IExtHostRpcService,65@ILogService logService: ILogService,66@IExtHostTelemetry extHostTelemetry: IExtHostTelemetry67) {68this.#proxy = extHostRpc.getProxy(MainContext.MainThreadCommands);69this._logService = logService;70this.#extHostTelemetry = extHostTelemetry;71this.#telemetry = extHostRpc.getProxy(MainContext.MainThreadTelemetry);72this.converter = new CommandsConverter(73this,74id => {75// API commands that have no return type (void) can be76// converted to their internal command and don't need77// any indirection commands78const candidate = this._apiCommands.get(id);79return candidate?.result === ApiCommandResult.Void80? candidate : undefined;81},82logService83);84this._argumentProcessors = [85{86processArgument(a) {87// URI, Regex88return revive(a);89}90},91{92processArgument(arg) {93return cloneAndChange(arg, function (obj) {94// Reverse of https://github.com/microsoft/vscode/blob/1f28c5fc681f4c01226460b6d1c7e91b8acb4a5b/src/vs/workbench/api/node/extHostCommands.ts#L112-L12795if (Range.isIRange(obj)) {96return extHostTypeConverter.Range.to(obj);97}98if (Position.isIPosition(obj)) {99return extHostTypeConverter.Position.to(obj);100}101if (Range.isIRange((obj as languages.Location).range) && URI.isUri((obj as languages.Location).uri)) {102return extHostTypeConverter.location.to(obj);103}104if (obj instanceof VSBuffer) {105return obj.buffer.buffer;106}107if (!Array.isArray(obj)) {108return obj;109}110});111}112}113];114}115116registerArgumentProcessor(processor: ArgumentProcessor): void {117this._argumentProcessors.push(processor);118}119120registerApiCommand(apiCommand: ApiCommand): extHostTypes.Disposable {121122123const registration = this.registerCommand(false, apiCommand.id, async (...apiArgs) => {124125const internalArgs = apiCommand.args.map((arg, i) => {126if (!arg.validate(apiArgs[i])) {127throw new Error(`Invalid argument '${arg.name}' when running '${apiCommand.id}', received: ${typeof apiArgs[i] === 'object' ? JSON.stringify(apiArgs[i], null, '\t') : apiArgs[i]} `);128}129return arg.convert(apiArgs[i]);130});131132const internalResult = await this.executeCommand(apiCommand.internalId, ...internalArgs);133return apiCommand.result.convert(internalResult, apiArgs, this.converter);134}, undefined, {135description: apiCommand.description,136args: apiCommand.args,137returns: apiCommand.result.description138});139140this._apiCommands.set(apiCommand.id, apiCommand);141142return new extHostTypes.Disposable(() => {143registration.dispose();144this._apiCommands.delete(apiCommand.id);145});146}147148registerCommand(global: boolean, id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, metadata?: ICommandMetadata, extension?: IExtensionDescription): extHostTypes.Disposable {149this._logService.trace('ExtHostCommands#registerCommand', id);150151if (!id.trim().length) {152throw new Error('invalid id');153}154155if (this._commands.has(id)) {156throw new Error(`command '${id}' already exists`);157}158159this._commands.set(id, { callback, thisArg, metadata, extension });160if (global) {161this.#proxy.$registerCommand(id);162}163164return new extHostTypes.Disposable(() => {165if (this._commands.delete(id)) {166if (global) {167this.#proxy.$unregisterCommand(id);168}169}170});171}172173executeCommand<T>(id: string, ...args: any[]): Promise<T> {174this._logService.trace('ExtHostCommands#executeCommand', id);175return this._doExecuteCommand(id, args, true);176}177178private async _doExecuteCommand<T>(id: string, args: any[], retry: boolean): Promise<T> {179180if (this._commands.has(id)) {181// - We stay inside the extension host and support182// to pass any kind of parameters around.183// - We still emit the corresponding activation event184// BUT we don't await that event185this.#proxy.$fireCommandActivationEvent(id);186return this._executeContributedCommand<T>(id, args, false);187188} else {189// automagically convert some argument types190let hasBuffers = false;191const toArgs = cloneAndChange(args, function (value) {192if (value instanceof extHostTypes.Position) {193return extHostTypeConverter.Position.from(value);194} else if (value instanceof extHostTypes.Range) {195return extHostTypeConverter.Range.from(value);196} else if (value instanceof extHostTypes.Location) {197return extHostTypeConverter.location.from(value);198} else if (extHostTypes.NotebookRange.isNotebookRange(value)) {199return extHostTypeConverter.NotebookRange.from(value);200} else if (value instanceof ArrayBuffer) {201hasBuffers = true;202return VSBuffer.wrap(new Uint8Array(value));203} else if (value instanceof Uint8Array) {204hasBuffers = true;205return VSBuffer.wrap(value);206} else if (value instanceof VSBuffer) {207hasBuffers = true;208return value;209}210if (!Array.isArray(value)) {211return value;212}213});214215try {216const result = await this.#proxy.$executeCommand(id, hasBuffers ? new SerializableObjectWithBuffers(toArgs) : toArgs, retry);217return revive<any>(result);218} catch (e) {219// Rerun the command when it wasn't known, had arguments, and when retry220// is enabled. We do this because the command might be registered inside221// the extension host now and can therefore accept the arguments as-is.222if (e instanceof Error && e.message === '$executeCommand:retry') {223return this._doExecuteCommand(id, args, false);224} else {225throw e;226}227}228}229}230231private async _executeContributedCommand<T = unknown>(id: string, args: any[], annotateError: boolean): Promise<T> {232const command = this._commands.get(id);233if (!command) {234throw new Error('Unknown command');235}236const { callback, thisArg, metadata } = command;237if (metadata?.args) {238for (let i = 0; i < metadata.args.length; i++) {239try {240validateConstraint(args[i], metadata.args[i].constraint);241} catch (err) {242throw new Error(`Running the contributed command: '${id}' failed. Illegal argument '${metadata.args[i].name}' - ${metadata.args[i].description}`);243}244}245}246247const stopWatch = StopWatch.create();248try {249return await callback.apply(thisArg, args);250} catch (err) {251// The indirection-command from the converter can fail when invoking the actual252// command and in that case it is better to blame the correct command253if (id === this.converter.delegatingCommandId) {254const actual = this.converter.getActualCommand(...args);255if (actual) {256id = actual.command;257}258}259if (!isCancellationError(err)) {260this._logService.error(err, id, command.extension?.identifier);261}262263if (!annotateError) {264throw err;265}266267if (command.extension?.identifier) {268const reported = this.#extHostTelemetry.onExtensionError(command.extension.identifier, err);269this._logService.trace('forwarded error to extension?', reported, command.extension?.identifier);270}271272throw new class CommandError extends Error {273readonly id = id;274readonly source = command!.extension?.displayName ?? command!.extension?.name;275constructor() {276super(toErrorMessage(err));277}278};279}280finally {281this._reportTelemetry(command, id, stopWatch.elapsed());282}283}284285private _reportTelemetry(command: CommandHandler, id: string, duration: number) {286if (!command.extension) {287return;288}289if (id.startsWith('code.copilot.logStructured')) {290// This command is very active. See https://github.com/microsoft/vscode/issues/254153.291return;292}293type ExtensionActionTelemetry = {294extensionId: string;295id: TelemetryTrustedValue<string>;296duration: number;297};298type ExtensionActionTelemetryMeta = {299extensionId: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'The id of the extension handling the command, informing which extensions provide most-used functionality.' };300id: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'The id of the command, to understand which specific extension features are most popular.' };301duration: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'The duration of the command execution, to detect performance issues' };302owner: 'digitarald';303comment: 'Used to gain insight on the most popular commands used from extensions';304};305this.#telemetry.$publicLog2<ExtensionActionTelemetry, ExtensionActionTelemetryMeta>('Extension:ActionExecuted', {306extensionId: command.extension.identifier.value,307id: new TelemetryTrustedValue(id),308duration: duration,309});310}311312$executeContributedCommand(id: string, ...args: any[]): Promise<unknown> {313this._logService.trace('ExtHostCommands#$executeContributedCommand', id);314315const cmdHandler = this._commands.get(id);316if (!cmdHandler) {317return Promise.reject(new Error(`Contributed command '${id}' does not exist.`));318} else {319args = args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r, cmdHandler.extension), arg));320return this._executeContributedCommand(id, args, true);321}322}323324getCommands(filterUnderscoreCommands: boolean = false): Promise<string[]> {325this._logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands);326327return this.#proxy.$getCommands().then(result => {328if (filterUnderscoreCommands) {329result = result.filter(command => command[0] !== '_');330}331return result;332});333}334335$getContributedCommandMetadata(): Promise<{ [id: string]: string | ICommandMetadataDto }> {336const result: { [id: string]: string | ICommandMetadata } = Object.create(null);337for (const [id, command] of this._commands) {338const { metadata } = command;339if (metadata) {340result[id] = metadata;341}342}343return Promise.resolve(result);344}345}346347export interface IExtHostCommands extends ExtHostCommands { }348export const IExtHostCommands = createDecorator<IExtHostCommands>('IExtHostCommands');349350export class CommandsConverter implements extHostTypeConverter.Command.ICommandsConverter {351352readonly delegatingCommandId: string = `__vsc${generateUuid()}`;353private readonly _cache = new Map<string, vscode.Command>();354private _cachIdPool = 0;355356// --- conversion between internal and api commands357constructor(358private readonly _commands: ExtHostCommands,359private readonly _lookupApiCommand: (id: string) => ApiCommand | undefined,360private readonly _logService: ILogService361) {362this._commands.registerCommand(true, this.delegatingCommandId, this._executeConvertedCommand, this);363}364365toInternal(command: vscode.Command, disposables: DisposableStore): ICommandDto;366toInternal(command: vscode.Command | undefined, disposables: DisposableStore): ICommandDto | undefined;367toInternal(command: vscode.Command | undefined, disposables: DisposableStore): ICommandDto | undefined {368369if (!command) {370return undefined;371}372373const result: ICommandDto = {374$ident: undefined,375id: command.command,376title: command.title,377tooltip: command.tooltip378};379380if (!command.command) {381// falsy command id -> return converted command but don't attempt any382// argument or API-command dance since this command won't run anyways383return result;384}385386const apiCommand = this._lookupApiCommand(command.command);387if (apiCommand) {388// API command with return-value can be converted inplace389result.id = apiCommand.internalId;390result.arguments = apiCommand.args.map((arg, i) => arg.convert(command.arguments && command.arguments[i]));391392393} else if (isNonEmptyArray(command.arguments)) {394// we have a contributed command with arguments. that395// means we don't want to send the arguments around396397const id = `${command.command} /${++this._cachIdPool}`;398this._cache.set(id, command);399disposables.add(toDisposable(() => {400this._cache.delete(id);401this._logService.trace('CommandsConverter#DISPOSE', id);402}));403result.$ident = id;404405result.id = this.delegatingCommandId;406result.arguments = [id];407408this._logService.trace('CommandsConverter#CREATE', command.command, id);409}410411return result;412}413414fromInternal(command: ICommandDto): vscode.Command | undefined {415416if (typeof command.$ident === 'string') {417return this._cache.get(command.$ident);418419} else {420return {421command: command.id,422title: command.title,423arguments: command.arguments424};425}426}427428429getActualCommand(...args: any[]): vscode.Command | undefined {430return this._cache.get(args[0]);431}432433private _executeConvertedCommand<R>(...args: any[]): Promise<R> {434const actualCmd = this.getActualCommand(...args);435this._logService.trace('CommandsConverter#EXECUTE', args[0], actualCmd ? actualCmd.command : 'MISSING');436437if (!actualCmd) {438return Promise.reject(`Actual command not found, wanted to execute ${args[0]}`);439}440return this._commands.executeCommand(actualCmd.command, ...(actualCmd.arguments || []));441}442443}444445446export class ApiCommandArgument<V, O = V> {447448static readonly Uri = new ApiCommandArgument<URI>('uri', 'Uri of a text document', v => URI.isUri(v), v => v);449static readonly Position = new ApiCommandArgument<extHostTypes.Position, IPosition>('position', 'A position in a text document', v => extHostTypes.Position.isPosition(v), extHostTypeConverter.Position.from);450static readonly Range = new ApiCommandArgument<extHostTypes.Range, IRange>('range', 'A range in a text document', v => extHostTypes.Range.isRange(v), extHostTypeConverter.Range.from);451static readonly Selection = new ApiCommandArgument<extHostTypes.Selection, ISelection>('selection', 'A selection in a text document', v => extHostTypes.Selection.isSelection(v), extHostTypeConverter.Selection.from);452static readonly Number = new ApiCommandArgument<number>('number', '', v => typeof v === 'number', v => v);453static readonly String = new ApiCommandArgument<string>('string', '', v => typeof v === 'string', v => v);454455static Arr<T, K = T>(element: ApiCommandArgument<T, K>) {456return new ApiCommandArgument(457`${element.name}_array`,458`Array of ${element.name}, ${element.description}`,459(v: unknown) => Array.isArray(v) && v.every(e => element.validate(e)),460(v: T[]) => v.map(e => element.convert(e))461);462}463464static readonly CallHierarchyItem = new ApiCommandArgument('item', 'A call hierarchy item', v => v instanceof extHostTypes.CallHierarchyItem, extHostTypeConverter.CallHierarchyItem.from);465static readonly TypeHierarchyItem = new ApiCommandArgument('item', 'A type hierarchy item', v => v instanceof extHostTypes.TypeHierarchyItem, extHostTypeConverter.TypeHierarchyItem.from);466static readonly TestItem = new ApiCommandArgument('testItem', 'A VS Code TestItem', v => v instanceof TestItemImpl, extHostTypeConverter.TestItem.from);467static readonly TestProfile = new ApiCommandArgument('testProfile', 'A VS Code test profile', v => v instanceof extHostTypes.TestRunProfileBase, extHostTypeConverter.TestRunProfile.from);468469constructor(470readonly name: string,471readonly description: string,472readonly validate: (v: V) => boolean,473readonly convert: (v: V) => O474) { }475476optional(): ApiCommandArgument<V | undefined | null, O | undefined | null> {477return new ApiCommandArgument(478this.name, `(optional) ${this.description}`,479value => value === undefined || value === null || this.validate(value),480value => value === undefined ? undefined : value === null ? null : this.convert(value)481);482}483484with(name: string | undefined, description: string | undefined): ApiCommandArgument<V, O> {485return new ApiCommandArgument(name ?? this.name, description ?? this.description, this.validate, this.convert);486}487}488489export class ApiCommandResult<V, O = V> {490491static readonly Void = new ApiCommandResult<void, void>('no result', v => v);492493constructor(494readonly description: string,495readonly convert: (v: V, apiArgs: any[], cmdConverter: CommandsConverter) => O496) { }497}498499export class ApiCommand {500501constructor(502readonly id: string,503readonly internalId: string,504readonly description: string,505readonly args: ApiCommandArgument<any, any>[],506readonly result: ApiCommandResult<any, any>507) { }508}509510511