]> git.lizzy.rs Git - rust.git/blob - editors/code/src/client.ts
feat: add attributes support on struct fields and method #3870
[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 async function createClient(serverPath: string, cwd: string): Promise<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         } as any
36     };
37
38     const res = new lc.LanguageClient(
39         'rust-analyzer',
40         'Rust Analyzer Language Server',
41         serverOptions,
42         clientOptions,
43     );
44
45     // HACK: This is an awful way of filtering out the decorations notifications
46     // However, pending proper support, this is the most effecitve approach
47     // Proper support for this would entail a change to vscode-languageclient to allow not notifying on certain messages
48     // Or the ability to disable the serverside component of highlighting (but this means that to do tracing we need to disable hihlighting)
49     // This also requires considering our settings strategy, which is work which needs doing
50     // @ts-ignore The tracer is private to vscode-languageclient, but we need access to it to not log publishDecorations requests
51     res._tracer = {
52         log: (messageOrDataObject: string | unknown, data?: string) => {
53             if (typeof messageOrDataObject === 'string') {
54                 if (
55                     messageOrDataObject.includes(
56                         'rust-analyzer/publishDecorations',
57                     ) ||
58                     messageOrDataObject.includes(
59                         'rust-analyzer/decorationsRequest',
60                     )
61                 ) {
62                     // Don't log publish decorations requests
63                 } else {
64                     // @ts-ignore This is just a utility function
65                     res.logTrace(messageOrDataObject, data);
66                 }
67             } else {
68                 // @ts-ignore
69                 res.logObjectTrace(messageOrDataObject);
70             }
71         },
72     };
73
74     // To turn on all proposed features use: res.registerProposedFeatures();
75     // Here we want to enable CallHierarchyFeature and SemanticTokensFeature
76     // since they are available on stable.
77     // Note that while these features are stable in vscode their LSP protocol
78     // implementations are still in the "proposed" category for 3.16.
79     res.registerFeature(new CallHierarchyFeature(res));
80     res.registerFeature(new SemanticTokensFeature(res));
81
82     return res;
83 }