]> git.lizzy.rs Git - rust.git/blob - editors/code/src/server.ts
ff50fcd994cf3e72a7979463e41ee480b0a79ca6
[rust.git] / editors / code / src / server.ts
1 import { homedir } from 'os';
2 import * as lc from 'vscode-languageclient';
3
4 import { window, workspace } from 'vscode';
5 import { Config } from './config';
6 import { Highlighter } from './highlighting';
7
8 function expandPathResolving(path: string) {
9     if (path.startsWith('~/')) {
10         return path.replace('~', homedir());
11     }
12     return path;
13 }
14
15 export class Server {
16     public static highlighter = new Highlighter();
17     public static config = new Config();
18     public static client: lc.LanguageClient;
19
20     public static start(
21         notificationHandlers: Iterable<[string, lc.GenericNotificationHandler]>
22     ) {
23         // '.' Is the fallback if no folder is open
24         // 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.
25         let folder: string = '.';
26         if (workspace.workspaceFolders !== undefined) {
27             folder = workspace.workspaceFolders[0].uri.fsPath.toString();
28         }
29
30         const run: lc.Executable = {
31             command: expandPathResolving(this.config.raLspServerPath),
32             options: { cwd: folder }
33         };
34         const serverOptions: lc.ServerOptions = {
35             run,
36             debug: run
37         };
38         const traceOutputChannel = window.createOutputChannel(
39             'Rust Analyzer Language Server Trace'
40         );
41         const clientOptions: lc.LanguageClientOptions = {
42             documentSelector: [{ scheme: 'file', language: 'rust' }],
43             initializationOptions: {
44                 publishDecorations: true,
45                 showWorkspaceLoaded:
46                     Server.config.showWorkspaceLoadedNotification,
47                 lruCapacity: Server.config.lruCapacity,
48                 excludeGlobs: Server.config.excludeGlobs,
49                 useClientWatching: Server.config.useClientWatching,
50                 featureFlags: Server.config.featureFlags
51             },
52             traceOutputChannel
53         };
54
55         Server.client = new lc.LanguageClient(
56             'rust-analyzer',
57             'Rust Analyzer Language Server',
58             serverOptions,
59             clientOptions
60         );
61         // HACK: This is an awful way of filtering out the decorations notifications
62         // However, pending proper support, this is the most effecitve approach
63         // Proper support for this would entail a change to vscode-languageclient to allow not notifying on certain messages
64         // Or the ability to disable the serverside component of highlighting (but this means that to do tracing we need to disable hihlighting)
65         // This also requires considering our settings strategy, which is work which needs doing
66         // @ts-ignore The tracer is private to vscode-languageclient, but we need access to it to not log publishDecorations requests
67         Server.client._tracer = {
68             log: (messageOrDataObject: string | any, data?: string) => {
69                 if (typeof messageOrDataObject === 'string') {
70                     if (
71                         messageOrDataObject.includes(
72                             'rust-analyzer/publishDecorations'
73                         ) ||
74                         messageOrDataObject.includes(
75                             'rust-analyzer/decorationsRequest'
76                         )
77                     ) {
78                         // Don't log publish decorations requests
79                     } else {
80                         // @ts-ignore This is just a utility function
81                         Server.client.logTrace(messageOrDataObject, data);
82                     }
83                 } else {
84                     // @ts-ignore
85                     Server.client.logObjectTrace(messageOrDataObject);
86                 }
87             }
88         };
89         Server.client.registerProposedFeatures();
90         Server.client.onReady().then(() => {
91             for (const [type, handler] of notificationHandlers) {
92                 Server.client.onNotification(type, handler);
93             }
94         });
95         Server.client.start();
96     }
97 }