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 excludeGlobs: Server.config.excludeGlobs,
59 useClientWatching: Server.config.useClientWatching,
60 featureFlags: Server.config.featureFlags
65 Server.client = new lc.LanguageClient(
67 'Rust Analyzer Language Server',
71 // HACK: This is an awful way of filtering out the decorations notifications
72 // However, pending proper support, this is the most effecitve approach
73 // Proper support for this would entail a change to vscode-languageclient to allow not notifying on certain messages
74 // Or the ability to disable the serverside component of highlighting (but this means that to do tracing we need to disable hihlighting)
75 // This also requires considering our settings strategy, which is work which needs doing
76 // @ts-ignore The tracer is private to vscode-languageclient, but we need access to it to not log publishDecorations requests
77 Server.client._tracer = {
78 log: (messageOrDataObject: string | any, data?: string) => {
79 if (typeof messageOrDataObject === 'string') {
81 messageOrDataObject.includes(
82 'rust-analyzer/publishDecorations'
84 messageOrDataObject.includes(
85 'rust-analyzer/decorationsRequest'
88 // Don't log publish decorations requests
90 // @ts-ignore This is just a utility function
91 Server.client.logTrace(messageOrDataObject, data);
95 Server.client.logObjectTrace(messageOrDataObject);
99 Server.client.registerProposedFeatures();
100 Server.client.onReady().then(() => {
101 for (const [type, handler] of notificationHandlers) {
102 Server.client.onNotification(type, handler);
105 Server.client.start();