Unnamed repository; edit this file 'description' to name the repository.
Allow other extensions to override the configuration
Michael Gruenewald 6 months ago
parent 410cf55 · commit d1bfff1
-rw-r--r--editors/code/src/config.ts30
-rw-r--r--editors/code/src/ctx.ts6
-rw-r--r--editors/code/src/main.ts2
3 files changed, 31 insertions, 7 deletions
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 06e179eb0e..340f107ebb 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -5,7 +5,7 @@ import * as vscode from "vscode";
import { expectNotUndefined, log, normalizeDriveLetter, unwrapUndefinable } from "./util";
import type { Env } from "./util";
import type { Disposable } from "vscode";
-import { get } from "lodash";
+import { cloneDeep, get, merge } from "lodash";
export type RunnableEnvCfgItem = {
mask?: string;
@@ -23,7 +23,7 @@ export class Config {
configureLang: vscode.Disposable | undefined;
readonly rootSection = "rust-analyzer";
- private readonly requiresServerReloadOpts = ["server", "files", "showSyntaxTree"].map(
+ private readonly requiresServerReloadOpts = ["cargo", "server", "files", "showSyntaxTree"].map(
(opt) => `${this.rootSection}.${opt}`,
);
@@ -31,6 +31,19 @@ export class Config {
(opt) => `${this.rootSection}.${opt}`,
);
+ extensionConfigurations: Map<string, Record<string, unknown>> = new Map();
+
+ async addExtensionConfiguration(extensionId: string, configuration: Record<string, unknown>): Promise<void> {
+ this.extensionConfigurations.set(extensionId, configuration);
+ const prefix = `${this.rootSection}.`;
+ await this.onDidChangeConfiguration({
+ affectsConfiguration(section: string, _scope?: vscode.ConfigurationScope): boolean {
+ // FIXME: questionable
+ return section.startsWith(prefix) && section.slice(prefix.length) in configuration;
+ },
+ });
+ }
+
constructor(disposables: Disposable[]) {
vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, disposables);
this.refreshLogging();
@@ -180,10 +193,15 @@ export class Config {
// We don't do runtime config validation here for simplicity. More on stackoverflow:
// https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension
- private get cfg(): vscode.WorkspaceConfiguration {
+ private get rawCfg(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration(this.rootSection);
}
+ public get cfg(): ConfigurationTree {
+ const vsCodeConfig = cloneDeep<ConfigurationTree>(this.rawCfg);
+ return merge(vsCodeConfig, ...this.extensionConfigurations.values());
+ }
+
/**
* Beware that postfix `!` operator erases both `null` and `undefined`.
* This is why the following doesn't work as expected:
@@ -227,7 +245,7 @@ export class Config {
}
async toggleCheckOnSave() {
- const config = this.cfg.inspect<boolean>("checkOnSave") ?? { key: "checkOnSave" };
+ const config = this.rawCfg.inspect<boolean>("checkOnSave") ?? { key: "checkOnSave" };
let overrideInLanguage;
let target;
let value;
@@ -253,7 +271,7 @@ export class Config {
overrideInLanguage = config.defaultLanguageValue;
value = config.defaultValue || config.defaultLanguageValue;
}
- await this.cfg.update("checkOnSave", !(value || false), target || null, overrideInLanguage);
+ await this.rawCfg.update("checkOnSave", !(value || false), target || null, overrideInLanguage);
}
get problemMatcher(): string[] {
@@ -371,7 +389,7 @@ export class Config {
}
async setAskBeforeUpdateTest(value: boolean) {
- await this.cfg.update("runnables.askBeforeUpdateTest", value, true);
+ await this.rawCfg.update("runnables.askBeforeUpdateTest", value, true);
}
}
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
index e55754fb9f..dfbf5b1e47 100644
--- a/editors/code/src/ctx.ts
+++ b/editors/code/src/ctx.ts
@@ -150,6 +150,10 @@ export class Ctx implements RustAnalyzerExtensionApi {
});
}
+ async addConfiguration(extensionId: string, configuration: Record<string, unknown>): Promise<void> {
+ await this.config.addExtensionConfiguration(extensionId, configuration);
+ }
+
dispose() {
this.config.dispose();
this.statusBar.dispose();
@@ -230,7 +234,7 @@ export class Ctx implements RustAnalyzerExtensionApi {
debug: run,
};
- let rawInitializationOptions = vscode.workspace.getConfiguration("rust-analyzer");
+ let rawInitializationOptions = this.config.cfg;
if (this.workspace.kind === "Detached Files") {
rawInitializationOptions = {
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 996298524f..c126a0a105 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -13,6 +13,8 @@ const RUST_PROJECT_CONTEXT_NAME = "inRustProject";
export interface RustAnalyzerExtensionApi {
// FIXME: this should be non-optional
readonly client?: lc.LanguageClient;
+
+ addConfiguration(extensionId: string, configuration: Record<string, unknown>): Promise<void>;
}
export async function deactivate() {