Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/browser/mainThreadCommands.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
import { DisposableMap, IDisposable } from '../../../base/common/lifecycle.js';
7
import { revive } from '../../../base/common/marshalling.js';
8
import { CommandsRegistry, ICommandMetadata, ICommandService } from '../../../platform/commands/common/commands.js';
9
import { IExtHostContext, extHostNamedCustomer } from '../../services/extensions/common/extHostCustomers.js';
10
import { IExtensionService } from '../../services/extensions/common/extensions.js';
11
import { Dto, SerializableObjectWithBuffers } from '../../services/extensions/common/proxyIdentifier.js';
12
import { ExtHostCommandsShape, ExtHostContext, MainContext, MainThreadCommandsShape } from '../common/extHost.protocol.js';
13
import { isString } from '../../../base/common/types.js';
14
15
16
@extHostNamedCustomer(MainContext.MainThreadCommands)
17
export class MainThreadCommands implements MainThreadCommandsShape {
18
19
private readonly _commandRegistrations = new DisposableMap<string>();
20
private readonly _generateCommandsDocumentationRegistration: IDisposable;
21
private readonly _proxy: ExtHostCommandsShape;
22
23
constructor(
24
extHostContext: IExtHostContext,
25
@ICommandService private readonly _commandService: ICommandService,
26
@IExtensionService private readonly _extensionService: IExtensionService,
27
) {
28
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostCommands);
29
30
this._generateCommandsDocumentationRegistration = CommandsRegistry.registerCommand('_generateCommandsDocumentation', () => this._generateCommandsDocumentation());
31
}
32
33
dispose() {
34
this._commandRegistrations.dispose();
35
this._generateCommandsDocumentationRegistration.dispose();
36
}
37
38
private async _generateCommandsDocumentation(): Promise<void> {
39
const result = await this._proxy.$getContributedCommandMetadata();
40
41
// add local commands
42
const commands = CommandsRegistry.getCommands();
43
for (const [id, command] of commands) {
44
if (command.metadata) {
45
result[id] = command.metadata;
46
}
47
}
48
49
// print all as markdown
50
const all: string[] = [];
51
for (const id in result) {
52
all.push('`' + id + '` - ' + _generateMarkdown(result[id]));
53
}
54
console.log(all.join('\n'));
55
}
56
57
$registerCommand(id: string): void {
58
this._commandRegistrations.set(
59
id,
60
CommandsRegistry.registerCommand(id, (accessor, ...args) => {
61
return this._proxy.$executeContributedCommand(id, ...args).then(result => {
62
return revive(result);
63
});
64
})
65
);
66
}
67
68
$unregisterCommand(id: string): void {
69
this._commandRegistrations.deleteAndDispose(id);
70
}
71
72
$fireCommandActivationEvent(id: string): void {
73
const activationEvent = `onCommand:${id}`;
74
if (!this._extensionService.activationEventIsDone(activationEvent)) {
75
// this is NOT awaited because we only use it as drive-by-activation
76
// for commands that are already known inside the extension host
77
this._extensionService.activateByEvent(activationEvent);
78
}
79
}
80
81
async $executeCommand<T>(id: string, args: any[] | SerializableObjectWithBuffers<any[]>, retry: boolean): Promise<T | undefined> {
82
if (args instanceof SerializableObjectWithBuffers) {
83
args = args.value;
84
}
85
for (let i = 0; i < args.length; i++) {
86
args[i] = revive(args[i]);
87
}
88
if (retry && args.length > 0 && !CommandsRegistry.getCommand(id)) {
89
await this._extensionService.activateByEvent(`onCommand:${id}`);
90
throw new Error('$executeCommand:retry');
91
}
92
return this._commandService.executeCommand<T>(id, ...args);
93
}
94
95
$getCommands(): Promise<string[]> {
96
return Promise.resolve([...CommandsRegistry.getCommands().keys()]);
97
}
98
}
99
100
// --- command doc
101
102
function _generateMarkdown(description: string | Dto<ICommandMetadata> | ICommandMetadata): string {
103
if (typeof description === 'string') {
104
return description;
105
} else {
106
const descriptionString = isString(description.description)
107
? description.description
108
// Our docs website is in English, so keep the original here.
109
: description.description.original;
110
const parts = [descriptionString];
111
parts.push('\n\n');
112
if (description.args) {
113
for (const arg of description.args) {
114
parts.push(`* _${arg.name}_ - ${arg.description || ''}\n`);
115
}
116
}
117
if (description.returns) {
118
parts.push(`* _(returns)_ - ${description.returns}`);
119
}
120
parts.push('\n\n');
121
return parts.join('');
122
}
123
}
124
125