1 import { lookpath } from 'lookpath';
2 import { homedir, platform } from 'os';
3 import * as lc from 'vscode-languageclient';
5 import { window, workspace } from 'vscode';
6 import { Config } from './config';
7 import { Highlighter } from './highlighting';
9 function expandPathResolving(path: string) {
10 if (path.startsWith('~/')) {
11 return path.replace('~', homedir());
17 public static highlighter = new Highlighter();
18 public static config = new Config();
19 public static client: lc.LanguageClient;
21 public static async start(
22 notificationHandlers: Iterable<[string, lc.GenericNotificationHandler]>,
24 // '.' Is the fallback if no folder is open
25 // TODO?: Workspace folders support Uri's (eg: file://test.txt). It might be a good idea to test if the uri points to a file.
26 let folder: string = '.';
27 if (workspace.workspaceFolders !== undefined) {
28 folder = workspace.workspaceFolders[0].uri.fsPath.toString();
31 const command = expandPathResolving(this.config.raLspServerPath);
32 // FIXME: remove check when the following issue is fixed:
33 // https://github.com/otiai10/lookpath/issues/4
34 if (platform() !== 'win32') {
35 if (!(await lookpath(command))) {
37 `Cannot find rust-analyzer server \`${command}\` in PATH.`,
41 const run: lc.Executable = {
43 options: { cwd: folder },
45 const serverOptions: lc.ServerOptions = {
49 const traceOutputChannel = window.createOutputChannel(
50 'Rust Analyzer Language Server Trace',
52 const clientOptions: lc.LanguageClientOptions = {
53 documentSelector: [{ scheme: 'file', language: 'rust' }],
54 initializationOptions: {
55 publishDecorations: true,
56 lruCapacity: Server.config.lruCapacity,
57 maxInlayHintLength: Server.config.maxInlayHintLength,
58 cargoCheckEnable: Server.config.cargoCheckOptions.enabled,
59 cargoCheckCommand: Server.config.cargoCheckOptions.command,
60 cargoCheckArgs: Server.config.cargoCheckOptions.arguments,
61 excludeGlobs: Server.config.excludeGlobs,
62 useClientWatching: Server.config.useClientWatching,
63 featureFlags: Server.config.featureFlags,
64 withSysroot: Server.config.withSysroot,
65 cargoFeatures: Server.config.cargoFeatures,
70 Server.client = new lc.LanguageClient(
72 'Rust Analyzer Language Server',
76 // HACK: This is an awful way of filtering out the decorations notifications
77 // However, pending proper support, this is the most effecitve approach
78 // Proper support for this would entail a change to vscode-languageclient to allow not notifying on certain messages
79 // Or the ability to disable the serverside component of highlighting (but this means that to do tracing we need to disable hihlighting)
80 // This also requires considering our settings strategy, which is work which needs doing
81 // @ts-ignore The tracer is private to vscode-languageclient, but we need access to it to not log publishDecorations requests
82 Server.client._tracer = {
83 log: (messageOrDataObject: string | any, data?: string) => {
84 if (typeof messageOrDataObject === 'string') {
86 messageOrDataObject.includes(
87 'rust-analyzer/publishDecorations',
89 messageOrDataObject.includes(
90 'rust-analyzer/decorationsRequest',
93 // Don't log publish decorations requests
95 // @ts-ignore This is just a utility function
96 Server.client.logTrace(messageOrDataObject, data);
100 Server.client.logObjectTrace(messageOrDataObject);
104 Server.client.registerProposedFeatures();
105 Server.client.onReady().then(() => {
106 for (const [type, handler] of notificationHandlers) {
107 Server.client.onNotification(type, handler);
110 Server.client.start();