Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/common/extHostCommands.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
/* eslint-disable local/code-no-native-private */
7
8
import { validateConstraint } from '../../../base/common/types.js';
9
import { ICommandMetadata } from '../../../platform/commands/common/commands.js';
10
import * as extHostTypes from './extHostTypes.js';
11
import * as extHostTypeConverter from './extHostTypeConverters.js';
12
import { cloneAndChange } from '../../../base/common/objects.js';
13
import { MainContext, MainThreadCommandsShape, ExtHostCommandsShape, ICommandDto, ICommandMetadataDto, MainThreadTelemetryShape } from './extHost.protocol.js';
14
import { isNonEmptyArray } from '../../../base/common/arrays.js';
15
import * as languages from '../../../editor/common/languages.js';
16
import type * as vscode from 'vscode';
17
import { ILogService } from '../../../platform/log/common/log.js';
18
import { revive } from '../../../base/common/marshalling.js';
19
import { IRange, Range } from '../../../editor/common/core/range.js';
20
import { IPosition, Position } from '../../../editor/common/core/position.js';
21
import { URI } from '../../../base/common/uri.js';
22
import { DisposableStore, toDisposable } from '../../../base/common/lifecycle.js';
23
import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
24
import { IExtHostRpcService } from './extHostRpcService.js';
25
import { ISelection } from '../../../editor/common/core/selection.js';
26
import { TestItemImpl } from './extHostTestItem.js';
27
import { VSBuffer } from '../../../base/common/buffer.js';
28
import { SerializableObjectWithBuffers } from '../../services/extensions/common/proxyIdentifier.js';
29
import { toErrorMessage } from '../../../base/common/errorMessage.js';
30
import { StopWatch } from '../../../base/common/stopwatch.js';
31
import { IExtensionDescription } from '../../../platform/extensions/common/extensions.js';
32
import { TelemetryTrustedValue } from '../../../platform/telemetry/common/telemetryUtils.js';
33
import { IExtHostTelemetry } from './extHostTelemetry.js';
34
import { generateUuid } from '../../../base/common/uuid.js';
35
import { isCancellationError } from '../../../base/common/errors.js';
36
37
interface CommandHandler {
38
callback: Function;
39
thisArg: any;
40
metadata?: ICommandMetadata;
41
extension?: IExtensionDescription;
42
}
43
44
export interface ArgumentProcessor {
45
processArgument(arg: any, extension: IExtensionDescription | undefined): any;
46
}
47
48
export class ExtHostCommands implements ExtHostCommandsShape {
49
50
readonly _serviceBrand: undefined;
51
52
#proxy: MainThreadCommandsShape;
53
54
private readonly _commands = new Map<string, CommandHandler>();
55
private readonly _apiCommands = new Map<string, ApiCommand>();
56
#telemetry: MainThreadTelemetryShape;
57
58
private readonly _logService: ILogService;
59
readonly #extHostTelemetry: IExtHostTelemetry;
60
private readonly _argumentProcessors: ArgumentProcessor[];
61
62
readonly converter: CommandsConverter;
63
64
constructor(
65
@IExtHostRpcService extHostRpc: IExtHostRpcService,
66
@ILogService logService: ILogService,
67
@IExtHostTelemetry extHostTelemetry: IExtHostTelemetry
68
) {
69
this.#proxy = extHostRpc.getProxy(MainContext.MainThreadCommands);
70
this._logService = logService;
71
this.#extHostTelemetry = extHostTelemetry;
72
this.#telemetry = extHostRpc.getProxy(MainContext.MainThreadTelemetry);
73
this.converter = new CommandsConverter(
74
this,
75
id => {
76
// API commands that have no return type (void) can be
77
// converted to their internal command and don't need
78
// any indirection commands
79
const candidate = this._apiCommands.get(id);
80
return candidate?.result === ApiCommandResult.Void
81
? candidate : undefined;
82
},
83
logService
84
);
85
this._argumentProcessors = [
86
{
87
processArgument(a) {
88
// URI, Regex
89
return revive(a);
90
}
91
},
92
{
93
processArgument(arg) {
94
return cloneAndChange(arg, function (obj) {
95
// Reverse of https://github.com/microsoft/vscode/blob/1f28c5fc681f4c01226460b6d1c7e91b8acb4a5b/src/vs/workbench/api/node/extHostCommands.ts#L112-L127
96
if (Range.isIRange(obj)) {
97
return extHostTypeConverter.Range.to(obj);
98
}
99
if (Position.isIPosition(obj)) {
100
return extHostTypeConverter.Position.to(obj);
101
}
102
if (Range.isIRange((obj as languages.Location).range) && URI.isUri((obj as languages.Location).uri)) {
103
return extHostTypeConverter.location.to(obj);
104
}
105
if (obj instanceof VSBuffer) {
106
return obj.buffer.buffer;
107
}
108
if (!Array.isArray(obj)) {
109
return obj;
110
}
111
});
112
}
113
}
114
];
115
}
116
117
registerArgumentProcessor(processor: ArgumentProcessor): void {
118
this._argumentProcessors.push(processor);
119
}
120
121
registerApiCommand(apiCommand: ApiCommand): extHostTypes.Disposable {
122
123
124
const registration = this.registerCommand(false, apiCommand.id, async (...apiArgs) => {
125
126
const internalArgs = apiCommand.args.map((arg, i) => {
127
if (!arg.validate(apiArgs[i])) {
128
throw new Error(`Invalid argument '${arg.name}' when running '${apiCommand.id}', received: ${typeof apiArgs[i] === 'object' ? JSON.stringify(apiArgs[i], null, '\t') : apiArgs[i]} `);
129
}
130
return arg.convert(apiArgs[i]);
131
});
132
133
const internalResult = await this.executeCommand(apiCommand.internalId, ...internalArgs);
134
return apiCommand.result.convert(internalResult, apiArgs, this.converter);
135
}, undefined, {
136
description: apiCommand.description,
137
args: apiCommand.args,
138
returns: apiCommand.result.description
139
});
140
141
this._apiCommands.set(apiCommand.id, apiCommand);
142
143
return new extHostTypes.Disposable(() => {
144
registration.dispose();
145
this._apiCommands.delete(apiCommand.id);
146
});
147
}
148
149
registerCommand(global: boolean, id: string, callback: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any, metadata?: ICommandMetadata, extension?: IExtensionDescription): extHostTypes.Disposable {
150
this._logService.trace('ExtHostCommands#registerCommand', id);
151
152
if (!id.trim().length) {
153
throw new Error('invalid id');
154
}
155
156
if (this._commands.has(id)) {
157
throw new Error(`command '${id}' already exists`);
158
}
159
160
this._commands.set(id, { callback, thisArg, metadata, extension });
161
if (global) {
162
this.#proxy.$registerCommand(id);
163
}
164
165
return new extHostTypes.Disposable(() => {
166
if (this._commands.delete(id)) {
167
if (global) {
168
this.#proxy.$unregisterCommand(id);
169
}
170
}
171
});
172
}
173
174
executeCommand<T>(id: string, ...args: any[]): Promise<T> {
175
this._logService.trace('ExtHostCommands#executeCommand', id);
176
return this._doExecuteCommand(id, args, true);
177
}
178
179
private async _doExecuteCommand<T>(id: string, args: any[], retry: boolean): Promise<T> {
180
181
if (this._commands.has(id)) {
182
// - We stay inside the extension host and support
183
// to pass any kind of parameters around.
184
// - We still emit the corresponding activation event
185
// BUT we don't await that event
186
this.#proxy.$fireCommandActivationEvent(id);
187
return this._executeContributedCommand<T>(id, args, false);
188
189
} else {
190
// automagically convert some argument types
191
let hasBuffers = false;
192
const toArgs = cloneAndChange(args, function (value) {
193
if (value instanceof extHostTypes.Position) {
194
return extHostTypeConverter.Position.from(value);
195
} else if (value instanceof extHostTypes.Range) {
196
return extHostTypeConverter.Range.from(value);
197
} else if (value instanceof extHostTypes.Location) {
198
return extHostTypeConverter.location.from(value);
199
} else if (extHostTypes.NotebookRange.isNotebookRange(value)) {
200
return extHostTypeConverter.NotebookRange.from(value);
201
} else if (value instanceof ArrayBuffer) {
202
hasBuffers = true;
203
return VSBuffer.wrap(new Uint8Array(value));
204
} else if (value instanceof Uint8Array) {
205
hasBuffers = true;
206
return VSBuffer.wrap(value);
207
} else if (value instanceof VSBuffer) {
208
hasBuffers = true;
209
return value;
210
}
211
if (!Array.isArray(value)) {
212
return value;
213
}
214
});
215
216
try {
217
const result = await this.#proxy.$executeCommand(id, hasBuffers ? new SerializableObjectWithBuffers(toArgs) : toArgs, retry);
218
return revive<any>(result);
219
} catch (e) {
220
// Rerun the command when it wasn't known, had arguments, and when retry
221
// is enabled. We do this because the command might be registered inside
222
// the extension host now and can therefore accept the arguments as-is.
223
if (e instanceof Error && e.message === '$executeCommand:retry') {
224
return this._doExecuteCommand(id, args, false);
225
} else {
226
throw e;
227
}
228
}
229
}
230
}
231
232
private async _executeContributedCommand<T = unknown>(id: string, args: any[], annotateError: boolean): Promise<T> {
233
const command = this._commands.get(id);
234
if (!command) {
235
throw new Error('Unknown command');
236
}
237
const { callback, thisArg, metadata } = command;
238
if (metadata?.args) {
239
for (let i = 0; i < metadata.args.length; i++) {
240
try {
241
validateConstraint(args[i], metadata.args[i].constraint);
242
} catch (err) {
243
throw new Error(`Running the contributed command: '${id}' failed. Illegal argument '${metadata.args[i].name}' - ${metadata.args[i].description}`);
244
}
245
}
246
}
247
248
const stopWatch = StopWatch.create();
249
try {
250
return await callback.apply(thisArg, args);
251
} catch (err) {
252
// The indirection-command from the converter can fail when invoking the actual
253
// command and in that case it is better to blame the correct command
254
if (id === this.converter.delegatingCommandId) {
255
const actual = this.converter.getActualCommand(...args);
256
if (actual) {
257
id = actual.command;
258
}
259
}
260
if (!isCancellationError(err)) {
261
this._logService.error(err, id, command.extension?.identifier);
262
}
263
264
if (!annotateError) {
265
throw err;
266
}
267
268
if (command.extension?.identifier) {
269
const reported = this.#extHostTelemetry.onExtensionError(command.extension.identifier, err);
270
this._logService.trace('forwarded error to extension?', reported, command.extension?.identifier);
271
}
272
273
throw new class CommandError extends Error {
274
readonly id = id;
275
readonly source = command!.extension?.displayName ?? command!.extension?.name;
276
constructor() {
277
super(toErrorMessage(err));
278
}
279
};
280
}
281
finally {
282
this._reportTelemetry(command, id, stopWatch.elapsed());
283
}
284
}
285
286
private _reportTelemetry(command: CommandHandler, id: string, duration: number) {
287
if (!command.extension) {
288
return;
289
}
290
if (id.startsWith('code.copilot.logStructured')) {
291
// This command is very active. See https://github.com/microsoft/vscode/issues/254153.
292
return;
293
}
294
type ExtensionActionTelemetry = {
295
extensionId: string;
296
id: TelemetryTrustedValue<string>;
297
duration: number;
298
};
299
type ExtensionActionTelemetryMeta = {
300
extensionId: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'The id of the extension handling the command, informing which extensions provide most-used functionality.' };
301
id: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'The id of the command, to understand which specific extension features are most popular.' };
302
duration: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'The duration of the command execution, to detect performance issues' };
303
owner: 'digitarald';
304
comment: 'Used to gain insight on the most popular commands used from extensions';
305
};
306
this.#telemetry.$publicLog2<ExtensionActionTelemetry, ExtensionActionTelemetryMeta>('Extension:ActionExecuted', {
307
extensionId: command.extension.identifier.value,
308
id: new TelemetryTrustedValue(id),
309
duration: duration,
310
});
311
}
312
313
$executeContributedCommand(id: string, ...args: any[]): Promise<unknown> {
314
this._logService.trace('ExtHostCommands#$executeContributedCommand', id);
315
316
const cmdHandler = this._commands.get(id);
317
if (!cmdHandler) {
318
return Promise.reject(new Error(`Contributed command '${id}' does not exist.`));
319
} else {
320
args = args.map(arg => this._argumentProcessors.reduce((r, p) => p.processArgument(r, cmdHandler.extension), arg));
321
return this._executeContributedCommand(id, args, true);
322
}
323
}
324
325
getCommands(filterUnderscoreCommands: boolean = false): Promise<string[]> {
326
this._logService.trace('ExtHostCommands#getCommands', filterUnderscoreCommands);
327
328
return this.#proxy.$getCommands().then(result => {
329
if (filterUnderscoreCommands) {
330
result = result.filter(command => command[0] !== '_');
331
}
332
return result;
333
});
334
}
335
336
$getContributedCommandMetadata(): Promise<{ [id: string]: string | ICommandMetadataDto }> {
337
const result: { [id: string]: string | ICommandMetadata } = Object.create(null);
338
for (const [id, command] of this._commands) {
339
const { metadata } = command;
340
if (metadata) {
341
result[id] = metadata;
342
}
343
}
344
return Promise.resolve(result);
345
}
346
}
347
348
export interface IExtHostCommands extends ExtHostCommands { }
349
export const IExtHostCommands = createDecorator<IExtHostCommands>('IExtHostCommands');
350
351
export class CommandsConverter implements extHostTypeConverter.Command.ICommandsConverter {
352
353
readonly delegatingCommandId: string = `__vsc${generateUuid()}`;
354
private readonly _cache = new Map<string, vscode.Command>();
355
private _cachIdPool = 0;
356
357
// --- conversion between internal and api commands
358
constructor(
359
private readonly _commands: ExtHostCommands,
360
private readonly _lookupApiCommand: (id: string) => ApiCommand | undefined,
361
private readonly _logService: ILogService
362
) {
363
this._commands.registerCommand(true, this.delegatingCommandId, this._executeConvertedCommand, this);
364
}
365
366
toInternal(command: vscode.Command, disposables: DisposableStore): ICommandDto;
367
toInternal(command: vscode.Command | undefined, disposables: DisposableStore): ICommandDto | undefined;
368
toInternal(command: vscode.Command | undefined, disposables: DisposableStore): ICommandDto | undefined {
369
370
if (!command) {
371
return undefined;
372
}
373
374
const result: ICommandDto = {
375
$ident: undefined,
376
id: command.command,
377
title: command.title,
378
tooltip: command.tooltip
379
};
380
381
if (!command.command) {
382
// falsy command id -> return converted command but don't attempt any
383
// argument or API-command dance since this command won't run anyways
384
return result;
385
}
386
387
const apiCommand = this._lookupApiCommand(command.command);
388
if (apiCommand) {
389
// API command with return-value can be converted inplace
390
result.id = apiCommand.internalId;
391
result.arguments = apiCommand.args.map((arg, i) => arg.convert(command.arguments && command.arguments[i]));
392
393
394
} else if (isNonEmptyArray(command.arguments)) {
395
// we have a contributed command with arguments. that
396
// means we don't want to send the arguments around
397
398
const id = `${command.command} /${++this._cachIdPool}`;
399
this._cache.set(id, command);
400
disposables.add(toDisposable(() => {
401
this._cache.delete(id);
402
this._logService.trace('CommandsConverter#DISPOSE', id);
403
}));
404
result.$ident = id;
405
406
result.id = this.delegatingCommandId;
407
result.arguments = [id];
408
409
this._logService.trace('CommandsConverter#CREATE', command.command, id);
410
}
411
412
return result;
413
}
414
415
fromInternal(command: ICommandDto): vscode.Command | undefined {
416
417
if (typeof command.$ident === 'string') {
418
return this._cache.get(command.$ident);
419
420
} else {
421
return {
422
command: command.id,
423
title: command.title,
424
arguments: command.arguments
425
};
426
}
427
}
428
429
430
getActualCommand(...args: any[]): vscode.Command | undefined {
431
return this._cache.get(args[0]);
432
}
433
434
private _executeConvertedCommand<R>(...args: any[]): Promise<R> {
435
const actualCmd = this.getActualCommand(...args);
436
this._logService.trace('CommandsConverter#EXECUTE', args[0], actualCmd ? actualCmd.command : 'MISSING');
437
438
if (!actualCmd) {
439
return Promise.reject(`Actual command not found, wanted to execute ${args[0]}`);
440
}
441
return this._commands.executeCommand(actualCmd.command, ...(actualCmd.arguments || []));
442
}
443
444
}
445
446
447
export class ApiCommandArgument<V, O = V> {
448
449
static readonly Uri = new ApiCommandArgument<URI>('uri', 'Uri of a text document', v => URI.isUri(v), v => v);
450
static readonly Position = new ApiCommandArgument<extHostTypes.Position, IPosition>('position', 'A position in a text document', v => extHostTypes.Position.isPosition(v), extHostTypeConverter.Position.from);
451
static readonly Range = new ApiCommandArgument<extHostTypes.Range, IRange>('range', 'A range in a text document', v => extHostTypes.Range.isRange(v), extHostTypeConverter.Range.from);
452
static readonly Selection = new ApiCommandArgument<extHostTypes.Selection, ISelection>('selection', 'A selection in a text document', v => extHostTypes.Selection.isSelection(v), extHostTypeConverter.Selection.from);
453
static readonly Number = new ApiCommandArgument<number>('number', '', v => typeof v === 'number', v => v);
454
static readonly String = new ApiCommandArgument<string>('string', '', v => typeof v === 'string', v => v);
455
456
static Arr<T, K = T>(element: ApiCommandArgument<T, K>) {
457
return new ApiCommandArgument(
458
`${element.name}_array`,
459
`Array of ${element.name}, ${element.description}`,
460
(v: unknown) => Array.isArray(v) && v.every(e => element.validate(e)),
461
(v: T[]) => v.map(e => element.convert(e))
462
);
463
}
464
465
static readonly CallHierarchyItem = new ApiCommandArgument('item', 'A call hierarchy item', v => v instanceof extHostTypes.CallHierarchyItem, extHostTypeConverter.CallHierarchyItem.from);
466
static readonly TypeHierarchyItem = new ApiCommandArgument('item', 'A type hierarchy item', v => v instanceof extHostTypes.TypeHierarchyItem, extHostTypeConverter.TypeHierarchyItem.from);
467
static readonly TestItem = new ApiCommandArgument('testItem', 'A VS Code TestItem', v => v instanceof TestItemImpl, extHostTypeConverter.TestItem.from);
468
static readonly TestProfile = new ApiCommandArgument('testProfile', 'A VS Code test profile', v => v instanceof extHostTypes.TestRunProfileBase, extHostTypeConverter.TestRunProfile.from);
469
470
constructor(
471
readonly name: string,
472
readonly description: string,
473
readonly validate: (v: V) => boolean,
474
readonly convert: (v: V) => O
475
) { }
476
477
optional(): ApiCommandArgument<V | undefined | null, O | undefined | null> {
478
return new ApiCommandArgument(
479
this.name, `(optional) ${this.description}`,
480
value => value === undefined || value === null || this.validate(value),
481
value => value === undefined ? undefined : value === null ? null : this.convert(value)
482
);
483
}
484
485
with(name: string | undefined, description: string | undefined): ApiCommandArgument<V, O> {
486
return new ApiCommandArgument(name ?? this.name, description ?? this.description, this.validate, this.convert);
487
}
488
}
489
490
export class ApiCommandResult<V, O = V> {
491
492
static readonly Void = new ApiCommandResult<void, void>('no result', v => v);
493
494
constructor(
495
readonly description: string,
496
readonly convert: (v: V, apiArgs: any[], cmdConverter: CommandsConverter) => O
497
) { }
498
}
499
500
export class ApiCommand {
501
502
constructor(
503
readonly id: string,
504
readonly internalId: string,
505
readonly description: string,
506
readonly args: ApiCommandArgument<any, any>[],
507
readonly result: ApiCommandResult<any, any>
508
) { }
509
}
510
511