]> git.lizzy.rs Git - rust.git/blob - editors/code/src/client.ts
Merge #4505
[rust.git] / editors / code / src / client.ts
1 import * as lc from 'vscode-languageclient';
2 import * as vscode from 'vscode';
3
4 import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed';
5 import { SemanticTokensFeature, DocumentSemanticsTokensSignature } from 'vscode-languageclient/lib/semanticTokens.proposed';
6
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.
11
12     const run: lc.Executable = {
13         command: serverPath,
14         options: { cwd },
15     };
16     const serverOptions: lc.ServerOptions = {
17         run,
18         debug: run,
19     };
20     const traceOutputChannel = vscode.window.createOutputChannel(
21         'Rust Analyzer Language Server Trace',
22     );
23
24     const clientOptions: lc.LanguageClientOptions = {
25         documentSelector: [{ scheme: 'file', language: 'rust' }],
26         initializationOptions: vscode.workspace.getConfiguration("rust-analyzer"),
27         traceOutputChannel,
28         middleware: {
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');
33                 return res;
34             },
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)
40                 };
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                     for (const item of values) {
45                         if (lc.CodeAction.is(item)) {
46                             const action = client.protocol2CodeConverter.asCodeAction(item);
47                             if (isSnippetEdit(item)) {
48                                 action.command = {
49                                     command: "rust-analyzer.applySnippetWorkspaceEdit",
50                                     title: "",
51                                     arguments: [action.edit],
52                                 };
53                                 action.edit = undefined;
54                             }
55                             result.push(action);
56                         } else {
57                             const command = client.protocol2CodeConverter.asCommand(item);
58                             result.push(command);
59                         }
60                     }
61                     return result;
62                 },
63                     (_error) => undefined
64                 );
65             }
66
67         } as any
68     };
69
70     const client = new lc.LanguageClient(
71         'rust-analyzer',
72         'Rust Analyzer Language Server',
73         serverOptions,
74         clientOptions,
75     );
76
77     // To turn on all proposed features use: client.registerProposedFeatures();
78     // Here we want to enable CallHierarchyFeature and SemanticTokensFeature
79     // since they are available on stable.
80     // Note that while these features are stable in vscode their LSP protocol
81     // implementations are still in the "proposed" category for 3.16.
82     client.registerFeature(new CallHierarchyFeature(client));
83     client.registerFeature(new SemanticTokensFeature(client));
84     client.registerFeature(new SnippetTextEditFeature());
85
86     return client;
87 }
88
89 class SnippetTextEditFeature implements lc.StaticFeature {
90     fillClientCapabilities(capabilities: lc.ClientCapabilities): void {
91         const caps: any = capabilities.experimental ?? {};
92         caps.snippetTextEdit = true;
93         capabilities.experimental = caps;
94     }
95     initialize(_capabilities: lc.ServerCapabilities<any>, _documentSelector: lc.DocumentSelector | undefined): void {
96     }
97 }
98
99 function isSnippetEdit(action: lc.CodeAction): boolean {
100     const documentChanges = action.edit?.documentChanges ?? [];
101     for (const edit of documentChanges) {
102         if (lc.TextDocumentEdit.is(edit)) {
103             if (edit.edits.some((indel) => (indel as any).insertTextFormat === lc.InsertTextFormat.Snippet)) {
104                 return true;
105             }
106         }
107     }
108     return false;
109 }