Properly handle vscode workspace changes

This commit is contained in:
Lukas Wirth 2022-10-29 01:28:32 +02:00
parent 2071d00fd2
commit fccf8eb1fd
2 changed files with 76 additions and 42 deletions

View file

@ -4,11 +4,15 @@ import * as ra from "./lsp_ext";
import { Config, substituteVariablesInEnv, substituteVSCodeVariables } from "./config"; import { Config, substituteVariablesInEnv, substituteVSCodeVariables } from "./config";
import { createClient } from "./client"; import { createClient } from "./client";
import { isRustEditor, log, RustEditor } from "./util"; import { isRustDocument, isRustEditor, log, RustEditor } from "./util";
import { ServerStatusParams } from "./lsp_ext"; import { ServerStatusParams } from "./lsp_ext";
import { PersistentState } from "./persistent_state"; import { PersistentState } from "./persistent_state";
import { bootstrap } from "./bootstrap"; import { bootstrap } from "./bootstrap";
// We only support local folders, not eg. Live Share (`vlsl:` scheme), so don't activate if
// only those are in use. We use "Empty" to represent these scenarios
// (r-a still somewhat works with Live Share, because commands are tunneled to the host)
export type Workspace = export type Workspace =
| { kind: "Empty" } | { kind: "Empty" }
| { | {
@ -19,6 +23,24 @@ export type Workspace =
files: vscode.TextDocument[]; files: vscode.TextDocument[];
}; };
export function fetchWorkspace(): Workspace {
const folders = (vscode.workspace.workspaceFolders || []).filter(
(folder) => folder.uri.scheme === "file"
);
const rustDocuments = vscode.workspace.textDocuments.filter((document) =>
isRustDocument(document)
);
return folders.length === 0
? rustDocuments.length === 0
? { kind: "Empty" }
: {
kind: "Detached Files",
files: rustDocuments,
}
: { kind: "Workspace Folder" };
}
export type CommandFactory = { export type CommandFactory = {
enabled: (ctx: CtxInit) => Cmd; enabled: (ctx: CtxInit) => Cmd;
disabled?: (ctx: Ctx) => Cmd; disabled?: (ctx: Ctx) => Cmd;
@ -75,6 +97,31 @@ export class Ctx {
this.commandDisposables.forEach((disposable) => disposable.dispose()); this.commandDisposables.forEach((disposable) => disposable.dispose());
} }
async onWorkspaceFolderChanges() {
const workspace = fetchWorkspace();
if (workspace.kind === "Detached Files" && this.workspace.kind === "Detached Files") {
if (workspace.files !== this.workspace.files) {
if (this.client?.isRunning()) {
// Ideally we wouldn't need to tear down the server here, but currently detached files
// are only specified at server start
await this.stopAndDispose();
await this.start();
}
return;
}
}
if (workspace.kind === "Workspace Folder" && this.workspace.kind === "Workspace Folder") {
return;
}
if (workspace.kind === "Empty") {
await this.stopAndDispose();
return;
}
if (this.client?.isRunning()) {
await this.restart();
}
}
private async getOrCreateClient() { private async getOrCreateClient() {
if (this.workspace.kind === "Empty") { if (this.workspace.kind === "Empty") {
return; return;
@ -143,8 +190,8 @@ export class Ctx {
return this._client; return this._client;
} }
async activate() { async start() {
log.info("Activating language client"); log.info("Starting language client");
const client = await this.getOrCreateClient(); const client = await this.getOrCreateClient();
if (!client) { if (!client) {
return; return;
@ -153,13 +200,10 @@ export class Ctx {
this.updateCommands(); this.updateCommands();
} }
async deactivate() { async restart() {
if (!this._client) { // FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
return; await this.stopAndDispose();
} await this.start();
log.info("Deactivating language client");
this.updateCommands("disable");
await this._client.stop();
} }
async stop() { async stop() {
@ -168,6 +212,15 @@ export class Ctx {
} }
log.info("Stopping language client"); log.info("Stopping language client");
this.updateCommands("disable"); this.updateCommands("disable");
await this._client.stop();
}
async stopAndDispose() {
if (!this._client) {
return;
}
log.info("Disposing language client");
this.updateCommands("disable");
await this.disposeClient(); await this.disposeClient();
} }

View file

@ -2,8 +2,7 @@ import * as vscode from "vscode";
import * as lc from "vscode-languageclient/node"; import * as lc from "vscode-languageclient/node";
import * as commands from "./commands"; import * as commands from "./commands";
import { CommandFactory, Ctx, Workspace } from "./ctx"; import { CommandFactory, Ctx, fetchWorkspace } from "./ctx";
import { isRustDocument } from "./util";
import { activateTaskProvider } from "./tasks"; import { activateTaskProvider } from "./tasks";
import { setContextValue } from "./util"; import { setContextValue } from "./util";
@ -31,28 +30,7 @@ export async function activate(
.then(() => {}, console.error); .then(() => {}, console.error);
} }
// We only support local folders, not eg. Live Share (`vlsl:` scheme), so don't activate if const ctx = new Ctx(context, createCommands(), fetchWorkspace());
// only those are in use.
// (r-a still somewhat works with Live Share, because commands are tunneled to the host)
const folders = (vscode.workspace.workspaceFolders || []).filter(
(folder) => folder.uri.scheme === "file"
);
const rustDocuments = vscode.workspace.textDocuments.filter((document) =>
isRustDocument(document)
);
// FIXME: This can change over time
const workspace: Workspace =
folders.length === 0
? rustDocuments.length === 0
? { kind: "Empty" }
: {
kind: "Detached Files",
files: rustDocuments,
}
: { kind: "Workspace Folder" };
const ctx = new Ctx(context, createCommands(), workspace);
// VS Code doesn't show a notification when an extension fails to activate // VS Code doesn't show a notification when an extension fails to activate
// so we do it ourselves. // so we do it ourselves.
const api = await activateServer(ctx).catch((err) => { const api = await activateServer(ctx).catch((err) => {
@ -70,6 +48,11 @@ async function activateServer(ctx: Ctx): Promise<RustAnalyzerExtensionApi> {
ctx.pushExtCleanup(activateTaskProvider(ctx.config)); ctx.pushExtCleanup(activateTaskProvider(ctx.config));
} }
vscode.workspace.onDidChangeWorkspaceFolders(
async (_) => ctx.onWorkspaceFolderChanges(),
null,
ctx.subscriptions
);
vscode.workspace.onDidChangeConfiguration( vscode.workspace.onDidChangeConfiguration(
async (_) => { async (_) => {
await ctx.client?.sendNotification("workspace/didChangeConfiguration", { await ctx.client?.sendNotification("workspace/didChangeConfiguration", {
@ -80,7 +63,7 @@ async function activateServer(ctx: Ctx): Promise<RustAnalyzerExtensionApi> {
ctx.subscriptions ctx.subscriptions
); );
await ctx.activate(); await ctx.start();
return ctx; return ctx;
} }
@ -93,27 +76,25 @@ function createCommands(): Record<string, CommandFactory> {
reload: { reload: {
enabled: (ctx) => async () => { enabled: (ctx) => async () => {
void vscode.window.showInformationMessage("Reloading rust-analyzer..."); void vscode.window.showInformationMessage("Reloading rust-analyzer...");
// FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed await ctx.restart();
await ctx.stop();
await ctx.activate();
}, },
disabled: (ctx) => async () => { disabled: (ctx) => async () => {
void vscode.window.showInformationMessage("Reloading rust-analyzer..."); void vscode.window.showInformationMessage("Reloading rust-analyzer...");
await ctx.activate(); await ctx.start();
}, },
}, },
startServer: { startServer: {
enabled: (ctx) => async () => { enabled: (ctx) => async () => {
await ctx.activate(); await ctx.start();
}, },
disabled: (ctx) => async () => { disabled: (ctx) => async () => {
await ctx.activate(); await ctx.start();
}, },
}, },
stopServer: { stopServer: {
enabled: (ctx) => async () => { enabled: (ctx) => async () => {
// FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed // FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
await ctx.stop(); await ctx.stopAndDispose();
ctx.setServerStatus({ ctx.setServerStatus({
health: "stopped", health: "stopped",
}); });