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

View File

@ -758,14 +758,20 @@ export function addProject(ctx: CtxInit): Cmd {
const workspaces: JsonProject[] = await Promise.all( const workspaces: JsonProject[] = await Promise.all(
vscode.workspace.workspaceFolders!.map(async (folder): Promise<JsonProject> => { 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, cwd: folder.uri.fsPath,
}); });
}) })
); );
await ctx.client.sendRequest(ra.addProject, { ctx.addToDiscoveredWorkspaces(workspaces);
project: 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) { constructor(ctx: vscode.ExtensionContext) {
this.globalStorageUri = ctx.globalStorageUri; this.globalStorageUri = ctx.globalStorageUri;
this.discoveredWorkspaces = [];
vscode.workspace.onDidChangeConfiguration( vscode.workspace.onDidChangeConfiguration(
this.onDidChangeConfiguration, this.onDidChangeConfiguration,
this, this,
@ -55,6 +56,8 @@ export class Config {
log.info("Using configuration", Object.fromEntries(cfg)); log.info("Using configuration", Object.fromEntries(cfg));
} }
public discoveredWorkspaces: JsonProject[];
private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) { private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
this.refreshLogging(); this.refreshLogging();
@ -191,7 +194,7 @@ export class Config {
* So this getter handles this quirk by not requiring the caller to use postfix `!` * So this getter handles this quirk by not requiring the caller to use postfix `!`
*/ */
private get<T>(path: string): T | undefined { private get<T>(path: string): T | undefined {
return substituteVSCodeVariables(this.cfg.get<T>(path)); return prepareVSCodeConfig(this.cfg.get<T>(path));
} }
get serverPath() { 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)) { if (Is.string(resp)) {
return substituteVSCodeVariableInString(resp) as T; return substituteVSCodeVariableInString(resp) as T;
} else if (resp && Is.array<any>(resp)) { } else if (resp && Is.array<any>(resp)) {
return resp.map((val) => { return resp.map((val) => {
return substituteVSCodeVariables(val); return prepareVSCodeConfig(val);
}) as T; }) as T;
} else if (resp && typeof resp === "object") { } else if (resp && typeof resp === "object") {
const res: { [key: string]: any } = {}; const res: { [key: string]: any } = {};
for (const key in resp) { for (const key in resp) {
const val = resp[key]; const val = resp[key];
res[key] = substituteVSCodeVariables(val); res[key] = prepareVSCodeConfig(val);
if (cb) {
cb(key, res);
}
} }
return res as T; return res as T;
} }

View File

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