2019-12-30 13:42:59 +00:00
|
|
|
import * as vscode from "vscode";
|
2020-09-01 16:53:07 +00:00
|
|
|
import * as lc from "vscode-languageclient/node";
|
2020-07-02 10:37:04 +00:00
|
|
|
import * as ra from "./lsp_ext";
|
2020-02-02 21:23:01 +00:00
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
import { Config, substituteVariablesInEnv, substituteVSCodeVariables } from "./config";
|
2020-04-02 10:47:58 +00:00
|
|
|
import { createClient } from "./client";
|
2022-10-17 12:20:14 +00:00
|
|
|
import { isRustEditor, log, RustEditor } from "./util";
|
2021-04-06 11:16:35 +00:00
|
|
|
import { ServerStatusParams } from "./lsp_ext";
|
2022-10-17 12:53:46 +00:00
|
|
|
import { PersistentState } from "./persistent_state";
|
|
|
|
import { bootstrap } from "./bootstrap";
|
2019-12-30 13:42:59 +00:00
|
|
|
|
2021-05-23 13:22:13 +00:00
|
|
|
export type Workspace =
|
|
|
|
| {
|
2021-05-23 20:47:58 +00:00
|
|
|
kind: "Workspace Folder";
|
2022-05-17 17:15:06 +00:00
|
|
|
}
|
|
|
|
| {
|
2021-05-23 20:47:58 +00:00
|
|
|
kind: "Detached Files";
|
|
|
|
files: vscode.TextDocument[];
|
2021-05-23 13:22:13 +00:00
|
|
|
};
|
|
|
|
|
2022-10-21 14:00:43 +00:00
|
|
|
export type CommandFactory = {
|
|
|
|
enabled: (ctx: Ctx) => Cmd;
|
|
|
|
disabled?: (ctx: Ctx) => Cmd;
|
|
|
|
};
|
|
|
|
|
2019-12-30 13:42:59 +00:00
|
|
|
export class Ctx {
|
2022-10-17 12:20:14 +00:00
|
|
|
readonly statusBar: vscode.StatusBarItem;
|
2022-10-17 12:53:46 +00:00
|
|
|
readonly config: Config;
|
|
|
|
|
|
|
|
private client: lc.LanguageClient | undefined;
|
2022-10-17 13:05:20 +00:00
|
|
|
private _serverPath: string | undefined;
|
|
|
|
private traceOutputChannel: vscode.OutputChannel | undefined;
|
|
|
|
private outputChannel: vscode.OutputChannel | undefined;
|
2022-10-21 14:00:43 +00:00
|
|
|
private clientSubscriptions: Disposable[];
|
2022-10-17 13:05:20 +00:00
|
|
|
private state: PersistentState;
|
2022-10-21 14:00:43 +00:00
|
|
|
private commandFactories: Record<string, CommandFactory>;
|
|
|
|
private commandDisposables: Disposable[];
|
2022-10-17 12:20:14 +00:00
|
|
|
|
|
|
|
workspace: Workspace;
|
|
|
|
|
2022-10-21 14:00:43 +00:00
|
|
|
constructor(
|
|
|
|
readonly extCtx: vscode.ExtensionContext,
|
|
|
|
workspace: Workspace,
|
|
|
|
commandFactories: Record<string, CommandFactory>
|
|
|
|
) {
|
2022-10-20 19:12:58 +00:00
|
|
|
extCtx.subscriptions.push(this);
|
2022-10-21 14:00:43 +00:00
|
|
|
this.statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
|
2022-10-17 12:20:14 +00:00
|
|
|
this.statusBar.text = "rust-analyzer";
|
|
|
|
this.statusBar.tooltip = "ready";
|
|
|
|
this.statusBar.command = "rust-analyzer.analyzerStatus";
|
|
|
|
this.statusBar.show();
|
|
|
|
this.workspace = workspace;
|
2022-10-21 14:00:43 +00:00
|
|
|
this.clientSubscriptions = [];
|
|
|
|
this.commandDisposables = [];
|
|
|
|
this.commandFactories = commandFactories;
|
2022-10-17 12:53:46 +00:00
|
|
|
|
|
|
|
this.state = new PersistentState(extCtx.globalState);
|
|
|
|
this.config = new Config(extCtx);
|
2022-10-21 14:00:43 +00:00
|
|
|
|
|
|
|
this.updateCommands();
|
2022-10-17 12:20:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 19:12:58 +00:00
|
|
|
dispose() {
|
|
|
|
this.config.dispose();
|
2022-10-21 14:00:43 +00:00
|
|
|
this.statusBar.dispose();
|
|
|
|
void this.disposeClient();
|
|
|
|
this.commandDisposables.forEach((disposable) => disposable.dispose());
|
2022-10-20 19:12:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
clientFetcher() {
|
2022-10-20 19:12:58 +00:00
|
|
|
const self = this;
|
2022-10-17 12:20:14 +00:00
|
|
|
return {
|
|
|
|
get client(): lc.LanguageClient | undefined {
|
2022-10-20 19:12:58 +00:00
|
|
|
return self.client;
|
2022-10-17 12:20:14 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async getClient() {
|
|
|
|
if (!this.traceOutputChannel) {
|
|
|
|
this.traceOutputChannel = vscode.window.createOutputChannel(
|
|
|
|
"Rust Analyzer Language Server Trace"
|
|
|
|
);
|
2022-10-17 14:01:39 +00:00
|
|
|
this.pushExtCleanup(this.traceOutputChannel);
|
2022-10-17 12:20:14 +00:00
|
|
|
}
|
|
|
|
if (!this.outputChannel) {
|
|
|
|
this.outputChannel = vscode.window.createOutputChannel("Rust Analyzer Language Server");
|
2022-10-17 14:01:39 +00:00
|
|
|
this.pushExtCleanup(this.outputChannel);
|
2022-10-17 12:20:14 +00:00
|
|
|
}
|
2022-10-17 12:53:46 +00:00
|
|
|
|
|
|
|
if (!this.client) {
|
2022-10-17 13:05:20 +00:00
|
|
|
this._serverPath = await bootstrap(this.extCtx, this.config, this.state).catch(
|
|
|
|
(err) => {
|
|
|
|
let message = "bootstrap error. ";
|
2022-10-17 12:53:46 +00:00
|
|
|
|
2022-10-17 13:05:20 +00:00
|
|
|
message +=
|
|
|
|
'See the logs in "OUTPUT > Rust Analyzer Client" (should open automatically). ';
|
|
|
|
message +=
|
|
|
|
'To enable verbose logs use { "rust-analyzer.trace.extension": true }';
|
2022-10-17 12:53:46 +00:00
|
|
|
|
2022-10-17 13:05:20 +00:00
|
|
|
log.error("Bootstrap error", err);
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
);
|
2022-10-17 12:20:14 +00:00
|
|
|
const newEnv = substituteVariablesInEnv(
|
|
|
|
Object.assign({}, process.env, this.config.serverExtraEnv)
|
|
|
|
);
|
|
|
|
const run: lc.Executable = {
|
2022-10-17 13:05:20 +00:00
|
|
|
command: this._serverPath,
|
2022-10-17 12:20:14 +00:00
|
|
|
options: { env: newEnv },
|
|
|
|
};
|
2022-10-17 12:53:46 +00:00
|
|
|
const serverOptions = {
|
2022-10-17 12:20:14 +00:00
|
|
|
run,
|
|
|
|
debug: run,
|
|
|
|
};
|
2022-10-17 12:53:46 +00:00
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
let rawInitializationOptions = vscode.workspace.getConfiguration("rust-analyzer");
|
|
|
|
|
|
|
|
if (this.workspace.kind === "Detached Files") {
|
|
|
|
rawInitializationOptions = {
|
|
|
|
detachedFiles: this.workspace.files.map((file) => file.uri.fsPath),
|
|
|
|
...rawInitializationOptions,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const initializationOptions = substituteVSCodeVariables(rawInitializationOptions);
|
|
|
|
|
|
|
|
this.client = await createClient(
|
|
|
|
this.traceOutputChannel,
|
|
|
|
this.outputChannel,
|
|
|
|
initializationOptions,
|
2022-10-17 12:53:46 +00:00
|
|
|
serverOptions
|
2022-10-17 12:20:14 +00:00
|
|
|
);
|
2022-10-21 14:00:43 +00:00
|
|
|
this.pushClientCleanup(
|
|
|
|
this.client.onNotification(ra.serverStatus, (params) =>
|
|
|
|
this.setServerStatus(params)
|
|
|
|
)
|
|
|
|
);
|
2022-10-17 12:20:14 +00:00
|
|
|
}
|
|
|
|
return this.client;
|
|
|
|
}
|
|
|
|
|
|
|
|
async activate() {
|
|
|
|
log.info("Activating language client");
|
|
|
|
const client = await this.getClient();
|
|
|
|
await client.start();
|
2022-10-21 14:00:43 +00:00
|
|
|
this.updateCommands();
|
2022-10-17 12:20:14 +00:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deactivate() {
|
|
|
|
log.info("Deactivating language client");
|
|
|
|
await this.client?.stop();
|
2022-10-21 14:00:43 +00:00
|
|
|
this.updateCommands();
|
2022-10-17 12:20:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 14:00:43 +00:00
|
|
|
async stop() {
|
|
|
|
log.info("Stopping language client");
|
|
|
|
await this.disposeClient();
|
|
|
|
this.updateCommands();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async disposeClient() {
|
|
|
|
this.clientSubscriptions?.forEach((disposable) => disposable.dispose());
|
|
|
|
this.clientSubscriptions = [];
|
2022-10-17 12:20:14 +00:00
|
|
|
await this.client?.dispose();
|
2022-10-17 13:05:20 +00:00
|
|
|
this._serverPath = undefined;
|
2022-10-17 12:20:14 +00:00
|
|
|
this.client = undefined;
|
2020-02-17 13:11:01 +00:00
|
|
|
}
|
2019-12-31 17:14:00 +00:00
|
|
|
|
2020-03-07 12:07:44 +00:00
|
|
|
get activeRustEditor(): RustEditor | undefined {
|
2019-12-30 14:20:13 +00:00
|
|
|
const editor = vscode.window.activeTextEditor;
|
2020-03-07 12:07:44 +00:00
|
|
|
return editor && isRustEditor(editor) ? editor : undefined;
|
2019-12-30 14:20:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 13:35:37 +00:00
|
|
|
get extensionPath(): string {
|
|
|
|
return this.extCtx.extensionPath;
|
|
|
|
}
|
|
|
|
|
2020-02-02 21:23:01 +00:00
|
|
|
get subscriptions(): Disposable[] {
|
2019-12-30 18:05:41 +00:00
|
|
|
return this.extCtx.subscriptions;
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:05:20 +00:00
|
|
|
get serverPath(): string | undefined {
|
|
|
|
return this._serverPath;
|
|
|
|
}
|
|
|
|
|
2022-10-21 14:00:43 +00:00
|
|
|
private updateCommands() {
|
|
|
|
this.commandDisposables.forEach((disposable) => disposable.dispose());
|
|
|
|
this.commandDisposables = [];
|
|
|
|
const fetchFactory = (factory: CommandFactory, fullName: string) => {
|
|
|
|
return this.client && this.client.isRunning()
|
|
|
|
? factory.enabled
|
|
|
|
: factory.disabled ||
|
|
|
|
((_) => () =>
|
|
|
|
vscode.window.showErrorMessage(
|
|
|
|
`command ${fullName} failed: rust-analyzer server is not running`
|
|
|
|
));
|
|
|
|
};
|
|
|
|
for (const [name, factory] of Object.entries(this.commandFactories)) {
|
|
|
|
const fullName = `rust-analyzer.${name}`;
|
|
|
|
const callback = fetchFactory(factory, fullName)(this);
|
|
|
|
this.commandDisposables.push(vscode.commands.registerCommand(fullName, callback));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:16:35 +00:00
|
|
|
setServerStatus(status: ServerStatusParams) {
|
|
|
|
let icon = "";
|
2022-07-13 11:16:35 +00:00
|
|
|
const statusBar = this.statusBar;
|
2021-04-06 11:16:35 +00:00
|
|
|
switch (status.health) {
|
|
|
|
case "ok":
|
2022-07-13 11:16:35 +00:00
|
|
|
statusBar.tooltip = status.message ?? "Ready";
|
|
|
|
statusBar.command = undefined;
|
|
|
|
statusBar.color = undefined;
|
|
|
|
statusBar.backgroundColor = undefined;
|
2020-07-02 10:37:04 +00:00
|
|
|
break;
|
2021-04-06 11:16:35 +00:00
|
|
|
case "warning":
|
2022-07-13 11:16:35 +00:00
|
|
|
statusBar.tooltip =
|
|
|
|
(status.message ? status.message + "\n" : "") + "Click to reload.";
|
|
|
|
|
|
|
|
statusBar.command = "rust-analyzer.reloadWorkspace";
|
|
|
|
statusBar.color = new vscode.ThemeColor("statusBarItem.warningForeground");
|
|
|
|
statusBar.backgroundColor = new vscode.ThemeColor(
|
2022-05-26 10:31:07 +00:00
|
|
|
"statusBarItem.warningBackground"
|
|
|
|
);
|
2021-04-06 11:16:35 +00:00
|
|
|
icon = "$(warning) ";
|
|
|
|
break;
|
|
|
|
case "error":
|
2022-07-22 22:52:12 +00:00
|
|
|
statusBar.tooltip =
|
|
|
|
(status.message ? status.message + "\n" : "") + "Click to reload.";
|
|
|
|
|
2022-07-13 11:16:35 +00:00
|
|
|
statusBar.command = "rust-analyzer.reloadWorkspace";
|
|
|
|
statusBar.color = new vscode.ThemeColor("statusBarItem.errorForeground");
|
|
|
|
statusBar.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground");
|
2021-04-06 11:16:35 +00:00
|
|
|
icon = "$(error) ";
|
2020-07-02 10:37:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-06 11:16:35 +00:00
|
|
|
if (!status.quiescent) icon = "$(sync~spin) ";
|
2022-07-13 11:16:35 +00:00
|
|
|
statusBar.text = `${icon}rust-analyzer`;
|
2020-07-02 10:37:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 12:20:14 +00:00
|
|
|
pushExtCleanup(d: Disposable) {
|
2019-12-30 14:11:30 +00:00
|
|
|
this.extCtx.subscriptions.push(d);
|
2019-12-30 13:42:59 +00:00
|
|
|
}
|
2022-10-21 14:00:43 +00:00
|
|
|
|
|
|
|
private pushClientCleanup(d: Disposable) {
|
|
|
|
this.clientSubscriptions.push(d);
|
|
|
|
}
|
2019-12-30 13:42:59 +00:00
|
|
|
}
|
2019-12-30 13:53:43 +00:00
|
|
|
|
2020-02-02 21:23:01 +00:00
|
|
|
export interface Disposable {
|
|
|
|
dispose(): void;
|
|
|
|
}
|
2020-02-02 20:36:12 +00:00
|
|
|
export type Cmd = (...args: any[]) => unknown;
|