mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2026-04-29 23:14:49 +00:00
Allow other extensions to override the configuration
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user