Add a workspace config-based approach to reloading discovered projects.

This commit is contained in:
David Barsky 2023-03-10 19:35:05 -05:00
parent 91371494ee
commit 8d9bff0c74
4 changed files with 54 additions and 23 deletions

View File

@ -3,10 +3,10 @@ import * as lc from "vscode-languageclient/node";
import * as vscode from "vscode";
import * as ra from "../src/lsp_ext";
import * as Is from "vscode-languageclient/lib/common/utils/is";
import { assert } from "./util";
import { assert, log } from "./util";
import * as diagnostics from "./diagnostics";
import { WorkspaceEdit } from "vscode";
import { Config, substituteVSCodeVariables } from "./config";
import { Config, prepareVSCodeConfig } from "./config";
import { randomUUID } from "crypto";
export interface Env {
@ -95,7 +95,9 @@ export async function createClient(
const resp = await next(params, token);
if (resp && Array.isArray(resp)) {
return resp.map((val) => {
return substituteVSCodeVariables(val);
return prepareVSCodeConfig(val, (key, cfg) => {
cfg[key] = config.discoveredWorkspaces;
});
});
} else {
return resp;

View File

@ -758,14 +758,20 @@ export function addProject(ctx: CtxInit): Cmd {
const workspaces: JsonProject[] = await Promise.all(
vscode.workspace.workspaceFolders!.map(async (folder): Promise<JsonProject> => {
return discoverWorkspace(vscode.workspace.textDocuments, discoverProjectCommand, {
const rustDocuments = vscode.workspace.textDocuments.filter(isRustDocument);
return discoverWorkspace(rustDocuments, discoverProjectCommand, {
cwd: folder.uri.fsPath,
});
})
);
await ctx.client.sendRequest(ra.addProject, {
project: workspaces,
ctx.addToDiscoveredWorkspaces(workspaces);
// this is a workaround to avoid needing writing the `rust-project.json` into
// a workspace-level VS Code-specific settings folder. We'd like to keep the
// `rust-project.json` entirely in-memory.
await ctx.client?.sendNotification(lc.DidChangeConfigurationNotification.type, {
settings: "",
});
};
}

View File

@ -34,6 +34,7 @@ export class Config {
constructor(ctx: vscode.ExtensionContext) {
this.globalStorageUri = ctx.globalStorageUri;
this.discoveredWorkspaces = [];
vscode.workspace.onDidChangeConfiguration(
this.onDidChangeConfiguration,
this,
@ -55,6 +56,8 @@ export class Config {
log.info("Using configuration", Object.fromEntries(cfg));
}
public discoveredWorkspaces: JsonProject[];
private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
this.refreshLogging();
@ -191,7 +194,7 @@ export class Config {
* So this getter handles this quirk by not requiring the caller to use postfix `!`
*/
private get<T>(path: string): T | undefined {
return substituteVSCodeVariables(this.cfg.get<T>(path));
return prepareVSCodeConfig(this.cfg.get<T>(path));
}
get serverPath() {
@ -284,18 +287,24 @@ export class Config {
}
}
export function substituteVSCodeVariables<T>(resp: T): T {
export function prepareVSCodeConfig<T>(
resp: T,
cb?: (key: Extract<keyof T, string>, res: { [key: string]: any }) => void
): T {
if (Is.string(resp)) {
return substituteVSCodeVariableInString(resp) as T;
} else if (resp && Is.array<any>(resp)) {
return resp.map((val) => {
return substituteVSCodeVariables(val);
return prepareVSCodeConfig(val);
}) as T;
} else if (resp && typeof resp === "object") {
const res: { [key: string]: any } = {};
for (const key in resp) {
const val = resp[key];
res[key] = substituteVSCodeVariables(val);
res[key] = prepareVSCodeConfig(val);
if (cb) {
cb(key, res);
}
}
return res as T;
}

View File

@ -2,7 +2,7 @@ import * as vscode from "vscode";
import * as lc from "vscode-languageclient/node";
import * as ra from "./lsp_ext";
import { Config, substituteVSCodeVariables } from "./config";
import { Config, prepareVSCodeConfig } from "./config";
import { createClient } from "./client";
import {
executeDiscoverProject,
@ -54,7 +54,7 @@ export async function discoverWorkspace(
command: string[],
options: ExecOptions
): Promise<JsonProject> {
const paths = files.map((f) => f.uri.fsPath).join(" ");
const paths = files.map((f) => `"${f.uri.fsPath}"`).join(" ");
const joinedCommand = command.join(" ");
const data = await executeDiscoverProject(`${joinedCommand} ${paths}`, options);
return JSON.parse(data) as JsonProject;
@ -71,7 +71,7 @@ export type CtxInit = Ctx & {
export class Ctx {
readonly statusBar: vscode.StatusBarItem;
readonly config: Config;
config: Config;
readonly workspace: Workspace;
private _client: lc.LanguageClient | undefined;
@ -82,7 +82,6 @@ export class Ctx {
private state: PersistentState;
private commandFactories: Record<string, CommandFactory>;
private commandDisposables: Disposable[];
private discoveredWorkspaces: JsonProject[] | undefined;
get client() {
return this._client;
@ -193,20 +192,24 @@ export class Ctx {
if (discoverProjectCommand) {
const workspaces: JsonProject[] = await Promise.all(
vscode.workspace.workspaceFolders!.map(async (folder): Promise<JsonProject> => {
return discoverWorkspace(
vscode.workspace.textDocuments,
discoverProjectCommand,
{ cwd: folder.uri.fsPath }
);
const rustDocuments = vscode.workspace.textDocuments.filter(isRustDocument);
return discoverWorkspace(rustDocuments, discoverProjectCommand, {
cwd: folder.uri.fsPath,
});
})
);
this.discoveredWorkspaces = workspaces;
this.addToDiscoveredWorkspaces(workspaces);
}
const initializationOptions = substituteVSCodeVariables(rawInitializationOptions);
// this appears to be load-bearing, for better or worse.
await initializationOptions.update("linkedProjects", this.discoveredWorkspaces);
const initializationOptions = prepareVSCodeConfig(
rawInitializationOptions,
(key, obj) => {
if (key === "linkedProjects") {
obj["linkedProjects"] = this.config.discoveredWorkspaces;
}
}
);
this._client = await createClient(
this.traceOutputChannel,
@ -288,6 +291,17 @@ export class Ctx {
return this._serverPath;
}
addToDiscoveredWorkspaces(workspaces: JsonProject[]) {
for (const workspace of workspaces) {
const index = this.config.discoveredWorkspaces.indexOf(workspace);
if (~index) {
this.config.discoveredWorkspaces[index] = workspace;
} else {
this.config.discoveredWorkspaces.push(workspace);
}
}
}
private updateCommands(forceDisable?: "disable") {
this.commandDisposables.forEach((disposable) => disposable.dispose());
this.commandDisposables = [];