]> git.lizzy.rs Git - rust.git/blob - editors/code/src/server.ts
b346c08288d51a731a0d20fdf04d0e8ccd23b36a
[rust.git] / editors / code / src / server.ts
1 import { lookpath } from 'lookpath';
2 import { homedir, platform } from 'os';
3 import * as lc from 'vscode-languageclient';
4
5 import { window, workspace } from 'vscode';
6 import { Config } from './config';
7 import { Highlighter } from './highlighting';
8
9 function expandPathResolving(path: string) {
10     if (path.startsWith('~/')) {
11         return path.replace('~', homedir());
12     }
13     return path;
14 }
15
16 export class Server {
17     public static highlighter = new Highlighter();
18     public static config = new Config();
19     public static client: lc.LanguageClient;
20
21     public static async start(
22         notificationHandlers: Iterable<[string, lc.GenericNotificationHandler]>
23     ) {
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();
29         }
30
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))) {
36                 throw new Error(
37                     `Cannot find rust-analyzer server \`${command}\` in PATH.`
38                 );
39             }
40         }
41         const run: lc.Executable = {
42             command,
43             options: { cwd: folder }
44         };
45         const serverOptions: lc.ServerOptions = {
46             run,
47             debug: run
48         };
49         const traceOutputChannel = window.createOutputChannel(
50             'Rust Analyzer Language Server Trace'
51         );
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
61             },
62             traceOutputChannel
63         };
64
65         Server.client = new lc.LanguageClient(
66             'rust-analyzer',
67             'Rust Analyzer Language Server',
68             serverOptions,
69             clientOptions
70         );
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') {
80                     if (
81                         messageOrDataObject.includes(
82                             'rust-analyzer/publishDecorations'
83                         ) ||
84                         messageOrDataObject.includes(
85                             'rust-analyzer/decorationsRequest'
86                         )
87                     ) {
88                         // Don't log publish decorations requests
89                     } else {
90                         // @ts-ignore This is just a utility function
91                         Server.client.logTrace(messageOrDataObject, data);
92                     }
93                 } else {
94                     // @ts-ignore
95                     Server.client.logObjectTrace(messageOrDataObject);
96                 }
97             }
98         };
99         Server.client.registerProposedFeatures();
100         Server.client.onReady().then(() => {
101             for (const [type, handler] of notificationHandlers) {
102                 Server.client.onNotification(type, handler);
103             }
104         });
105         Server.client.start();
106     }
107 }