2019-12-08 06:41:44 -06:00
|
|
|
import { lookpath } from 'lookpath';
|
2019-12-08 13:08:46 -06:00
|
|
|
import { homedir, platform } from 'os';
|
2018-10-07 15:59:02 -05:00
|
|
|
import * as lc from 'vscode-languageclient';
|
2018-10-07 15:44:25 -05:00
|
|
|
|
2019-03-11 14:38:46 -05:00
|
|
|
import { window, workspace } from 'vscode';
|
2018-10-07 15:59:02 -05:00
|
|
|
import { Config } from './config';
|
2018-10-08 13:55:22 -05:00
|
|
|
import { Highlighter } from './highlighting';
|
2018-10-07 15:44:25 -05:00
|
|
|
|
2019-08-18 21:48:39 -05:00
|
|
|
function expandPathResolving(path: string) {
|
|
|
|
if (path.startsWith('~/')) {
|
|
|
|
return path.replace('~', homedir());
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2018-10-07 15:44:25 -05:00
|
|
|
export class Server {
|
2018-10-07 15:59:02 -05:00
|
|
|
public static highlighter = new Highlighter();
|
|
|
|
public static config = new Config();
|
|
|
|
public static client: lc.LanguageClient;
|
|
|
|
|
2019-12-08 06:41:44 -06:00
|
|
|
public static async start(
|
2018-10-08 16:38:33 -05:00
|
|
|
notificationHandlers: Iterable<[string, lc.GenericNotificationHandler]>
|
|
|
|
) {
|
2019-03-11 14:38:46 -05:00
|
|
|
// '.' Is the fallback if no folder is open
|
|
|
|
// 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.
|
|
|
|
let folder: string = '.';
|
|
|
|
if (workspace.workspaceFolders !== undefined) {
|
2019-03-11 15:22:54 -05:00
|
|
|
folder = workspace.workspaceFolders[0].uri.fsPath.toString();
|
2019-03-11 14:38:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-08 06:41:44 -06:00
|
|
|
const command = expandPathResolving(this.config.raLspServerPath);
|
2019-12-08 13:08:46 -06:00
|
|
|
// FIXME: remove check when the following issue is fixed:
|
|
|
|
// https://github.com/otiai10/lookpath/issues/4
|
|
|
|
if (platform() !== 'win32') {
|
|
|
|
if (!(await lookpath(command))) {
|
|
|
|
throw new Error(
|
|
|
|
`Cannot find rust-analyzer server \`${command}\` in PATH.`
|
|
|
|
);
|
|
|
|
}
|
2019-12-08 06:41:44 -06:00
|
|
|
}
|
2018-10-07 15:59:02 -05:00
|
|
|
const run: lc.Executable = {
|
2019-12-08 06:41:44 -06:00
|
|
|
command,
|
2019-03-11 14:38:46 -05:00
|
|
|
options: { cwd: folder }
|
2018-10-07 15:59:02 -05:00
|
|
|
};
|
|
|
|
const serverOptions: lc.ServerOptions = {
|
2018-10-07 15:44:25 -05:00
|
|
|
run,
|
2018-10-08 16:38:33 -05:00
|
|
|
debug: run
|
2018-10-07 15:44:25 -05:00
|
|
|
};
|
2019-02-10 04:30:16 -06:00
|
|
|
const traceOutputChannel = window.createOutputChannel(
|
|
|
|
'Rust Analyzer Language Server Trace'
|
|
|
|
);
|
2018-10-07 15:59:02 -05:00
|
|
|
const clientOptions: lc.LanguageClientOptions = {
|
2018-11-08 09:43:02 -06:00
|
|
|
documentSelector: [{ scheme: 'file', language: 'rust' }],
|
|
|
|
initializationOptions: {
|
2019-03-06 03:34:38 -06:00
|
|
|
publishDecorations: true,
|
2019-08-06 06:34:28 -05:00
|
|
|
lruCapacity: Server.config.lruCapacity,
|
2019-11-18 11:02:28 -06:00
|
|
|
maxInlayHintLength: Server.config.maxInlayHintLength,
|
2019-08-22 06:44:16 -05:00
|
|
|
excludeGlobs: Server.config.excludeGlobs,
|
2019-09-06 08:25:24 -05:00
|
|
|
useClientWatching: Server.config.useClientWatching,
|
2019-12-09 11:05:49 -06:00
|
|
|
featureFlags: Server.config.featureFlags,
|
|
|
|
withSysroot: Server.config.withSysroot
|
2019-02-10 04:06:33 -06:00
|
|
|
},
|
|
|
|
traceOutputChannel
|
2018-10-07 15:44:25 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Server.client = new lc.LanguageClient(
|
2019-02-10 04:30:16 -06:00
|
|
|
'rust-analyzer',
|
2019-02-10 04:06:33 -06:00
|
|
|
'Rust Analyzer Language Server',
|
2018-10-07 15:44:25 -05:00
|
|
|
serverOptions,
|
2018-10-08 16:38:33 -05:00
|
|
|
clientOptions
|
2018-10-07 15:44:25 -05:00
|
|
|
);
|
2018-12-24 07:43:08 -06:00
|
|
|
// HACK: This is an awful way of filtering out the decorations notifications
|
|
|
|
// However, pending proper support, this is the most effecitve approach
|
|
|
|
// Proper support for this would entail a change to vscode-languageclient to allow not notifying on certain messages
|
|
|
|
// Or the ability to disable the serverside component of highlighting (but this means that to do tracing we need to disable hihlighting)
|
|
|
|
// This also requires considering our settings strategy, which is work which needs doing
|
|
|
|
// @ts-ignore The tracer is private to vscode-languageclient, but we need access to it to not log publishDecorations requests
|
|
|
|
Server.client._tracer = {
|
|
|
|
log: (messageOrDataObject: string | any, data?: string) => {
|
|
|
|
if (typeof messageOrDataObject === 'string') {
|
|
|
|
if (
|
2019-01-28 05:43:07 -06:00
|
|
|
messageOrDataObject.includes(
|
|
|
|
'rust-analyzer/publishDecorations'
|
|
|
|
) ||
|
|
|
|
messageOrDataObject.includes(
|
|
|
|
'rust-analyzer/decorationsRequest'
|
|
|
|
)
|
2018-12-24 07:43:08 -06:00
|
|
|
) {
|
|
|
|
// Don't log publish decorations requests
|
|
|
|
} else {
|
|
|
|
// @ts-ignore This is just a utility function
|
|
|
|
Server.client.logTrace(messageOrDataObject, data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
Server.client.logObjectTrace(messageOrDataObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-04-21 04:13:48 -05:00
|
|
|
Server.client.registerProposedFeatures();
|
2018-10-07 15:44:25 -05:00
|
|
|
Server.client.onReady().then(() => {
|
2018-10-08 13:55:22 -05:00
|
|
|
for (const [type, handler] of notificationHandlers) {
|
|
|
|
Server.client.onNotification(type, handler);
|
|
|
|
}
|
2018-10-07 15:59:02 -05:00
|
|
|
});
|
2018-10-07 15:44:25 -05:00
|
|
|
Server.client.start();
|
|
|
|
}
|
|
|
|
}
|