From bb85dc0cc46a20ba010754ca94f977b245591adf Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 6 Mar 2024 19:10:50 +0100 Subject: [PATCH] fix: Add config and capability for test explorer --- crates/rust-analyzer/src/config.rs | 4 ++++ crates/rust-analyzer/src/main_loop.rs | 7 ++++--- editors/code/package.json | 5 +++++ editors/code/src/client.ts | 8 +++++++- editors/code/src/config.ts | 4 ++++ editors/code/src/ctx.ts | 20 ++++++++++++-------- 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 9f50da4dc880..4d0b01daf2a7 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -1146,6 +1146,10 @@ impl Config { self.experimental("colorDiagnosticOutput") } + pub fn test_discovery(&self) -> bool { + self.experimental("testDiscovery") + } + pub fn publish_diagnostics(&self) -> bool { self.data.diagnostics_enable } diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 87da95c87e32..606b5e9bb741 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -386,10 +386,11 @@ impl GlobalState { } } - let update_diagnostics = (!was_quiescent || state_changed || memdocs_added_or_removed) - && self.config.publish_diagnostics(); - if update_diagnostics { + let ready = (!was_quiescent || state_changed || memdocs_added_or_removed); + if ready && self.config.publish_diagnostics() { self.update_diagnostics(); + } + if ready && self.config.test_discovery() { self.update_tests(); } } diff --git a/editors/code/package.json b/editors/code/package.json index 16d3dcbab6fe..c34b8e25de02 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -510,6 +510,11 @@ "default": true, "type": "boolean" }, + "rust-analyzer.testExplorer": { + "markdownDescription": "Whether to show the test explorer.", + "default": false, + "type": "boolean" + }, "$generated-start": {}, "rust-analyzer.assist.emitMustUse": { "markdownDescription": "Whether to insert #[must_use] when generating `as_` methods\nfor enum variants.", diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index c27a446b3804..1cbf247297ff 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -372,13 +372,18 @@ export async function createClient( ); // To turn on all proposed features use: client.registerProposedFeatures(); - client.registerFeature(new ExperimentalFeatures()); + client.registerFeature(new ExperimentalFeatures(config)); client.registerFeature(new OverrideFeatures()); return client; } class ExperimentalFeatures implements lc.StaticFeature { + private readonly testExplorer: boolean; + + constructor(config: Config) { + this.testExplorer = config.testExplorer || false; + } getState(): lc.FeatureState { return { kind: "static" }; } @@ -391,6 +396,7 @@ class ExperimentalFeatures implements lc.StaticFeature { colorDiagnosticOutput: true, openServerLogs: true, localDocs: true, + testExplorer: this.testExplorer, commands: { commands: [ "rust-analyzer.runSingle", diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 51a0aece820e..92a816bfbcb7 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -266,6 +266,10 @@ export class Config { return this.get("cargoRunner"); } + get testExplorer() { + return this.get("testExplorer"); + } + get runnablesExtraEnv() { const item = this.get("runnables.extraEnv") ?? this.get("runnableEnv"); if (!item) return item; diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 9be846f96924..f76dec2629ac 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -75,7 +75,7 @@ export class Ctx implements RustAnalyzerExtensionApi { private _client: lc.LanguageClient | undefined; private _serverPath: string | undefined; private traceOutputChannel: vscode.OutputChannel | undefined; - private testController: vscode.TestController; + private testController: vscode.TestController | undefined; private outputChannel: vscode.OutputChannel | undefined; private clientSubscriptions: Disposable[]; private state: PersistentState; @@ -104,18 +104,20 @@ export class Ctx implements RustAnalyzerExtensionApi { workspace: Workspace, ) { extCtx.subscriptions.push(this); + this.config = new Config(extCtx); this.statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left); - this.testController = vscode.tests.createTestController( - "rustAnalyzerTestController", - "Rust Analyzer test controller", - ); + if (this.config.testExplorer) { + this.testController = vscode.tests.createTestController( + "rustAnalyzerTestController", + "Rust Analyzer test controller", + ); + } this.workspace = workspace; this.clientSubscriptions = []; this.commandDisposables = []; this.commandFactories = commandFactories; this.unlinkedFiles = []; this.state = new PersistentState(extCtx.globalState); - this.config = new Config(extCtx); this.updateCommands("disable"); this.setServerStatus({ @@ -126,7 +128,7 @@ export class Ctx implements RustAnalyzerExtensionApi { dispose() { this.config.dispose(); this.statusBar.dispose(); - this.testController.dispose(); + this.testController?.dispose(); void this.disposeClient(); this.commandDisposables.forEach((disposable) => disposable.dispose()); } @@ -271,7 +273,9 @@ export class Ctx implements RustAnalyzerExtensionApi { await client.start(); this.updateCommands(); - prepareTestExplorer(this, this.testController, client); + if (this.testController) { + prepareTestExplorer(this, this.testController, client); + } if (this.config.showDependenciesExplorer) { this.prepareTreeDependenciesView(client); }