aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-02-14 21:08:47 +0000
committerGitHub <[email protected]>2020-02-14 21:08:47 +0000
commitab42174653ae42f8d862f28ef802bf1312be1694 (patch)
tree0d3bf6a0d6712263e2ec4da30914ad3be37e8387
parentb2b94cbf71738e7bc372328f22c8dcc8c734b34c (diff)
parent20fabaf1eeec32e1115627a5552eddf0c074ce37 (diff)
Merge #3131
3131: vscode: simplified config and to removed one source of truth of default values r=matklad a=Veetaha Though not intended initially, the implementation of config design is alike [dart's one](https://github.com/Dart-Code/Dart-Code/blob/master/src/extension/config.ts) as pointed by @matklad in PM. Co-authored-by: Veetaha <[email protected]>
-rw-r--r--editors/code/package.json10
-rw-r--r--editors/code/src/client.ts22
-rw-r--r--editors/code/src/config.ts271
-rw-r--r--editors/code/src/status_display.ts4
4 files changed, 96 insertions, 211 deletions
diff --git a/editors/code/package.json b/editors/code/package.json
index db1fe5189..a607c2148 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -182,6 +182,9 @@
182 }, 182 },
183 "rust-analyzer.excludeGlobs": { 183 "rust-analyzer.excludeGlobs": {
184 "type": "array", 184 "type": "array",
185 "items": {
186 "type": "string"
187 },
185 "default": [], 188 "default": [],
186 "description": "Paths to exclude from analysis" 189 "description": "Paths to exclude from analysis"
187 }, 190 },
@@ -197,6 +200,9 @@
197 }, 200 },
198 "rust-analyzer.cargo-watch.arguments": { 201 "rust-analyzer.cargo-watch.arguments": {
199 "type": "array", 202 "type": "array",
203 "items": {
204 "type": "string"
205 },
200 "description": "`cargo-watch` arguments. (e.g: `--features=\"shumway,pdf\"` will run as `cargo watch -x \"check --features=\"shumway,pdf\"\"` )", 206 "description": "`cargo-watch` arguments. (e.g: `--features=\"shumway,pdf\"` will run as `cargo watch -x \"check --features=\"shumway,pdf\"\"` )",
201 "default": [] 207 "default": []
202 }, 208 },
@@ -242,6 +248,7 @@
242 "rust-analyzer.maxInlayHintLength": { 248 "rust-analyzer.maxInlayHintLength": {
243 "type": "number", 249 "type": "number",
244 "default": 20, 250 "default": 20,
251 "exclusiveMinimum": 0,
245 "description": "Maximum length for inlay hints" 252 "description": "Maximum length for inlay hints"
246 }, 253 },
247 "rust-analyzer.cargoFeatures.noDefaultFeatures": { 254 "rust-analyzer.cargoFeatures.noDefaultFeatures": {
@@ -256,6 +263,9 @@
256 }, 263 },
257 "rust-analyzer.cargoFeatures.features": { 264 "rust-analyzer.cargoFeatures.features": {
258 "type": "array", 265 "type": "array",
266 "items": {
267 "type": "string"
268 },
259 "default": [], 269 "default": [],
260 "description": "List of features to activate" 270 "description": "List of features to activate"
261 } 271 }
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts
index d2759969b..dcf9d0c06 100644
--- a/editors/code/src/client.ts
+++ b/editors/code/src/client.ts
@@ -1,6 +1,6 @@
1import * as lc from 'vscode-languageclient'; 1import * as lc from 'vscode-languageclient';
2import * as vscode from 'vscode';
2 3
3import { window, workspace } from 'vscode';
4import { Config } from './config'; 4import { Config } from './config';
5import { ensureLanguageServerBinary } from './installation/language_server'; 5import { ensureLanguageServerBinary } from './installation/language_server';
6import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed'; 6import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed';
@@ -9,32 +9,34 @@ export async function createClient(config: Config): Promise<null | lc.LanguageCl
9 // '.' Is the fallback if no folder is open 9 // '.' Is the fallback if no folder is open
10 // TODO?: Workspace folders support Uri's (eg: file://test.txt). 10 // TODO?: Workspace folders support Uri's (eg: file://test.txt).
11 // It might be a good idea to test if the uri points to a file. 11 // It might be a good idea to test if the uri points to a file.
12 const workspaceFolderPath = workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.'; 12 const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.';
13 13
14 const raLspServerPath = await ensureLanguageServerBinary(config.langServerSource); 14 const langServerPath = await ensureLanguageServerBinary(config.langServerBinarySource);
15 if (!raLspServerPath) return null; 15 if (!langServerPath) return null;
16 16
17 const run: lc.Executable = { 17 const run: lc.Executable = {
18 command: raLspServerPath, 18 command: langServerPath,
19 options: { cwd: workspaceFolderPath }, 19 options: { cwd: workspaceFolderPath },
20 }; 20 };
21 const serverOptions: lc.ServerOptions = { 21 const serverOptions: lc.ServerOptions = {
22 run, 22 run,
23 debug: run, 23 debug: run,
24 }; 24 };
25 const traceOutputChannel = window.createOutputChannel( 25 const traceOutputChannel = vscode.window.createOutputChannel(
26 'Rust Analyzer Language Server Trace', 26 'Rust Analyzer Language Server Trace',
27 ); 27 );
28 const cargoWatchOpts = config.cargoWatchOptions;
29
28 const clientOptions: lc.LanguageClientOptions = { 30 const clientOptions: lc.LanguageClientOptions = {
29 documentSelector: [{ scheme: 'file', language: 'rust' }], 31 documentSelector: [{ scheme: 'file', language: 'rust' }],
30 initializationOptions: { 32 initializationOptions: {
31 publishDecorations: true, 33 publishDecorations: true,
32 lruCapacity: config.lruCapacity, 34 lruCapacity: config.lruCapacity,
33 maxInlayHintLength: config.maxInlayHintLength, 35 maxInlayHintLength: config.maxInlayHintLength,
34 cargoWatchEnable: config.cargoWatchOptions.enable, 36 cargoWatchEnable: cargoWatchOpts.enable,
35 cargoWatchArgs: config.cargoWatchOptions.arguments, 37 cargoWatchArgs: cargoWatchOpts.arguments,
36 cargoWatchCommand: config.cargoWatchOptions.command, 38 cargoWatchCommand: cargoWatchOpts.command,
37 cargoWatchAllTargets: config.cargoWatchOptions.allTargets, 39 cargoWatchAllTargets: cargoWatchOpts.allTargets,
38 excludeGlobs: config.excludeGlobs, 40 excludeGlobs: config.excludeGlobs,
39 useClientWatching: config.useClientWatching, 41 useClientWatching: config.useClientWatching,
40 featureFlags: config.featureFlags, 42 featureFlags: config.featureFlags,
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 418845436..8cd89e119 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -16,45 +16,49 @@ export interface CargoFeatures {
16 allFeatures: boolean; 16 allFeatures: boolean;
17 features: string[]; 17 features: string[];
18} 18}
19
20export class Config { 19export class Config {
21 langServerSource!: null | BinarySource; 20 private static readonly rootSection = "rust-analyzer";
21 private static readonly requiresReloadOpts = [
22 "cargoFeatures",
23 "cargo-watch",
24 ]
25 .map(opt => `${Config.rootSection}.${opt}`);
26
27 private cfg!: vscode.WorkspaceConfiguration;
28
29 constructor(private readonly ctx: vscode.ExtensionContext) {
30 vscode.workspace.onDidChangeConfiguration(this.onConfigChange, this, ctx.subscriptions);
31 this.refreshConfig();
32 }
22 33
23 highlightingOn = true;
24 rainbowHighlightingOn = false;
25 enableEnhancedTyping = true;
26 lruCapacity: null | number = null;
27 displayInlayHints = true;
28 maxInlayHintLength: null | number = null;
29 excludeGlobs: string[] = [];
30 useClientWatching = true;
31 featureFlags: Record<string, boolean> = {};
32 // for internal use
33 withSysroot: null | boolean = null;
34 cargoWatchOptions: CargoWatchOptions = {
35 enable: true,
36 arguments: [],
37 command: '',
38 allTargets: true,
39 };
40 cargoFeatures: CargoFeatures = {
41 noDefaultFeatures: false,
42 allFeatures: true,
43 features: [],
44 };
45 34
46 private prevEnhancedTyping: null | boolean = null; 35 private refreshConfig() {
47 private prevCargoFeatures: null | CargoFeatures = null; 36 this.cfg = vscode.workspace.getConfiguration(Config.rootSection);
48 private prevCargoWatchOptions: null | CargoWatchOptions = null; 37 console.log("Using configuration:", this.cfg);
38 }
39
40 private async onConfigChange(event: vscode.ConfigurationChangeEvent) {
41 this.refreshConfig();
42
43 const requiresReloadOpt = Config.requiresReloadOpts.find(
44 opt => event.affectsConfiguration(opt)
45 );
46
47 if (!requiresReloadOpt) return;
49 48
50 constructor(ctx: vscode.ExtensionContext) { 49 const userResponse = await vscode.window.showInformationMessage(
51 vscode.workspace.onDidChangeConfiguration(_ => this.refresh(ctx), null, ctx.subscriptions); 50 `Changing "${requiresReloadOpt}" requires a reload`,
52 this.refresh(ctx); 51 "Reload now"
52 );
53
54 if (userResponse === "Reload now") {
55 vscode.commands.executeCommand("workbench.action.reloadWindow");
56 }
53 } 57 }
54 58
55 private static expandPathResolving(path: string) { 59 private static replaceTildeWithHomeDir(path: string) {
56 if (path.startsWith('~/')) { 60 if (path.startsWith("~/")) {
57 return path.replace('~', os.homedir()); 61 return os.homedir() + path.slice("~".length);
58 } 62 }
59 return path; 63 return path;
60 } 64 }
@@ -64,17 +68,14 @@ export class Config {
64 * `platform` on GitHub releases. (It is also stored under the same name when 68 * `platform` on GitHub releases. (It is also stored under the same name when
65 * downloaded by the extension). 69 * downloaded by the extension).
66 */ 70 */
67 private static prebuiltLangServerFileName( 71 get prebuiltLangServerFileName(): null | string {
68 platform: NodeJS.Platform,
69 arch: string
70 ): null | string {
71 // See possible `arch` values here: 72 // See possible `arch` values here:
72 // https://nodejs.org/api/process.html#process_process_arch 73 // https://nodejs.org/api/process.html#process_process_arch
73 74
74 switch (platform) { 75 switch (process.platform) {
75 76
76 case "linux": { 77 case "linux": {
77 switch (arch) { 78 switch (process.arch) {
78 case "arm": 79 case "arm":
79 case "arm64": return null; 80 case "arm64": return null;
80 81
@@ -97,28 +98,23 @@ export class Config {
97 } 98 }
98 } 99 }
99 100
100 private static langServerBinarySource( 101 get langServerBinarySource(): null | BinarySource {
101 ctx: vscode.ExtensionContext, 102 const langServerPath = RA_LSP_DEBUG ?? this.cfg.get<null | string>("raLspServerPath");
102 config: vscode.WorkspaceConfiguration
103 ): null | BinarySource {
104 const langServerPath = RA_LSP_DEBUG ?? config.get<null | string>("raLspServerPath");
105 103
106 if (langServerPath) { 104 if (langServerPath) {
107 return { 105 return {
108 type: BinarySource.Type.ExplicitPath, 106 type: BinarySource.Type.ExplicitPath,
109 path: Config.expandPathResolving(langServerPath) 107 path: Config.replaceTildeWithHomeDir(langServerPath)
110 }; 108 };
111 } 109 }
112 110
113 const prebuiltBinaryName = Config.prebuiltLangServerFileName( 111 const prebuiltBinaryName = this.prebuiltLangServerFileName;
114 process.platform, process.arch
115 );
116 112
117 if (!prebuiltBinaryName) return null; 113 if (!prebuiltBinaryName) return null;
118 114
119 return { 115 return {
120 type: BinarySource.Type.GithubRelease, 116 type: BinarySource.Type.GithubRelease,
121 dir: ctx.globalStoragePath, 117 dir: this.ctx.globalStoragePath,
122 file: prebuiltBinaryName, 118 file: prebuiltBinaryName,
123 repo: { 119 repo: {
124 name: "rust-analyzer", 120 name: "rust-analyzer",
@@ -127,158 +123,35 @@ export class Config {
127 }; 123 };
128 } 124 }
129 125
126 // We don't do runtime config validation here for simplicity. More on stackoverflow:
127 // https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension
130 128
131 // FIXME: revisit the logic for `if (.has(...)) config.get(...)` set default 129 get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; }
132 // values only in one place (i.e. remove default values from non-readonly members declarations) 130 get rainbowHighlightingOn() { return this.cfg.get("rainbowHighlightingOn") as boolean; }
133 private refresh(ctx: vscode.ExtensionContext) { 131 get lruCapacity() { return this.cfg.get("lruCapacity") as null | number; }
134 const config = vscode.workspace.getConfiguration('rust-analyzer'); 132 get displayInlayHints() { return this.cfg.get("displayInlayHints") as boolean; }
135 133 get maxInlayHintLength() { return this.cfg.get("maxInlayHintLength") as number; }
136 let requireReloadMessage = null; 134 get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; }
137 135 get useClientWatching() { return this.cfg.get("useClientWatching") as boolean; }
138 if (config.has('highlightingOn')) { 136 get featureFlags() { return this.cfg.get("featureFlags") as Record<string, boolean>; }
139 this.highlightingOn = config.get('highlightingOn') as boolean;
140 }
141 137
142 if (config.has('rainbowHighlightingOn')) { 138 get cargoWatchOptions(): CargoWatchOptions {
143 this.rainbowHighlightingOn = config.get( 139 return {
144 'rainbowHighlightingOn', 140 enable: this.cfg.get("cargo-watch.enable") as boolean,
145 ) as boolean; 141 arguments: this.cfg.get("cargo-watch.arguments") as string[],
146 } 142 allTargets: this.cfg.get("cargo-watch.allTargets") as boolean,
147 143 command: this.cfg.get("cargo-watch.command") as string,
148 if (config.has('enableEnhancedTyping')) { 144 };
149 this.enableEnhancedTyping = config.get( 145 }
150 'enableEnhancedTyping',
151 ) as boolean;
152
153 if (this.prevEnhancedTyping === null) {
154 this.prevEnhancedTyping = this.enableEnhancedTyping;
155 }
156 } else if (this.prevEnhancedTyping === null) {
157 this.prevEnhancedTyping = this.enableEnhancedTyping;
158 }
159
160 if (this.prevEnhancedTyping !== this.enableEnhancedTyping) {
161 requireReloadMessage =
162 'Changing enhanced typing setting requires a reload';
163 this.prevEnhancedTyping = this.enableEnhancedTyping;
164 }
165
166 this.langServerSource = Config.langServerBinarySource(ctx, config);
167
168 if (config.has('cargo-watch.enable')) {
169 this.cargoWatchOptions.enable = config.get<boolean>(
170 'cargo-watch.enable',
171 true,
172 );
173 }
174
175 if (config.has('cargo-watch.arguments')) {
176 this.cargoWatchOptions.arguments = config.get<string[]>(
177 'cargo-watch.arguments',
178 [],
179 );
180 }
181
182 if (config.has('cargo-watch.command')) {
183 this.cargoWatchOptions.command = config.get<string>(
184 'cargo-watch.command',
185 '',
186 );
187 }
188
189 if (config.has('cargo-watch.allTargets')) {
190 this.cargoWatchOptions.allTargets = config.get<boolean>(
191 'cargo-watch.allTargets',
192 true,
193 );
194 }
195
196 if (config.has('lruCapacity')) {
197 this.lruCapacity = config.get('lruCapacity') as number;
198 }
199
200 if (config.has('displayInlayHints')) {
201 this.displayInlayHints = config.get('displayInlayHints') as boolean;
202 }
203 if (config.has('maxInlayHintLength')) {
204 this.maxInlayHintLength = config.get(
205 'maxInlayHintLength',
206 ) as number;
207 }
208 if (config.has('excludeGlobs')) {
209 this.excludeGlobs = config.get('excludeGlobs') || [];
210 }
211 if (config.has('useClientWatching')) {
212 this.useClientWatching = config.get('useClientWatching') || true;
213 }
214 if (config.has('featureFlags')) {
215 this.featureFlags = config.get('featureFlags') || {};
216 }
217 if (config.has('withSysroot')) {
218 this.withSysroot = config.get('withSysroot') || false;
219 }
220
221 if (config.has('cargoFeatures.noDefaultFeatures')) {
222 this.cargoFeatures.noDefaultFeatures = config.get(
223 'cargoFeatures.noDefaultFeatures',
224 false,
225 );
226 }
227 if (config.has('cargoFeatures.allFeatures')) {
228 this.cargoFeatures.allFeatures = config.get(
229 'cargoFeatures.allFeatures',
230 true,
231 );
232 }
233 if (config.has('cargoFeatures.features')) {
234 this.cargoFeatures.features = config.get(
235 'cargoFeatures.features',
236 [],
237 );
238 }
239
240 if (
241 this.prevCargoFeatures !== null &&
242 (this.cargoFeatures.allFeatures !==
243 this.prevCargoFeatures.allFeatures ||
244 this.cargoFeatures.noDefaultFeatures !==
245 this.prevCargoFeatures.noDefaultFeatures ||
246 this.cargoFeatures.features.length !==
247 this.prevCargoFeatures.features.length ||
248 this.cargoFeatures.features.some(
249 (v, i) => v !== this.prevCargoFeatures!.features[i],
250 ))
251 ) {
252 requireReloadMessage = 'Changing cargo features requires a reload';
253 }
254 this.prevCargoFeatures = { ...this.cargoFeatures };
255
256 if (this.prevCargoWatchOptions !== null) {
257 const changed =
258 this.cargoWatchOptions.enable !== this.prevCargoWatchOptions.enable ||
259 this.cargoWatchOptions.command !== this.prevCargoWatchOptions.command ||
260 this.cargoWatchOptions.allTargets !== this.prevCargoWatchOptions.allTargets ||
261 this.cargoWatchOptions.arguments.length !== this.prevCargoWatchOptions.arguments.length ||
262 this.cargoWatchOptions.arguments.some(
263 (v, i) => v !== this.prevCargoWatchOptions!.arguments[i],
264 );
265 if (changed) {
266 requireReloadMessage = 'Changing cargo-watch options requires a reload';
267 }
268 }
269 this.prevCargoWatchOptions = { ...this.cargoWatchOptions };
270 146
271 if (requireReloadMessage !== null) { 147 get cargoFeatures(): CargoFeatures {
272 const reloadAction = 'Reload now'; 148 return {
273 vscode.window 149 noDefaultFeatures: this.cfg.get("cargoFeatures.noDefaultFeatures") as boolean,
274 .showInformationMessage(requireReloadMessage, reloadAction) 150 allFeatures: this.cfg.get("cargoFeatures.allFeatures") as boolean,
275 .then(selectedAction => { 151 features: this.cfg.get("cargoFeatures.features") as string[],
276 if (selectedAction === reloadAction) { 152 };
277 vscode.commands.executeCommand(
278 'workbench.action.reloadWindow',
279 );
280 }
281 });
282 }
283 } 153 }
154
155 // for internal use
156 get withSysroot() { return this.cfg.get("withSysroot", false); }
284} 157}
diff --git a/editors/code/src/status_display.ts b/editors/code/src/status_display.ts
index 51dbf388b..993e79d70 100644
--- a/editors/code/src/status_display.ts
+++ b/editors/code/src/status_display.ts
@@ -66,9 +66,9 @@ class StatusDisplay implements Disposable {
66 66
67 refreshLabel() { 67 refreshLabel() {
68 if (this.packageName) { 68 if (this.packageName) {
69 this.statusBarItem!.text = `${spinnerFrames[this.i]} cargo ${this.command} [${this.packageName}]`; 69 this.statusBarItem.text = `${spinnerFrames[this.i]} cargo ${this.command} [${this.packageName}]`;
70 } else { 70 } else {
71 this.statusBarItem!.text = `${spinnerFrames[this.i]} cargo ${this.command}`; 71 this.statusBarItem.text = `${spinnerFrames[this.i]} cargo ${this.command}`;
72 } 72 }
73 } 73 }
74 74