Skip to content

Commit 1d6024e

Browse files
bors[bot]Veetaha
andauthored
Merge #3695
3695: vscode: simplify and refactor config r=matklad a=Veetaha Removed unnecessary interfaces, changed `cfg` to be a getter to ensure the fresh values any time possible. Migrated from explicit casts to implicit. Co-authored-by: veetaha <veetaha2@gmail.com>
2 parents c6db6e2 + 65e2d8a commit 1d6024e

File tree

1 file changed

+45
-61
lines changed

1 file changed

+45
-61
lines changed

editors/code/src/config.ts

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
import * as vscode from 'vscode';
22
import { log } from "./util";
33

4-
export interface InlayHintOptions {
5-
typeHints: boolean;
6-
parameterHints: boolean;
7-
maxLength: number | null;
8-
}
9-
10-
export interface CargoWatchOptions {
11-
enable: boolean;
12-
arguments: string[];
13-
command: string;
14-
allTargets: boolean;
15-
}
16-
17-
export interface CargoFeatures {
18-
noDefaultFeatures: boolean;
19-
allFeatures: boolean;
20-
features: string[];
21-
loadOutDirsFromCheck: boolean;
22-
}
23-
244
export type UpdatesChannel = "stable" | "nightly";
255

266
export const NIGHTLY_TAG = "nightly";
7+
278
export class Config {
289
readonly extensionId = "matklad.rust-analyzer";
2910

@@ -44,25 +25,24 @@ export class Config {
4425
enableProposedApi: boolean | undefined;
4526
} = vscode.extensions.getExtension(this.extensionId)!.packageJSON;
4627

47-
private cfg!: vscode.WorkspaceConfiguration;
28+
readonly globalStoragePath: string;
4829

49-
constructor(private readonly ctx: vscode.ExtensionContext) {
50-
vscode.workspace.onDidChangeConfiguration(this.onConfigChange, this, ctx.subscriptions);
51-
this.refreshConfig();
30+
constructor(ctx: vscode.ExtensionContext) {
31+
this.globalStoragePath = ctx.globalStoragePath;
32+
vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
33+
this.refreshLogging();
5234
}
5335

54-
private refreshConfig() {
55-
this.cfg = vscode.workspace.getConfiguration(this.rootSection);
56-
const enableLogging = this.cfg.get("trace.extension") as boolean;
57-
log.setEnabled(enableLogging);
36+
private refreshLogging() {
37+
log.setEnabled(this.traceExtension);
5838
log.debug(
5939
"Extension version:", this.package.version,
6040
"using configuration:", this.cfg
6141
);
6242
}
6343

64-
private async onConfigChange(event: vscode.ConfigurationChangeEvent) {
65-
this.refreshConfig();
44+
private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
45+
this.refreshLogging();
6646

6747
const requiresReloadOpt = this.requiresReloadOpts.find(
6848
opt => event.affectsConfiguration(opt)
@@ -80,49 +60,53 @@ export class Config {
8060
}
8161
}
8262

83-
get globalStoragePath(): string { return this.ctx.globalStoragePath; }
84-
8563
// We don't do runtime config validation here for simplicity. More on stackoverflow:
8664
// https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension
8765

88-
get serverPath() { return this.cfg.get("serverPath") as null | string; }
89-
get channel() { return this.cfg.get<"stable" | "nightly">("updates.channel")!; }
90-
get askBeforeDownload() { return this.cfg.get("updates.askBeforeDownload") as boolean; }
91-
get highlightingSemanticTokens() { return this.cfg.get("highlighting.semanticTokens") as boolean; }
92-
get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; }
93-
get rainbowHighlightingOn() { return this.cfg.get("rainbowHighlightingOn") as boolean; }
94-
get lruCapacity() { return this.cfg.get("lruCapacity") as null | number; }
95-
get inlayHints(): InlayHintOptions {
66+
private get cfg(): vscode.WorkspaceConfiguration {
67+
return vscode.workspace.getConfiguration(this.rootSection);
68+
}
69+
70+
get serverPath() { return this.cfg.get<null | string>("serverPath")!; }
71+
get channel() { return this.cfg.get<UpdatesChannel>("updates.channel")!; }
72+
get askBeforeDownload() { return this.cfg.get<boolean>("updates.askBeforeDownload")!; }
73+
get highlightingSemanticTokens() { return this.cfg.get<boolean>("highlighting.semanticTokens")!; }
74+
get highlightingOn() { return this.cfg.get<boolean>("highlightingOn")!; }
75+
get rainbowHighlightingOn() { return this.cfg.get<boolean>("rainbowHighlightingOn")!; }
76+
get lruCapacity() { return this.cfg.get<null | number>("lruCapacity")!; }
77+
get excludeGlobs() { return this.cfg.get<string[]>("excludeGlobs")!; }
78+
get useClientWatching() { return this.cfg.get<boolean>("useClientWatching")!; }
79+
get featureFlags() { return this.cfg.get<Record<string, boolean>>("featureFlags")!; }
80+
get rustfmtArgs() { return this.cfg.get<string[]>("rustfmtArgs")!; }
81+
get loadOutDirsFromCheck() { return this.cfg.get<boolean>("loadOutDirsFromCheck")!; }
82+
get traceExtension() { return this.cfg.get<boolean>("trace.extension")!; }
83+
84+
// for internal use
85+
get withSysroot() { return this.cfg.get<boolean>("withSysroot", true)!; }
86+
87+
get inlayHints() {
9688
return {
97-
typeHints: this.cfg.get("inlayHints.typeHints") as boolean,
98-
parameterHints: this.cfg.get("inlayHints.parameterHints") as boolean,
99-
maxLength: this.cfg.get("inlayHints.maxLength") as null | number,
89+
typeHints: this.cfg.get<boolean>("inlayHints.typeHints")!,
90+
parameterHints: this.cfg.get<boolean>("inlayHints.parameterHints")!,
91+
maxLength: this.cfg.get<null | number>("inlayHints.maxLength")!,
10092
};
10193
}
102-
get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; }
103-
get useClientWatching() { return this.cfg.get("useClientWatching") as boolean; }
104-
get featureFlags() { return this.cfg.get("featureFlags") as Record<string, boolean>; }
105-
get rustfmtArgs() { return this.cfg.get("rustfmtArgs") as string[]; }
106-
get loadOutDirsFromCheck() { return this.cfg.get("loadOutDirsFromCheck") as boolean; }
10794

108-
get cargoWatchOptions(): CargoWatchOptions {
95+
get cargoWatchOptions() {
10996
return {
110-
enable: this.cfg.get("cargo-watch.enable") as boolean,
111-
arguments: this.cfg.get("cargo-watch.arguments") as string[],
112-
allTargets: this.cfg.get("cargo-watch.allTargets") as boolean,
113-
command: this.cfg.get("cargo-watch.command") as string,
97+
enable: this.cfg.get<boolean>("cargo-watch.enable")!,
98+
arguments: this.cfg.get<string[]>("cargo-watch.arguments")!,
99+
allTargets: this.cfg.get<boolean>("cargo-watch.allTargets")!,
100+
command: this.cfg.get<string>("cargo-watch.command")!,
114101
};
115102
}
116103

117-
get cargoFeatures(): CargoFeatures {
104+
get cargoFeatures() {
118105
return {
119-
noDefaultFeatures: this.cfg.get("cargoFeatures.noDefaultFeatures") as boolean,
120-
allFeatures: this.cfg.get("cargoFeatures.allFeatures") as boolean,
121-
features: this.cfg.get("cargoFeatures.features") as string[],
122-
loadOutDirsFromCheck: this.cfg.get("cargoFeatures.loadOutDirsFromCheck") as boolean,
106+
noDefaultFeatures: this.cfg.get<boolean>("cargoFeatures.noDefaultFeatures")!,
107+
allFeatures: this.cfg.get<boolean>("cargoFeatures.allFeatures")!,
108+
features: this.cfg.get<string[]>("cargoFeatures.features")!,
109+
loadOutDirsFromCheck: this.cfg.get<boolean>("cargoFeatures.loadOutDirsFromCheck")!,
123110
};
124111
}
125-
126-
// for internal use
127-
get withSysroot() { return this.cfg.get("withSysroot", true) as boolean; }
128112
}

0 commit comments

Comments
 (0)