Skip to content

Commit 37a8790

Browse files
committed
Auto merge of rust-lang#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.
2 parents 58de0b1 + 0cd68bf commit 37a8790

File tree

4 files changed

+23
-41
lines changed

4 files changed

+23
-41
lines changed

editors/code/package.json

-5
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,6 @@
209209
"title": "Rebuild proc macros and build scripts",
210210
"category": "rust-analyzer"
211211
},
212-
{
213-
"command": "rust-analyzer.addProject",
214-
"title": "Add current file's crate to workspace",
215-
"category": "rust-analyzer"
216-
},
217212
{
218213
"command": "rust-analyzer.restartServer",
219214
"title": "Restart server",

editors/code/src/commands.ts

-22
Original file line numberDiff line numberDiff line change
@@ -870,28 +870,6 @@ export function rebuildProcMacros(ctx: CtxInit): Cmd {
870870
return async () => ctx.client.sendRequest(ra.rebuildProcMacros);
871871
}
872872

873-
export function addProject(ctx: CtxInit): Cmd {
874-
return async () => {
875-
const extensionName = ctx.config.discoverProjectRunner;
876-
// this command shouldn't be enabled in the first place if this isn't set.
877-
if (!extensionName) {
878-
return;
879-
}
880-
881-
const command = `${extensionName}.discoverWorkspaceCommand`;
882-
const project: JsonProject = await vscode.commands.executeCommand(command);
883-
884-
ctx.addToDiscoveredWorkspaces([project]);
885-
886-
// this is a workaround to avoid needing writing the `rust-project.json` into
887-
// a workspace-level VS Code-specific settings folder. We'd like to keep the
888-
// `rust-project.json` entirely in-memory.
889-
await ctx.client?.sendNotification(lc.DidChangeConfigurationNotification.type, {
890-
settings: "",
891-
});
892-
};
893-
}
894-
895873
async function showReferencesImpl(
896874
client: LanguageClient | undefined,
897875
uri: string,

editors/code/src/ctx.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from "vscode";
2-
import type * as lc from "vscode-languageclient/node";
2+
import * as lc from "vscode-languageclient/node";
33
import * as ra from "./lsp_ext";
44

55
import { Config, prepareVSCodeConfig } from "./config";
@@ -22,6 +22,7 @@ import {
2222
import { execRevealDependency } from "./commands";
2323
import { PersistentState } from "./persistent_state";
2424
import { bootstrap } from "./bootstrap";
25+
import type { RustAnalyzerExtensionApi } from "./main";
2526

2627
// We only support local folders, not eg. Live Share (`vlsl:` scheme), so don't activate if
2728
// only those are in use. We use "Empty" to represent these scenarios
@@ -64,7 +65,7 @@ export type CtxInit = Ctx & {
6465
readonly client: lc.LanguageClient;
6566
};
6667

67-
export class Ctx {
68+
export class Ctx implements RustAnalyzerExtensionApi {
6869
readonly statusBar: vscode.StatusBarItem;
6970
config: Config;
7071
readonly workspace: Workspace;
@@ -189,8 +190,11 @@ export class Ctx {
189190
if (this.config.discoverProjectRunner) {
190191
const command = `${this.config.discoverProjectRunner}.discoverWorkspaceCommand`;
191192
log.info(`running command: ${command}`);
192-
const project: JsonProject = await vscode.commands.executeCommand(command);
193-
this.addToDiscoveredWorkspaces([project]);
193+
const uris = vscode.workspace.textDocuments
194+
.filter(isRustDocument)
195+
.map((document) => document.uri);
196+
const projects: JsonProject[] = await vscode.commands.executeCommand(command, uris);
197+
this.setWorkspaces(projects);
194198
}
195199

196200
if (this.workspace.kind === "Detached Files") {
@@ -342,15 +346,17 @@ export class Ctx {
342346
return this._serverPath;
343347
}
344348

345-
addToDiscoveredWorkspaces(workspaces: JsonProject[]) {
346-
for (const workspace of workspaces) {
347-
const index = this.config.discoveredWorkspaces.indexOf(workspace);
348-
if (~index) {
349-
this.config.discoveredWorkspaces[index] = workspace;
350-
} else {
351-
this.config.discoveredWorkspaces.push(workspace);
352-
}
353-
}
349+
setWorkspaces(workspaces: JsonProject[]) {
350+
this.config.discoveredWorkspaces = workspaces;
351+
}
352+
353+
async notifyRustAnalyzer(): Promise<void> {
354+
// this is a workaround to avoid needing writing the `rust-project.json` into
355+
// a workspace-level VS Code-specific settings folder. We'd like to keep the
356+
// `rust-project.json` entirely in-memory.
357+
await this.client?.sendNotification(lc.DidChangeConfigurationNotification.type, {
358+
settings: "",
359+
});
354360
}
355361

356362
private updateCommands(forceDisable?: "disable") {

editors/code/src/main.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import { setContextValue } from "./util";
99

1010
const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
1111

12+
// This API is not stable and may break in between minor releases.
1213
export interface RustAnalyzerExtensionApi {
1314
readonly client?: lc.LanguageClient;
15+
16+
setWorkspaces(workspaces: JsonProject[]): void;
17+
notifyRustAnalyzer(): Promise<void>;
1418
}
1519

1620
export async function deactivate() {
@@ -152,7 +156,6 @@ function createCommands(): Record<string, CommandFactory> {
152156
shuffleCrateGraph: { enabled: commands.shuffleCrateGraph },
153157
reloadWorkspace: { enabled: commands.reloadWorkspace },
154158
rebuildProcMacros: { enabled: commands.rebuildProcMacros },
155-
addProject: { enabled: commands.addProject },
156159
matchingBrace: { enabled: commands.matchingBrace },
157160
joinLines: { enabled: commands.joinLines },
158161
parentModule: { enabled: commands.parentModule },

0 commit comments

Comments
 (0)