mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Auto merge of #14307 - davidbarsky:davidbarsky/add-cargo-style-project-discovery-for-buck-and-bazel-sickos, r=Veykril
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 "rust-analyzer: 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. Note that there is no mechanism to _remove_ workspaces other than "quit the rust-analyzer server". Few notes: - I think that the command-running functionality _could_ merit being placed into its own extension (and expose it via extension contribution points) to provide build-system idiomatic progress reporting and status handling, but I haven't (yet) made an extension that does this nor does Buck expose this sort of functionality. - This approach would _just work_ for Bazel. I'll try and get the tool that's responsible for Buck integration open-sourced soon. - On the testing side of things, I've used this in around my employer's Buck-powered monorepo and it's a nice experience. That being said, I can't think of an open-source repository where this can be tested in public, so you might need to trust me on this one. I'd love to get feedback on: - Naming of LSP extensions/new commands. I'm not too pleased with how "rust-analyzer: Add current file to workspace" is named, in that it's creating a _new_ workspace. I think that this command being added should be gated on `rust-analyzer.discoverProjectCommand` on being set, so I can add this in sequent commits. - My Typescript. It's not particularly good. - Suggestions on handling folders with _both_ Cargo and non-Cargo build systems and if I make activation a bit better. (I previously tried to add this functionality entirely within rust-analyzer-the-LSP server itself, but matklad was right—an extension side approach is much, much easier.)
This commit is contained in:
commit
c15335c8b0
11 changed files with 257 additions and 12 deletions
|
@ -2,12 +2,20 @@ 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 { 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
|
||||
|
@ -41,6 +49,17 @@ export function fetchWorkspace(): Workspace {
|
|||
: { kind: "Workspace Folder" };
|
||||
}
|
||||
|
||||
export async function discoverWorkspace(
|
||||
files: readonly vscode.TextDocument[],
|
||||
command: string[],
|
||||
options: ExecOptions
|
||||
): Promise<JsonProject> {
|
||||
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;
|
||||
|
@ -52,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;
|
||||
|
@ -169,7 +188,30 @@ export class Ctx {
|
|||
};
|
||||
}
|
||||
|
||||
const initializationOptions = substituteVSCodeVariables(rawInitializationOptions);
|
||||
const discoverProjectCommand = this.config.discoverProjectCommand;
|
||||
if (discoverProjectCommand) {
|
||||
const workspaces: JsonProject[] = await Promise.all(
|
||||
vscode.workspace.workspaceFolders!.map(async (folder): Promise<JsonProject> => {
|
||||
const rustDocuments = vscode.workspace.textDocuments.filter(isRustDocument);
|
||||
return discoverWorkspace(rustDocuments, discoverProjectCommand, {
|
||||
cwd: folder.uri.fsPath,
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
this.addToDiscoveredWorkspaces(workspaces);
|
||||
}
|
||||
|
||||
const initializationOptions = prepareVSCodeConfig(
|
||||
rawInitializationOptions,
|
||||
(key, obj) => {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
this._client = await createClient(
|
||||
this.traceOutputChannel,
|
||||
|
@ -251,6 +293,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 = [];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue