]> git.lizzy.rs Git - rust.git/blob - editors/code/src/server.ts
Configuration plumbing for cargo watcher
[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                 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,
66             },
67             traceOutputChannel,
68         };
69
70         Server.client = new lc.LanguageClient(
71             'rust-analyzer',
72             'Rust Analyzer Language Server',
73             serverOptions,
74             clientOptions,
75         );
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') {
85                     if (
86                         messageOrDataObject.includes(
87                             'rust-analyzer/publishDecorations',
88                         ) ||
89                         messageOrDataObject.includes(
90                             'rust-analyzer/decorationsRequest',
91                         )
92                     ) {
93                         // Don't log publish decorations requests
94                     } else {
95                         // @ts-ignore This is just a utility function
96                         Server.client.logTrace(messageOrDataObject, data);
97                     }
98                 } else {
99                     // @ts-ignore
100                     Server.client.logObjectTrace(messageOrDataObject);
101                 }
102             },
103         };
104         Server.client.registerProposedFeatures();
105         Server.client.onReady().then(() => {
106             for (const [type, handler] of notificationHandlers) {
107                 Server.client.onNotification(type, handler);
108             }
109         });
110         Server.client.start();
111     }
112 }