Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #15830 - davidbarsky:davidbarsky/allow-companion-extension-to-call-rust-analyzer, r=davidbarsky
code: expose workspaces to other extensions; remove `addProject` command This (mostly red) PR does three things: - Exposes two methods to companion extensions (`setWorkspaces` and `notifyRustAnalyzer`). - `setWorkspaces` is needed to update `linkedProjects` _without_ writing workspace/global configuration. - `notifyRustAnalyzer` to get the server to pull the new configuration. - Makes `Ctx` implement `RustAnalyzerExtensionApi` to prevent accidental regressions. - Remove `rust-analyzer.addProject`, as that will live in a buck2 companion extension. No need for that to be in rust-analyzer! I can see the utility of combining `notifyRustAnalyzer` and `setWorkspaces` into a single method (`updateWorkspacesAndNotify()`?), but I don't feel strongly about this. My feeling is that this API could be easily changed in the future.
bors 2023-11-17
parent 58de0b1 · parent 0cd68bf · commit 37a8790
-rw-r--r--editors/code/package.json5
-rw-r--r--editors/code/src/commands.ts22
-rw-r--r--editors/code/src/ctx.ts32
-rw-r--r--editors/code/src/main.ts5
4 files changed, 23 insertions, 41 deletions
diff --git a/editors/code/package.json b/editors/code/package.json
index c2d8954bee..265c63f3e2 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -210,11 +210,6 @@
"category": "rust-analyzer"
},
{
- "command": "rust-analyzer.addProject",
- "title": "Add current file's crate to workspace",
- "category": "rust-analyzer"
- },
- {
"command": "rust-analyzer.restartServer",
"title": "Restart server",
"category": "rust-analyzer"
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 7e24de664e..3d33d255ad 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -870,28 +870,6 @@ export function rebuildProcMacros(ctx: CtxInit): Cmd {
return async () => ctx.client.sendRequest(ra.rebuildProcMacros);
}
-export function addProject(ctx: CtxInit): Cmd {
- return async () => {
- const extensionName = ctx.config.discoverProjectRunner;
- // this command shouldn't be enabled in the first place if this isn't set.
- if (!extensionName) {
- return;
- }
-
- const command = `${extensionName}.discoverWorkspaceCommand`;
- const project: JsonProject = await vscode.commands.executeCommand(command);
-
- ctx.addToDiscoveredWorkspaces([project]);
-
- // 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: "",
- });
- };
-}
-
async function showReferencesImpl(
client: LanguageClient | undefined,
uri: string,
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
index 84d1ad98bd..63ae386c8a 100644
--- a/editors/code/src/ctx.ts
+++ b/editors/code/src/ctx.ts
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
-import type * as lc from "vscode-languageclient/node";
+import * as lc from "vscode-languageclient/node";
import * as ra from "./lsp_ext";
import { Config, prepareVSCodeConfig } from "./config";
@@ -22,6 +22,7 @@ import {
import { execRevealDependency } from "./commands";
import { PersistentState } from "./persistent_state";
import { bootstrap } from "./bootstrap";
+import type { RustAnalyzerExtensionApi } from "./main";
// 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
@@ -64,7 +65,7 @@ export type CtxInit = Ctx & {
readonly client: lc.LanguageClient;
};
-export class Ctx {
+export class Ctx implements RustAnalyzerExtensionApi {
readonly statusBar: vscode.StatusBarItem;
config: Config;
readonly workspace: Workspace;
@@ -189,8 +190,11 @@ export class Ctx {
if (this.config.discoverProjectRunner) {
const command = `${this.config.discoverProjectRunner}.discoverWorkspaceCommand`;
log.info(`running command: ${command}`);
- const project: JsonProject = await vscode.commands.executeCommand(command);
- this.addToDiscoveredWorkspaces([project]);
+ const uris = vscode.workspace.textDocuments
+ .filter(isRustDocument)
+ .map((document) => document.uri);
+ const projects: JsonProject[] = await vscode.commands.executeCommand(command, uris);
+ this.setWorkspaces(projects);
}
if (this.workspace.kind === "Detached Files") {
@@ -342,15 +346,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);
- }
- }
+ setWorkspaces(workspaces: JsonProject[]) {
+ this.config.discoveredWorkspaces = workspaces;
+ }
+
+ async notifyRustAnalyzer(): Promise<void> {
+ // 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 this.client?.sendNotification(lc.DidChangeConfigurationNotification.type, {
+ settings: "",
+ });
}
private updateCommands(forceDisable?: "disable") {
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 5de5aabc39..3073353674 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -9,8 +9,12 @@ import { setContextValue } from "./util";
const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
+// This API is not stable and may break in between minor releases.
export interface RustAnalyzerExtensionApi {
readonly client?: lc.LanguageClient;
+
+ setWorkspaces(workspaces: JsonProject[]): void;
+ notifyRustAnalyzer(): Promise<void>;
}
export async function deactivate() {
@@ -152,7 +156,6 @@ function createCommands(): Record<string, CommandFactory> {
shuffleCrateGraph: { enabled: commands.shuffleCrateGraph },
reloadWorkspace: { enabled: commands.reloadWorkspace },
rebuildProcMacros: { enabled: commands.rebuildProcMacros },
- addProject: { enabled: commands.addProject },
matchingBrace: { enabled: commands.matchingBrace },
joinLines: { enabled: commands.joinLines },
parentModule: { enabled: commands.parentModule },