Skip to content

Commit c79e830

Browse files
BoykoAlexangelozerr
authored andcommitted
Support 'xml/executeClientCommand' request. Command 'xml.workspace.executeCommand'
1 parent 98fb947 commit c79e830

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

docs/Extensions.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ You can see the [vscode-xml-maven](https://github.com/angelozerr/vscode-xml-mave
3939

4040
## XML extension API (TypeScript)
4141

42-
See [PR 292](https://github.com/redhat-developer/vscode-xml/pull/292)
42+
See [PR 292](https://github.com/redhat-developer/vscode-xml/pull/292)
43+
44+
### Commands
45+
46+
`xml.workspace.executeCommand` - command registered on VSCode client (via **vscode-xml** extension) to let other extensions execute commands on XML Language server
47+
48+
`xml/executeClientCommand` - XML Language server LSP extension to let XML LS extenders execute commands on the client. The command is made available via `IXMLCommandService` on the server side. See [XML LS extensions docs](https://github.com/eclipse/lemminx/blob/master/docs/LemMinX-Extensions.md#xml-language-server-services-available-for-extensions)

src/commands.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ export namespace Commands {
5050
export const OPEN_DOCS = "xml.open.docs";
5151

5252
export const OPEN_DOCS_HOME = "xml.open.docs.home";
53+
54+
/**
55+
* VSCode client command to executes an LSP command on the XML Language Server
56+
*/
57+
export const EXECUTE_WORKSPACE_COMMAND = "xml.workspace.executeCommand";
5358
}

src/extension.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,22 @@
1111
*/
1212

1313
import { prepareExecutable } from './javaServerStarter';
14-
import { LanguageClientOptions, RevealOutputChannelOn, LanguageClient, DidChangeConfigurationNotification, RequestType, TextDocumentPositionParams, ReferencesRequest, NotificationType, MessageType, ConfigurationRequest, ConfigurationParams } from 'vscode-languageclient';
14+
import {
15+
LanguageClientOptions,
16+
RevealOutputChannelOn,
17+
LanguageClient,
18+
DidChangeConfigurationNotification,
19+
RequestType,
20+
TextDocumentPositionParams,
21+
ReferencesRequest,
22+
NotificationType,
23+
MessageType,
24+
ConfigurationRequest,
25+
ConfigurationParams,
26+
ExecuteCommandParams,
27+
CancellationToken,
28+
ExecuteCommandRequest
29+
} from 'vscode-languageclient';
1530
import * as requirements from './requirements';
1631
import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration, Uri, extensions, Command, TextEditor } from "vscode";
1732
import * as path from 'path';
@@ -114,6 +129,10 @@ export interface XMLExtensionApi {
114129

115130
}
116131

132+
namespace ExecuteClientCommandRequest {
133+
export const type: RequestType<ExecuteCommandParams, any, void, void> = new RequestType('xml/executeClientCommand')
134+
}
135+
117136
namespace TagCloseRequest {
118137
export const type: RequestType<TextDocumentPositionParams, AutoCloseResult, any, any> = new RequestType('xml/closeTag');
119138
}
@@ -240,6 +259,30 @@ export function activate(context: ExtensionContext) {
240259

241260
setupActionableNotificationListener(languageClient);
242261

262+
// Handler for 'xml/executeClientCommand` request message that executes a command on the client
263+
languageClient.onRequest(ExecuteClientCommandRequest.type, async (params: ExecuteCommandParams) => {
264+
return await commands.executeCommand(params.command, ...params.arguments);
265+
});
266+
267+
// Register client command to execute custom XML Language Server command
268+
context.subscriptions.push(commands.registerCommand(Commands.EXECUTE_WORKSPACE_COMMAND, (command, ...rest) => {
269+
let token: CancellationToken;
270+
let commandArgs: any[] = rest;
271+
if (rest && rest.length && CancellationToken.is(rest[rest.length - 1])) {
272+
token = rest[rest.length - 1];
273+
commandArgs = rest.slice(0, rest.length - 1);
274+
}
275+
const params: ExecuteCommandParams = {
276+
command,
277+
arguments: commandArgs
278+
};
279+
if (token) {
280+
return languageClient.sendRequest(ExecuteCommandRequest.type, params, token);
281+
} else {
282+
return languageClient.sendRequest(ExecuteCommandRequest.type, params);
283+
}
284+
}));
285+
243286
context.subscriptions.push(commands.registerCommand(Commands.OPEN_SETTINGS, async (settingId?: string) => {
244287
commands.executeCommand('workbench.action.openSettings', settingId);
245288
}));

0 commit comments

Comments
 (0)