-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathexecuteCommand.ts
More file actions
102 lines (84 loc) · 3.32 KB
/
executeCommand.ts
File metadata and controls
102 lines (84 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import {
commands as Commands, Disposable, ProviderResult
} from 'vscode';
import {
ClientCapabilities, ServerCapabilities, ExecuteCommandRegistrationOptions, RegistrationType, ExecuteCommandRequest, ExecuteCommandParams
} from 'vscode-languageserver-protocol';
import * as UUID from './utils/uuid';
import { FeatureClient, ensure, DynamicFeature, FeatureState, RegistrationData } from './features';
export interface ExecuteCommandSignature {
(this: void, command: string, args: any[]): ProviderResult<any>;
}
export interface ExecuteCommandMiddleware {
executeCommand?: (this: void, command: string, args: any[], next: ExecuteCommandSignature) => ProviderResult<any>;
}
export class ExecuteCommandFeature implements DynamicFeature<ExecuteCommandRegistrationOptions> {
private readonly _client: FeatureClient<ExecuteCommandMiddleware>;
private readonly _commands: Map<string, Disposable[]>;
constructor(client: FeatureClient<ExecuteCommandMiddleware>) {
this._client = client;
this._commands = new Map();
}
public getState(): FeatureState {
return { kind: 'workspace', id: this.registrationType.method, registrations: this._commands.size > 0 };
}
public get registrationType(): RegistrationType<ExecuteCommandRegistrationOptions> {
return ExecuteCommandRequest.type;
}
public fillClientCapabilities(capabilities: ClientCapabilities): void {
ensure(ensure(capabilities, 'workspace')!, 'executeCommand')!.dynamicRegistration = true;
}
public initialize(capabilities: ServerCapabilities): void {
if (!capabilities.executeCommandProvider) {
return;
}
this.register({
id: UUID.generateUuid(),
registerOptions: Object.assign({}, capabilities.executeCommandProvider)
});
}
public register(data: RegistrationData<ExecuteCommandRegistrationOptions>): void {
const client = this._client;
const middleware = client.middleware;
const executeCommand: ExecuteCommandSignature = (command: string, args: any[]): any => {
const params: ExecuteCommandParams = {
command,
arguments: args
};
return client.sendRequest(ExecuteCommandRequest.type, params).then(
undefined,
(error) => {
return client.handleFailedRequest(ExecuteCommandRequest.type, undefined, error, undefined);
}
);
};
if (data.registerOptions.commands) {
const disposables: Disposable[] = [];
for (const command of data.registerOptions.commands) {
disposables.push(Commands.registerCommand(command, (...args: any[]) => {
return middleware.executeCommand
? middleware.executeCommand(command, args, executeCommand)
: executeCommand(command, args);
}));
}
this._commands.set(data.id, disposables);
}
}
public unregister(id: string): void {
const disposables = this._commands.get(id);
if (disposables) {
this._commands.delete(id);
disposables.forEach(disposable => disposable.dispose());
}
}
public clear(): void {
this._commands.forEach((value) => {
value.forEach(disposable => disposable.dispose());
});
this._commands.clear();
}
}