auto update old configurations to newer ones

This commit is contained in:
Lukas Wirth 2022-04-29 13:34:03 +02:00
parent 1f11b70c3b
commit d6dba1c97c
3 changed files with 87 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import * as Is from 'vscode-languageclient/lib/common/utils/is';
import { assert } from './util';
import { WorkspaceEdit } from 'vscode';
import { Workspace } from './ctx';
import { updateConfig } from './config';
export interface Env {
[name: string]: string;
@ -24,7 +25,7 @@ function renderHoverActions(actions: ra.CommandLinkGroup[]): vscode.MarkdownStri
return result;
}
export function createClient(serverPath: string, workspace: Workspace, extraEnv: Env): lc.LanguageClient {
export async function createClient(serverPath: string, workspace: Workspace, extraEnv: Env): Promise<lc.LanguageClient> {
// '.' Is the fallback if no folder is open
// TODO?: Workspace folders support Uri's (eg: file://test.txt).
// It might be a good idea to test if the uri points to a file.
@ -45,6 +46,10 @@ export function createClient(serverPath: string, workspace: Workspace, extraEnv:
);
let initializationOptions = vscode.workspace.getConfiguration("rust-analyzer");
// Update outdated user configs
await updateConfig(initializationOptions);
if (workspace.kind === "Detached Files") {
initializationOptions = { "detachedFiles": workspace.files.map(file => file.uri.fsPath), ...initializationOptions };
}

View File

@ -18,7 +18,6 @@ export class Config {
"cargo",
"procMacro",
"files",
"highlighting",
"lens", // works as lens.*
]
.map(opt => `${this.rootSection}.${opt}`);
@ -79,7 +78,7 @@ export class Config {
* const nullableNum = vscode
* .workspace
* .getConfiguration
* .getConfiguration("rust-analyer")
* .getConfiguration("rust-analyzer")
* .get<number | null>(path)!;
*
* // What happens is that type of `nullableNum` is `number` but not `null | number`:
@ -124,11 +123,11 @@ export class Config {
get hoverActions() {
return {
enable: this.get<boolean>("hoverActions.enable"),
implementations: this.get<boolean>("hoverActions.implementations"),
references: this.get<boolean>("hoverActions.references"),
run: this.get<boolean>("hoverActions.run"),
debug: this.get<boolean>("hoverActions.debug"),
gotoTypeDef: this.get<boolean>("hoverActions.gotoTypeDef"),
implementations: this.get<boolean>("hoverActions.implementations.enable"),
references: this.get<boolean>("hoverActions.references.enable"),
run: this.get<boolean>("hoverActions.run.enable"),
debug: this.get<boolean>("hoverActions.debug.enable"),
gotoTypeDef: this.get<boolean>("hoverActions.gotoTypeDef.enable"),
};
}
@ -136,3 +135,77 @@ export class Config {
return this.package.releaseTag === NIGHTLY_TAG;
}
}
export async function updateConfig(config: vscode.WorkspaceConfiguration) {
const renames = [
["assist.allowMergingIntoGlobImports", "imports.merge.glob",],
["assist.exprFillDefault", "assist.expressionFillDefault",],
["assist.importEnforceGranularity", "imports.granularity.enforce",],
["assist.importGranularity", "imports.granularity.group",],
["assist.importMergeBehavior", "imports.granularity.group",],
["assist.importMergeBehaviour", "imports.granularity.group",],
["assist.importGroup", "imports.group.enable",],
["assist.importPrefix", "imports.prefix",],
["cache.warmup", "primeCaches.enable",],
["cargo.loadOutDirsFromCheck", "cargo.buildScripts.enable",],
["cargo.runBuildScripts", "cargo.runBuildScripts.overrideCommand",],
["cargo.runBuildScriptsCommand", "cargo.runBuildScripts.overrideCommand",],
["cargo.useRustcWrapperForBuildScripts", "cargo.runBuildScripts.useRustcWrapper",],
["completion.snippets", "completion.snippets.custom",],
["diagnostics.enableExperimental", "diagnostics.experimental.enable",],
["experimental.procAttrMacros", "procMacro.attributes.enable",],
["highlighting.strings", "semanticHighlighting.strings.enable",],
["highlightRelated.breakPoints", "highlightRelated.breakPoints.enable",],
["highlightRelated.exitPoints", "highlightRelated.exitPoints.enable",],
["highlightRelated.yieldPoints", "highlightRelated.yieldPoints.enable",],
["highlightRelated.references", "highlightRelated.references.enable",],
["hover.documentation", "hover.documentation.enable",],
["hover.linksInHover", "hover.links.enable",],
["hoverActions.linksInHover", "hover.links.enable",],
["hoverActions.debug", "hoverActions.debug.enable",],
["hoverActions.enable", "hoverActions.enable.enable",],
["hoverActions.gotoTypeDef", "hoverActions.gotoTypeDef.enable",],
["hoverActions.implementations", "hoverActions.implementations.enable",],
["hoverActions.references", "hoverActions.references.enable",],
["hoverActions.run", "hoverActions.run.enable",],
["inlayHints.chainingHints", "inlayHints.chainingHints.enable",],
["inlayHints.closureReturnTypeHints", "inlayHints.closureReturnTypeHints.enable",],
["inlayHints.hideNamedConstructorHints", "inlayHints.typeHints.hideNamedConstructorHints",],
["inlayHints.parameterHints", "inlayHints.parameterHints.enable",],
["inlayHints.reborrowHints", "inlayHints.reborrowHints.enable",],
["inlayHints.typeHints", "inlayHints.typeHints.enable",],
["lruCapacity", "lru.capacity",],
["runnables.cargoExtraArgs", "runnables.extraArgs",],
["runnables.overrideCargo", "runnables.command",],
["rustcSource", "rustc.source",],
["rustfmt.enableRangeFormatting", "rustfmt.rangeFormatting.enable"]
];
for (const [oldKey, newKey] of renames) {
const inspect = config.inspect(oldKey);
if (inspect !== undefined) {
const valMatrix = [
{ val: inspect.globalValue, langVal: inspect.globalLanguageValue, target: vscode.ConfigurationTarget.Global },
{ val: inspect.workspaceFolderValue, langVal: inspect.workspaceFolderLanguageValue, target: vscode.ConfigurationTarget.WorkspaceFolder },
{ val: inspect.workspaceValue, langVal: inspect.workspaceLanguageValue, target: vscode.ConfigurationTarget.Workspace }
];
for (const { val, langVal, target } of valMatrix) {
const pred = (val: unknown) => {
// some of the updates we do only append "enable" or "custom"
// that means on the next run we would find these again, but as objects with
// these properties causing us to destroy the config
// so filter those already updated ones out
return val !== undefined && !(typeof val === "object" && val !== null && (val.hasOwnProperty("enable") || val.hasOwnProperty("custom")));
};
if (pred(val)) {
await config.update(newKey, val, target, false);
await config.update(oldKey, undefined, target, false);
}
if (pred(langVal)) {
await config.update(newKey, langVal, target, true);
await config.update(oldKey, undefined, target, true);
}
}
}
}
}

View File

@ -33,7 +33,7 @@ export class Ctx {
serverPath: string,
workspace: Workspace,
): Promise<Ctx> {
const client = createClient(serverPath, workspace, config.serverExtraEnv);
const client = await createClient(serverPath, workspace, config.serverExtraEnv);
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
extCtx.subscriptions.push(statusBar);