1 import { homedir } from 'os';
2 import * as lc from 'vscode-languageclient';
4 import { window, workspace } from 'vscode';
5 import { Config } from './config';
6 import { Highlighter } from './highlighting';
8 function expandPathResolving(path: string) {
9 if (path.startsWith('~/')) {
10 return path.replace('~', homedir());
16 public static highlighter = new Highlighter();
17 public static config = new Config();
18 public static client: lc.LanguageClient;
21 notificationHandlers: Iterable<[string, lc.GenericNotificationHandler]>
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();
30 const run: lc.Executable = {
31 command: expandPathResolving(this.config.raLspServerPath),
32 options: { cwd: folder }
34 const serverOptions: lc.ServerOptions = {
38 const traceOutputChannel = window.createOutputChannel(
39 'Rust Analyzer Language Server Trace'
41 const clientOptions: lc.LanguageClientOptions = {
42 documentSelector: [{ scheme: 'file', language: 'rust' }],
43 initializationOptions: {
44 publishDecorations: true,
46 Server.config.showWorkspaceLoadedNotification,
47 lruCapacity: Server.config.lruCapacity,
48 excludeGlobs: Server.config.excludeGlobs,
49 useClientWatching: Server.config.useClientWatching,
50 featureFlags: Server.config.featureFlags
55 Server.client = new lc.LanguageClient(
57 'Rust Analyzer Language Server',
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') {
71 messageOrDataObject.includes(
72 'rust-analyzer/publishDecorations'
74 messageOrDataObject.includes(
75 'rust-analyzer/decorationsRequest'
78 // Don't log publish decorations requests
80 // @ts-ignore This is just a utility function
81 Server.client.logTrace(messageOrDataObject, data);
85 Server.client.logObjectTrace(messageOrDataObject);
89 Server.client.registerProposedFeatures();
90 Server.client.onReady().then(() => {
91 for (const [type, handler] of notificationHandlers) {
92 Server.client.onNotification(type, handler);
95 Server.client.start();