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 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.
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');
38 const res = new lc.LanguageClient(
40 'Rust Analyzer Language Server',
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
52 log: (messageOrDataObject: string | unknown, data?: string) => {
53 if (typeof messageOrDataObject === 'string') {
55 messageOrDataObject.includes(
56 'rust-analyzer/publishDecorations',
58 messageOrDataObject.includes(
59 'rust-analyzer/decorationsRequest',
62 // Don't log publish decorations requests
64 // @ts-ignore This is just a utility function
65 res.logTrace(messageOrDataObject, data);
69 res.logObjectTrace(messageOrDataObject);
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));