From d6dba1c97ca6c9a529e7dfbb17320e3e59713aef Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 29 Apr 2022 13:34:03 +0200 Subject: [PATCH] auto update old configurations to newer ones --- editors/code/src/client.ts | 7 ++- editors/code/src/config.ts | 87 +++++++++++++++++++++++++++++++++++--- editors/code/src/ctx.ts | 2 +- 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index f7520f6c436..99b72635d18 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -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 { // '.' 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 }; } diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 183521c10c5..5b650e1420a 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -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(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("hoverActions.enable"), - implementations: this.get("hoverActions.implementations"), - references: this.get("hoverActions.references"), - run: this.get("hoverActions.run"), - debug: this.get("hoverActions.debug"), - gotoTypeDef: this.get("hoverActions.gotoTypeDef"), + implementations: this.get("hoverActions.implementations.enable"), + references: this.get("hoverActions.references.enable"), + run: this.get("hoverActions.run.enable"), + debug: this.get("hoverActions.debug.enable"), + gotoTypeDef: this.get("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); + } + } + } + } +} diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 666a4b6972b..0c3e6810e99 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -33,7 +33,7 @@ export class Ctx { serverPath: string, workspace: Workspace, ): Promise { - 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);