Skip to content

Commit a06d229

Browse files
committed
auto update old configurations to newer ones
1 parent 1c21b14 commit a06d229

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed

editors/code/src/client.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Is from 'vscode-languageclient/lib/common/utils/is';
55
import { assert } from './util';
66
import { WorkspaceEdit } from 'vscode';
77
import { Workspace } from './ctx';
8+
import { updateConfig } from './config';
89

910
export interface Env {
1011
[name: string]: string;
@@ -24,7 +25,7 @@ function renderHoverActions(actions: ra.CommandLinkGroup[]): vscode.MarkdownStri
2425
return result;
2526
}
2627

27-
export function createClient(serverPath: string, workspace: Workspace, extraEnv: Env): lc.LanguageClient {
28+
export async function createClient(serverPath: string, workspace: Workspace, extraEnv: Env): Promise<lc.LanguageClient> {
2829
// '.' Is the fallback if no folder is open
2930
// TODO?: Workspace folders support Uri's (eg: file://test.txt).
3031
// 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:
4546
);
4647

4748
let initializationOptions = vscode.workspace.getConfiguration("rust-analyzer");
49+
50+
// Update outdated user configs
51+
await updateConfig(initializationOptions);
52+
4853
if (workspace.kind === "Detached Files") {
4954
initializationOptions = { "detachedFiles": workspace.files.map(file => file.uri.fsPath), ...initializationOptions };
5055
}

editors/code/src/config.ts

+80-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export class Config {
1818
"cargo",
1919
"procMacro",
2020
"files",
21-
"highlighting",
2221
"lens", // works as lens.*
2322
]
2423
.map(opt => `${this.rootSection}.${opt}`);
@@ -79,7 +78,7 @@ export class Config {
7978
* const nullableNum = vscode
8079
* .workspace
8180
* .getConfiguration
82-
* .getConfiguration("rust-analyer")
81+
* .getConfiguration("rust-analyzer")
8382
* .get<number | null>(path)!;
8483
*
8584
* // What happens is that type of `nullableNum` is `number` but not `null | number`:
@@ -124,15 +123,89 @@ export class Config {
124123
get hoverActions() {
125124
return {
126125
enable: this.get<boolean>("hoverActions.enable"),
127-
implementations: this.get<boolean>("hoverActions.implementations"),
128-
references: this.get<boolean>("hoverActions.references"),
129-
run: this.get<boolean>("hoverActions.run"),
130-
debug: this.get<boolean>("hoverActions.debug"),
131-
gotoTypeDef: this.get<boolean>("hoverActions.gotoTypeDef"),
126+
implementations: this.get<boolean>("hoverActions.implementations.enable"),
127+
references: this.get<boolean>("hoverActions.references.enable"),
128+
run: this.get<boolean>("hoverActions.run.enable"),
129+
debug: this.get<boolean>("hoverActions.debug.enable"),
130+
gotoTypeDef: this.get<boolean>("hoverActions.gotoTypeDef.enable"),
132131
};
133132
}
134133

135134
get currentExtensionIsNightly() {
136135
return this.package.releaseTag === NIGHTLY_TAG;
137136
}
138137
}
138+
139+
export async function updateConfig(config: vscode.WorkspaceConfiguration) {
140+
const renames = [
141+
["assist.allowMergingIntoGlobImports", "imports.merge.glob",],
142+
["assist.exprFillDefault", "assist.expressionFillDefault",],
143+
["assist.importEnforceGranularity", "imports.granularity.enforce",],
144+
["assist.importGranularity", "imports.granularity.group",],
145+
["assist.importMergeBehavior", "imports.granularity.group",],
146+
["assist.importMergeBehaviour", "imports.granularity.group",],
147+
["assist.importGroup", "imports.group.enable",],
148+
["assist.importPrefix", "imports.prefix",],
149+
["cache.warmup", "primeCaches.enable",],
150+
["cargo.loadOutDirsFromCheck", "cargo.buildScripts.enable",],
151+
["cargo.runBuildScripts", "cargo.runBuildScripts.overrideCommand",],
152+
["cargo.runBuildScriptsCommand", "cargo.runBuildScripts.overrideCommand",],
153+
["cargo.useRustcWrapperForBuildScripts", "cargo.runBuildScripts.useRustcWrapper",],
154+
["completion.snippets", "completion.snippets.custom",],
155+
["diagnostics.enableExperimental", "diagnostics.experimental.enable",],
156+
["experimental.procAttrMacros", "procMacro.attributes.enable",],
157+
["highlighting.strings", "semanticHighlighting.strings.enable",],
158+
["highlightRelated.breakPoints", "highlightRelated.breakPoints.enable",],
159+
["highlightRelated.exitPoints", "highlightRelated.exitPoints.enable",],
160+
["highlightRelated.yieldPoints", "highlightRelated.yieldPoints.enable",],
161+
["highlightRelated.references", "highlightRelated.references.enable",],
162+
["hover.documentation", "hover.documentation.enable",],
163+
["hover.linksInHover", "hover.links.enable",],
164+
["hoverActions.linksInHover", "hover.links.enable",],
165+
["hoverActions.debug", "hoverActions.debug.enable",],
166+
["hoverActions.enable", "hoverActions.enable.enable",],
167+
["hoverActions.gotoTypeDef", "hoverActions.gotoTypeDef.enable",],
168+
["hoverActions.implementations", "hoverActions.implementations.enable",],
169+
["hoverActions.references", "hoverActions.references.enable",],
170+
["hoverActions.run", "hoverActions.run.enable",],
171+
["inlayHints.chainingHints", "inlayHints.chainingHints.enable",],
172+
["inlayHints.closureReturnTypeHints", "inlayHints.closureReturnTypeHints.enable",],
173+
["inlayHints.hideNamedConstructorHints", "inlayHints.typeHints.hideNamedConstructorHints",],
174+
["inlayHints.parameterHints", "inlayHints.parameterHints.enable",],
175+
["inlayHints.reborrowHints", "inlayHints.reborrowHints.enable",],
176+
["inlayHints.typeHints", "inlayHints.typeHints.enable",],
177+
["lruCapacity", "lru.capacity",],
178+
["runnables.cargoExtraArgs", "runnables.extraArgs",],
179+
["runnables.overrideCargo", "runnables.command",],
180+
["rustcSource", "rustc.source",],
181+
["rustfmt.enableRangeFormatting", "rustfmt.rangeFormatting.enable"]
182+
];
183+
184+
for (const [oldKey, newKey] of renames) {
185+
const inspect = config.inspect(oldKey);
186+
if (inspect !== undefined) {
187+
const valMatrix = [
188+
{ val: inspect.globalValue, langVal: inspect.globalLanguageValue, target: vscode.ConfigurationTarget.Global },
189+
{ val: inspect.workspaceFolderValue, langVal: inspect.workspaceFolderLanguageValue, target: vscode.ConfigurationTarget.WorkspaceFolder },
190+
{ val: inspect.workspaceValue, langVal: inspect.workspaceLanguageValue, target: vscode.ConfigurationTarget.Workspace }
191+
];
192+
for (const { val, langVal, target } of valMatrix) {
193+
const pred = (val: unknown) => {
194+
// some of the updates we do only append "enable" or "custom"
195+
// that means on the next run we would find these again, but as objects with
196+
// these properties causing us to destroy the config
197+
// so filter those already updated ones out
198+
return val !== undefined && !(typeof val === "object" && val !== null && (val.hasOwnProperty("enable") || val.hasOwnProperty("custom")));
199+
};
200+
if (pred(val)) {
201+
await config.update(newKey, val, target, false);
202+
await config.update(oldKey, undefined, target, false);
203+
}
204+
if (pred(langVal)) {
205+
await config.update(newKey, langVal, target, true);
206+
await config.update(oldKey, undefined, target, true);
207+
}
208+
}
209+
}
210+
}
211+
}

editors/code/src/ctx.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Ctx {
3333
serverPath: string,
3434
workspace: Workspace,
3535
): Promise<Ctx> {
36-
const client = createClient(serverPath, workspace, config.serverExtraEnv);
36+
const client = await createClient(serverPath, workspace, config.serverExtraEnv);
3737

3838
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
3939
extCtx.subscriptions.push(statusBar);

0 commit comments

Comments
 (0)