Skip to content

Commit

Permalink
auto update old configurations to newer ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Apr 29, 2022
1 parent 1c21b14 commit a06d229
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 9 deletions.
7 changes: 6 additions & 1 deletion editors/code/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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 };
}
Expand Down
87 changes: 80 additions & 7 deletions editors/code/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class Config {
"cargo",
"procMacro",
"files",
"highlighting",
"lens", // works as lens.*
]
.map(opt => `${this.rootSection}.${opt}`);
Expand Down Expand Up @@ -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`:
Expand Down Expand Up @@ -124,15 +123,89 @@ 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"),
};
}

get currentExtensionIsNightly() {
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);
}
}
}
}
}
2 changes: 1 addition & 1 deletion editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a06d229

Please sign in to comment.