1 import * as lc from 'vscode-languageclient';
2 import * as vscode from 'vscode';
4 import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed';
5 import { SemanticTokensFeature, DocumentSemanticsTokensSignature } from 'vscode-languageclient/lib/semanticTokens.proposed';
7 export function createClient(serverPath: string, cwd: string): lc.LanguageClient {
8 // '.' Is the fallback if no folder is open
9 // TODO?: Workspace folders support Uri's (eg: file://test.txt).
10 // It might be a good idea to test if the uri points to a file.
12 const run: lc.Executable = {
16 const serverOptions: lc.ServerOptions = {
20 const traceOutputChannel = vscode.window.createOutputChannel(
21 'Rust Analyzer Language Server Trace',
24 const clientOptions: lc.LanguageClientOptions = {
25 documentSelector: [{ scheme: 'file', language: 'rust' }],
26 initializationOptions: vscode.workspace.getConfiguration("rust-analyzer"),
29 // Workaround for https://github.com/microsoft/vscode-languageserver-node/issues/576
30 async provideDocumentSemanticTokens(document: vscode.TextDocument, token: vscode.CancellationToken, next: DocumentSemanticsTokensSignature) {
31 const res = await next(document, token);
32 if (res === undefined) throw new Error('busy');
35 async provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken, _next: lc.ProvideCodeActionsSignature) {
36 const params: lc.CodeActionParams = {
37 textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document),
38 range: client.code2ProtocolConverter.asRange(range),
39 context: client.code2ProtocolConverter.asCodeActionContext(context)
41 return client.sendRequest(lc.CodeActionRequest.type, params, token).then((values) => {
42 if (values === null) return undefined;
43 const result: (vscode.CodeAction | vscode.Command)[] = [];
44 const groups = new Map<string, { index: number; items: vscode.CodeAction[] }>();
45 for (const item of values) {
46 if (lc.CodeAction.is(item)) {
47 const action = client.protocol2CodeConverter.asCodeAction(item);
48 const group = actionGroup(item);
49 if (isSnippetEdit(item) || group) {
51 command: "rust-analyzer.applySnippetWorkspaceEdit",
53 arguments: [action.edit],
55 action.edit = undefined;
59 let entry = groups.get(group);
61 entry = { index: result.length, items: [] };
62 groups.set(group, entry);
65 entry.items.push(action);
70 const command = client.protocol2CodeConverter.asCommand(item);
74 for (const [group, { index, items }] of groups) {
75 if (items.length === 1) {
76 result[index] = items[0];
78 const action = new vscode.CodeAction(group);
80 command: "rust-analyzer.applyActionGroup",
82 arguments: [items.map((item) => {
83 return { label: item.title, edit: item.command!!.arguments!![0] };
86 result[index] = action;
98 const client = new lc.LanguageClient(
100 'Rust Analyzer Language Server',
105 // To turn on all proposed features use: client.registerProposedFeatures();
106 // Here we want to enable CallHierarchyFeature and SemanticTokensFeature
107 // since they are available on stable.
108 // Note that while these features are stable in vscode their LSP protocol
109 // implementations are still in the "proposed" category for 3.16.
110 client.registerFeature(new CallHierarchyFeature(client));
111 client.registerFeature(new SemanticTokensFeature(client));
112 client.registerFeature(new ExperimentalFeatures());
117 class ExperimentalFeatures implements lc.StaticFeature {
118 fillClientCapabilities(capabilities: lc.ClientCapabilities): void {
119 const caps: any = capabilities.experimental ?? {};
120 caps.snippetTextEdit = true;
121 caps.codeActionGroup = true;
122 capabilities.experimental = caps;
124 initialize(_capabilities: lc.ServerCapabilities<any>, _documentSelector: lc.DocumentSelector | undefined): void {
128 function isSnippetEdit(action: lc.CodeAction): boolean {
129 const documentChanges = action.edit?.documentChanges ?? [];
130 for (const edit of documentChanges) {
131 if (lc.TextDocumentEdit.is(edit)) {
132 if (edit.edits.some((indel) => (indel as any).insertTextFormat === lc.InsertTextFormat.Snippet)) {
140 function actionGroup(action: lc.CodeAction): string | undefined {
141 return (action as any).group;