From 8af3d6367ecead0abf80e697176f697d97c25215 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Thu, 9 Mar 2023 15:06:26 -0500 Subject: This commit add Cargo-style project discovery for Buck and Bazel users. This feature requires the user to add a command that generates a `rust-project.json` from a set of files. Project discovery can be invoked in two ways: 1. At extension activation time, which includes the generated `rust-project.json` as part of the linkedProjects argument in InitializeParams 2. Through a new command titled "Add current file to workspace", which makes use of a new, rust-analyzer specific LSP request that adds the workspace without erasing any existing workspaces. I think that the command-running functionality _could_ merit being placed into its own extension (and expose it via extension contribution points), if only provide build-system idiomatic progress reporting and status handling, but I haven't (yet) made an extension that does this. --- editors/code/src/ctx.ts | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) (limited to 'editors/code/src/ctx.ts') diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 1708d47cee7..ba2d4e97af1 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -4,10 +4,11 @@ import * as ra from "./lsp_ext"; import { Config, substituteVSCodeVariables } from "./config"; import { createClient } from "./client"; -import { isRustDocument, isRustEditor, LazyOutputChannel, log, RustEditor } from "./util"; +import { executeDiscoverProject, isRustDocument, isRustEditor, LazyOutputChannel, log, RustEditor } from "./util"; import { ServerStatusParams } from "./lsp_ext"; import { PersistentState } from "./persistent_state"; import { bootstrap } from "./bootstrap"; +import { ExecOptions } from "child_process"; // 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 @@ -16,12 +17,12 @@ import { bootstrap } from "./bootstrap"; export type Workspace = | { kind: "Empty" } | { - kind: "Workspace Folder"; - } + kind: "Workspace Folder"; + } | { - kind: "Detached Files"; - files: vscode.TextDocument[]; - }; + kind: "Detached Files"; + files: vscode.TextDocument[]; + }; export function fetchWorkspace(): Workspace { const folders = (vscode.workspace.workspaceFolders || []).filter( @@ -35,12 +36,19 @@ export function fetchWorkspace(): Workspace { ? rustDocuments.length === 0 ? { kind: "Empty" } : { - kind: "Detached Files", - files: rustDocuments, - } + kind: "Detached Files", + files: rustDocuments, + } : { kind: "Workspace Folder" }; } +export async function discoverWorkspace(files: readonly vscode.TextDocument[], command: string[], options: ExecOptions): Promise { + 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; +} + export type CommandFactory = { enabled: (ctx: CtxInit) => Cmd; disabled?: (ctx: Ctx) => Cmd; @@ -63,6 +71,7 @@ export class Ctx { private state: PersistentState; private commandFactories: Record; private commandDisposables: Disposable[]; + private discoveredWorkspaces: JsonProject[] | undefined; get client() { return this._client; @@ -71,7 +80,7 @@ export class Ctx { constructor( readonly extCtx: vscode.ExtensionContext, commandFactories: Record, - workspace: Workspace + workspace: Workspace, ) { extCtx.subscriptions.push(this); this.statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left); @@ -169,7 +178,18 @@ export class Ctx { }; } - const initializationOptions = substituteVSCodeVariables(rawInitializationOptions); + const discoverProjectCommand = this.config.discoverProjectCommand; + if (discoverProjectCommand) { + let workspaces: JsonProject[] = await Promise.all(vscode.workspace.workspaceFolders!.map(async (folder): Promise => { + return discoverWorkspace(vscode.workspace.textDocuments, discoverProjectCommand, { cwd: folder.uri.fsPath }); + })); + + this.discoveredWorkspaces = workspaces; + } + + let initializationOptions = substituteVSCodeVariables(rawInitializationOptions); + // this appears to be load-bearing, for better or worse. + await initializationOptions.update('linkedProjects', this.discoveredWorkspaces) this._client = await createClient( this.traceOutputChannel, -- cgit 1.4.1-3-g733a5 From 46e022098feda31c98120ca58b6ce02b45cdedf9 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Thu, 9 Mar 2023 15:27:24 -0500 Subject: fmt --- editors/code/package.json | 2 +- editors/code/src/commands.ts | 14 ++++++++---- editors/code/src/config.ts | 2 +- editors/code/src/ctx.ts | 49 +++++++++++++++++++++++++++------------- editors/code/src/lsp_ext.ts | 2 +- editors/code/src/main.ts | 8 +++---- editors/code/src/rust_project.ts | 14 ++++++------ editors/code/src/util.ts | 4 ++-- 8 files changed, 58 insertions(+), 37 deletions(-) (limited to 'editors/code/src/ctx.ts') diff --git a/editors/code/package.json b/editors/code/package.json index e79ab33726d..0a3e4103f6e 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1920,4 +1920,4 @@ } ] } -} \ No newline at end of file +} diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index beff8501dc8..a8ec75a78aa 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -756,14 +756,18 @@ export function addProject(ctx: CtxInit): Cmd { return; } - let workspaces: JsonProject[] = await Promise.all(vscode.workspace.workspaceFolders!.map(async (folder): Promise => { - return discoverWorkspace(vscode.workspace.textDocuments, discoverProjectCommand, { cwd: folder.uri.fsPath }); - })); + const workspaces: JsonProject[] = await Promise.all( + vscode.workspace.workspaceFolders!.map(async (folder): Promise => { + return discoverWorkspace(vscode.workspace.textDocuments, discoverProjectCommand, { + cwd: folder.uri.fsPath, + }); + }) + ); await ctx.client.sendRequest(ra.addProject, { - project: workspaces + project: workspaces, }); - } + }; } async function showReferencesImpl( diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index f62843dffa6..1dae603714d 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -215,7 +215,7 @@ export class Config { } get discoverProjectCommand() { - return this.get("discoverProjectCommand") + return this.get("discoverProjectCommand"); } get cargoRunner() { diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index ba2d4e97af1..5b019d6aeb5 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -4,7 +4,14 @@ import * as ra from "./lsp_ext"; import { Config, substituteVSCodeVariables } from "./config"; import { createClient } from "./client"; -import { executeDiscoverProject, isRustDocument, isRustEditor, LazyOutputChannel, log, RustEditor } from "./util"; +import { + executeDiscoverProject, + isRustDocument, + isRustEditor, + LazyOutputChannel, + log, + RustEditor, +} from "./util"; import { ServerStatusParams } from "./lsp_ext"; import { PersistentState } from "./persistent_state"; import { bootstrap } from "./bootstrap"; @@ -17,12 +24,12 @@ import { ExecOptions } from "child_process"; export type Workspace = | { kind: "Empty" } | { - kind: "Workspace Folder"; - } + kind: "Workspace Folder"; + } | { - kind: "Detached Files"; - files: vscode.TextDocument[]; - }; + kind: "Detached Files"; + files: vscode.TextDocument[]; + }; export function fetchWorkspace(): Workspace { const folders = (vscode.workspace.workspaceFolders || []).filter( @@ -36,13 +43,17 @@ export function fetchWorkspace(): Workspace { ? rustDocuments.length === 0 ? { kind: "Empty" } : { - kind: "Detached Files", - files: rustDocuments, - } + kind: "Detached Files", + files: rustDocuments, + } : { kind: "Workspace Folder" }; } -export async function discoverWorkspace(files: readonly vscode.TextDocument[], command: string[], options: ExecOptions): Promise { +export async function discoverWorkspace( + files: readonly vscode.TextDocument[], + command: string[], + options: ExecOptions +): Promise { const paths = files.map((f) => f.uri.fsPath).join(" "); const joinedCommand = command.join(" "); const data = await executeDiscoverProject(`${joinedCommand} -- ${paths}`, options); @@ -80,7 +91,7 @@ export class Ctx { constructor( readonly extCtx: vscode.ExtensionContext, commandFactories: Record, - workspace: Workspace, + workspace: Workspace ) { extCtx.subscriptions.push(this); this.statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left); @@ -180,16 +191,22 @@ export class Ctx { const discoverProjectCommand = this.config.discoverProjectCommand; if (discoverProjectCommand) { - let workspaces: JsonProject[] = await Promise.all(vscode.workspace.workspaceFolders!.map(async (folder): Promise => { - return discoverWorkspace(vscode.workspace.textDocuments, discoverProjectCommand, { cwd: folder.uri.fsPath }); - })); + const workspaces: JsonProject[] = await Promise.all( + vscode.workspace.workspaceFolders!.map(async (folder): Promise => { + return discoverWorkspace( + vscode.workspace.textDocuments, + discoverProjectCommand, + { cwd: folder.uri.fsPath } + ); + }) + ); this.discoveredWorkspaces = workspaces; } - let initializationOptions = substituteVSCodeVariables(rawInitializationOptions); + const initializationOptions = substituteVSCodeVariables(rawInitializationOptions); // this appears to be load-bearing, for better or worse. - await initializationOptions.update('linkedProjects', this.discoveredWorkspaces) + await initializationOptions.update("linkedProjects", this.discoveredWorkspaces); this._client = await createClient( this.traceOutputChannel, diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 6c8428aa972..942573c0f16 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -45,7 +45,7 @@ export const relatedTests = new lc.RequestType("rust-analyzer/reloadWorkspace"); export const addProject = new lc.RequestType( "rust-analyzer/addProject" -) +); export const runFlycheck = new lc.NotificationType<{ textDocument: lc.TextDocumentIdentifier | null; diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 323aa89ef06..d5de00561b1 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -24,11 +24,11 @@ export async function activate( vscode.window .showWarningMessage( `You have both the rust-analyzer (rust-lang.rust-analyzer) and Rust (rust-lang.rust) ` + - "plugins enabled. These are known to conflict and cause various functions of " + - "both plugins to not work correctly. You should disable one of them.", + "plugins enabled. These are known to conflict and cause various functions of " + + "both plugins to not work correctly. You should disable one of them.", "Got it" ) - .then(() => { }, console.error); + .then(() => {}, console.error); } const ctx = new Ctx(context, createCommands(), fetchWorkspace()); @@ -146,7 +146,7 @@ function createCommands(): Record { health: "stopped", }); }, - disabled: (_) => async () => { }, + disabled: (_) => async () => {}, }, analyzerStatus: { enabled: commands.analyzerStatus }, diff --git a/editors/code/src/rust_project.ts b/editors/code/src/rust_project.ts index adf0f89c961..187a1a96c10 100644 --- a/editors/code/src/rust_project.ts +++ b/editors/code/src/rust_project.ts @@ -60,9 +60,9 @@ interface Crate { /// rust-analyzer assumes that files from one /// source can't refer to files in another source. source?: { - include_dirs: string[], - exclude_dirs: string[], - }, + include_dirs: string[]; + exclude_dirs: string[]; + }; /// The set of cfgs activated for a given crate, like /// `["unix", "feature=\"foo\"", "feature=\"bar\""]`. cfg: string[]; @@ -73,7 +73,7 @@ interface Crate { target?: string; /// Environment variables, used for /// the `env!` macro - env: { [key: string]: string; }, + env: { [key: string]: string }; /// Whether the crate is a proc-macro crate. is_proc_macro: boolean; @@ -84,8 +84,8 @@ interface Crate { interface Dep { /// Index of a crate in the `crates` array. - crate: number, + crate: number; /// Name as should appear in the (implicit) /// `extern crate name` declaration. - name: string, -} \ No newline at end of file + name: string; +} diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index d2ecdce5b4e..922fbcbcf35 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -150,7 +150,7 @@ export function memoizeAsync( /** Awaitable wrapper around `child_process.exec` */ export function execute(command: string, options: ExecOptions): Promise { - log.info(`running command: ${command}`) + log.info(`running command: ${command}`); return new Promise((resolve, reject) => { exec(command, options, (err, stdout, stderr) => { if (err) { @@ -170,7 +170,7 @@ export function execute(command: string, options: ExecOptions): Promise } export function executeDiscoverProject(command: string, options: ExecOptions): Promise { - log.info(`running command: ${command}`) + log.info(`running command: ${command}`); return new Promise((resolve, reject) => { exec(command, options, (err, stdout, _) => { if (err) { -- cgit 1.4.1-3-g733a5 From 1f5c5350898dc18961a810e73d9ef5f80eaf3ae5 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Thu, 9 Mar 2023 15:47:12 -0500 Subject: remove errant `--` in `executeDiscoverProject` --- editors/code/src/ctx.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code/src/ctx.ts') diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 5b019d6aeb5..407d976bab9 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -56,7 +56,7 @@ export async function discoverWorkspace( ): Promise { const paths = files.map((f) => f.uri.fsPath).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; } -- cgit 1.4.1-3-g733a5 From 8d9bff0c74518d514d59a1638e4717f14caa1d71 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Fri, 10 Mar 2023 19:35:05 -0500 Subject: Add a workspace config-based approach to reloading discovered projects. --- editors/code/src/client.ts | 8 +++++--- editors/code/src/commands.ts | 12 +++++++++--- editors/code/src/config.ts | 17 +++++++++++++---- editors/code/src/ctx.ts | 40 +++++++++++++++++++++++++++------------- 4 files changed, 54 insertions(+), 23 deletions(-) (limited to 'editors/code/src/ctx.ts') diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index 62980ca0464..9103ef2f8fd 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -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; diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index a8ec75a78aa..8a953577e99 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -758,14 +758,20 @@ export function addProject(ctx: CtxInit): Cmd { const workspaces: JsonProject[] = await Promise.all( vscode.workspace.workspaceFolders!.map(async (folder): Promise => { - 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: "", }); }; } diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 1dae603714d..75403725479 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -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(path: string): T | undefined { - return substituteVSCodeVariables(this.cfg.get(path)); + return prepareVSCodeConfig(this.cfg.get(path)); } get serverPath() { @@ -284,18 +287,24 @@ export class Config { } } -export function substituteVSCodeVariables(resp: T): T { +export function prepareVSCodeConfig( + resp: T, + cb?: (key: Extract, res: { [key: string]: any }) => void +): T { if (Is.string(resp)) { return substituteVSCodeVariableInString(resp) as T; } else if (resp && Is.array(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; } diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 407d976bab9..ca30d239c95 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -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 { - 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; 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 => { - 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 = []; -- cgit 1.4.1-3-g733a5 From 6e7bc07cdf225aea811f793c2f712f25846b8d20 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Tue, 14 Mar 2023 13:36:21 -0400 Subject: fix: don't override `linkedProjects` if no workspace was discovered. --- editors/code/src/client.ts | 7 ++++++- editors/code/src/ctx.ts | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'editors/code/src/ctx.ts') diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index 9a576570beb..565cb9c6432 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -96,7 +96,12 @@ export async function createClient( if (resp && Array.isArray(resp)) { return resp.map((val) => { return prepareVSCodeConfig(val, (key, cfg) => { - if (key === "linkedProjects") { + // we only want to set discovered workspaces on the right key + // and if a workspace has been discovered. + if ( + key === "linkedProjects" && + config.discoveredWorkspaces.length > 0 + ) { cfg[key] = config.discoveredWorkspaces; } }); diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index ca30d239c95..ffcfb810a24 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -205,7 +205,9 @@ export class Ctx { const initializationOptions = prepareVSCodeConfig( rawInitializationOptions, (key, obj) => { - if (key === "linkedProjects") { + // we only want to set discovered workspaces on the right key + // and if a workspace has been discovered. + if (key === "linkedProjects" && this.config.discoveredWorkspaces.length > 0) { obj["linkedProjects"] = this.config.discoveredWorkspaces; } } -- cgit 1.4.1-3-g733a5