aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
authorVeetaha <[email protected]>2020-02-13 20:48:20 +0000
committerVeetaha <[email protected]>2020-02-13 20:48:20 +0000
commit7ad15c396286376c4a439b2dec4ec452b5f28dda (patch)
treed840788902ed29a561851c6e2ce02b26a14dc559 /editors
parent9b47124e6e5d32a676961c05661934215e98012c (diff)
vscode: redesigned config with simplicity and Dart extension config implementation in mind
Diffstat (limited to 'editors')
-rw-r--r--editors/code/src/client.ts36
-rw-r--r--editors/code/src/config.ts258
-rw-r--r--editors/code/src/highlighting.ts6
-rw-r--r--editors/code/src/inlay_hints.ts6
-rw-r--r--editors/code/src/status_display.ts6
5 files changed, 96 insertions, 216 deletions
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts
index 2e3d4aba2..a6fb04536 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';
6 6
@@ -8,37 +8,39 @@ export async function createClient(config: Config): Promise<null | lc.LanguageCl
8 // '.' Is the fallback if no folder is open 8 // '.' Is the fallback if no folder is open
9 // TODO?: Workspace folders support Uri's (eg: file://test.txt). 9 // TODO?: Workspace folders support Uri's (eg: file://test.txt).
10 // It might be a good idea to test if the uri points to a file. 10 // It might be a good idea to test if the uri points to a file.
11 const workspaceFolderPath = workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.'; 11 const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.';
12 12
13 const raLspServerPath = await ensureLanguageServerBinary(config.langServerSource); 13 const langServerPath = await ensureLanguageServerBinary(config.langServerBinarySource());
14 if (!raLspServerPath) return null; 14 if (!langServerPath) return null;
15 15
16 const run: lc.Executable = { 16 const run: lc.Executable = {
17 command: raLspServerPath, 17 command: langServerPath,
18 options: { cwd: workspaceFolderPath }, 18 options: { cwd: workspaceFolderPath },
19 }; 19 };
20 const serverOptions: lc.ServerOptions = { 20 const serverOptions: lc.ServerOptions = {
21 run, 21 run,
22 debug: run, 22 debug: run,
23 }; 23 };
24 const traceOutputChannel = window.createOutputChannel( 24 const traceOutputChannel = vscode.window.createOutputChannel(
25 'Rust Analyzer Language Server Trace', 25 'Rust Analyzer Language Server Trace',
26 ); 26 );
27 const cargoWatchOpts = config.cargoWatchOptions();
28
27 const clientOptions: lc.LanguageClientOptions = { 29 const clientOptions: lc.LanguageClientOptions = {
28 documentSelector: [{ scheme: 'file', language: 'rust' }], 30 documentSelector: [{ scheme: 'file', language: 'rust' }],
29 initializationOptions: { 31 initializationOptions: {
30 publishDecorations: true, 32 publishDecorations: true,
31 lruCapacity: config.lruCapacity, 33 lruCapacity: config.lruCapacity(),
32 maxInlayHintLength: config.maxInlayHintLength, 34 maxInlayHintLength: config.maxInlayHintLength(),
33 cargoWatchEnable: config.cargoWatchOptions.enable, 35 cargoWatchEnable: cargoWatchOpts.enable,
34 cargoWatchArgs: config.cargoWatchOptions.arguments, 36 cargoWatchArgs: cargoWatchOpts.arguments,
35 cargoWatchCommand: config.cargoWatchOptions.command, 37 cargoWatchCommand: cargoWatchOpts.command,
36 cargoWatchAllTargets: config.cargoWatchOptions.allTargets, 38 cargoWatchAllTargets: cargoWatchOpts.allTargets,
37 excludeGlobs: config.excludeGlobs, 39 excludeGlobs: config.excludeGlobs(),
38 useClientWatching: config.useClientWatching, 40 useClientWatching: config.useClientWatching(),
39 featureFlags: config.featureFlags, 41 featureFlags: config.featureFlags(),
40 withSysroot: config.withSysroot, 42 withSysroot: config.withSysroot(),
41 cargoFeatures: config.cargoFeatures, 43 cargoFeatures: config.cargoFeatures(),
42 }, 44 },
43 traceOutputChannel, 45 traceOutputChannel,
44 }; 46 };
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 418845436..6c4742464 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -16,45 +16,48 @@ 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 private refreshConfig() {
30 this.cfg = vscode.workspace.getConfiguration(Config.rootSection);
31 console.log("Using configuration:", this.cfg);
32 }
22 33
23 highlightingOn = true; 34 constructor(private ctx: vscode.ExtensionContext) {
24 rainbowHighlightingOn = false; 35 vscode.workspace.onDidChangeConfiguration(this.onConfigChange, this, ctx.subscriptions);
25 enableEnhancedTyping = true; 36 this.refreshConfig();
26 lruCapacity: null | number = null; 37 }
27 displayInlayHints = true; 38
28 maxInlayHintLength: null | number = null; 39 async onConfigChange(event: vscode.ConfigurationChangeEvent) {
29 excludeGlobs: string[] = []; 40 this.refreshConfig();
30 useClientWatching = true; 41
31 featureFlags: Record<string, boolean> = {}; 42 const requiresReloadOpt = Config.requiresReloadOpts.find(
32 // for internal use 43 opt => event.affectsConfiguration(opt)
33 withSysroot: null | boolean = null; 44 );
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 45
46 private prevEnhancedTyping: null | boolean = null; 46 if (!requiresReloadOpt) return;
47 private prevCargoFeatures: null | CargoFeatures = null;
48 private prevCargoWatchOptions: null | CargoWatchOptions = null;
49 47
50 constructor(ctx: vscode.ExtensionContext) { 48 const userResponse = await vscode.window.showInformationMessage(
51 vscode.workspace.onDidChangeConfiguration(_ => this.refresh(ctx), null, ctx.subscriptions); 49 `Changing "${requiresReloadOpt}" requires a reload`,
52 this.refresh(ctx); 50 "Reload now"
51 );
52
53 if (userResponse === "Reload now") {
54 vscode.commands.executeCommand("workbench.action.reloadWindow");
55 }
53 } 56 }
54 57
55 private static expandPathResolving(path: string) { 58 private static replaceTildeWithHomeDir(path: string) {
56 if (path.startsWith('~/')) { 59 if (path.startsWith("~/")) {
57 return path.replace('~', os.homedir()); 60 return os.homedir() + path.slice("~".length);
58 } 61 }
59 return path; 62 return path;
60 } 63 }
@@ -97,16 +100,13 @@ export class Config {
97 } 100 }
98 } 101 }
99 102
100 private static langServerBinarySource( 103 langServerBinarySource(): null | BinarySource {
101 ctx: vscode.ExtensionContext, 104 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 105
106 if (langServerPath) { 106 if (langServerPath) {
107 return { 107 return {
108 type: BinarySource.Type.ExplicitPath, 108 type: BinarySource.Type.ExplicitPath,
109 path: Config.expandPathResolving(langServerPath) 109 path: Config.replaceTildeWithHomeDir(langServerPath)
110 }; 110 };
111 } 111 }
112 112
@@ -118,7 +118,7 @@ export class Config {
118 118
119 return { 119 return {
120 type: BinarySource.Type.GithubRelease, 120 type: BinarySource.Type.GithubRelease,
121 dir: ctx.globalStoragePath, 121 dir: this.ctx.globalStoragePath,
122 file: prebuiltBinaryName, 122 file: prebuiltBinaryName,
123 repo: { 123 repo: {
124 name: "rust-analyzer", 124 name: "rust-analyzer",
@@ -127,158 +127,36 @@ export class Config {
127 }; 127 };
128 } 128 }
129 129
130 // We don't do runtime config validation here for simplicity. More on stackoverflow:
131 // https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension
130 132
131 // FIXME: revisit the logic for `if (.has(...)) config.get(...)` set default 133 // FIXME: add codegen for primitive configurations
132 // values only in one place (i.e. remove default values from non-readonly members declarations) 134 highlightingOn() { return this.cfg.get("highlightingOn") as boolean; }
133 private refresh(ctx: vscode.ExtensionContext) { 135 rainbowHighlightingOn() { return this.cfg.get("rainbowHighlightingOn") as boolean; }
134 const config = vscode.workspace.getConfiguration('rust-analyzer'); 136 lruCapacity() { return this.cfg.get("lruCapacity") as null | number; }
135 137 displayInlayHints() { return this.cfg.get("displayInlayHints") as boolean; }
136 let requireReloadMessage = null; 138 maxInlayHintLength() { return this.cfg.get("maxInlayHintLength") as number; }
137 139 excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; }
138 if (config.has('highlightingOn')) { 140 useClientWatching() { return this.cfg.get("useClientWatching") as boolean; }
139 this.highlightingOn = config.get('highlightingOn') as boolean; 141 featureFlags() { return this.cfg.get("featureFlags") as Record<string, boolean>; }
140 }
141
142 if (config.has('rainbowHighlightingOn')) {
143 this.rainbowHighlightingOn = config.get(
144 'rainbowHighlightingOn',
145 ) as boolean;
146 }
147
148 if (config.has('enableEnhancedTyping')) {
149 this.enableEnhancedTyping = config.get(
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 142
189 if (config.has('cargo-watch.allTargets')) { 143 cargoWatchOptions(): CargoWatchOptions {
190 this.cargoWatchOptions.allTargets = config.get<boolean>( 144 return {
191 'cargo-watch.allTargets', 145 enable: this.cfg.get("cargo-watch.enable") as boolean,
192 true, 146 arguments: this.cfg.get("cargo-watch.arguments") as string[],
193 ); 147 allTargets: this.cfg.get("cargo-watch.allTargets") as boolean,
194 } 148 command: this.cfg.get("cargo-watch.command") as string,
195 149 };
196 if (config.has('lruCapacity')) { 150 }
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 151
271 if (requireReloadMessage !== null) { 152 cargoFeatures(): CargoFeatures {
272 const reloadAction = 'Reload now'; 153 return {
273 vscode.window 154 noDefaultFeatures: this.cfg.get("cargoFeatures.noDefaultFeatures") as boolean,
274 .showInformationMessage(requireReloadMessage, reloadAction) 155 allFeatures: this.cfg.get("cargoFeatures.allFeatures") as boolean,
275 .then(selectedAction => { 156 features: this.cfg.get("cargoFeatures.features") as string[],
276 if (selectedAction === reloadAction) { 157 };
277 vscode.commands.executeCommand(
278 'workbench.action.reloadWindow',
279 );
280 }
281 });
282 }
283 } 158 }
159
160 // for internal use
161 withSysroot() { return this.cfg.get("withSysroot", false); }
284} 162}
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts
index 4fbbe3ddc..e2ae31d29 100644
--- a/editors/code/src/highlighting.ts
+++ b/editors/code/src/highlighting.ts
@@ -11,7 +11,7 @@ export function activateHighlighting(ctx: Ctx) {
11 client.onNotification( 11 client.onNotification(
12 'rust-analyzer/publishDecorations', 12 'rust-analyzer/publishDecorations',
13 (params: PublishDecorationsParams) => { 13 (params: PublishDecorationsParams) => {
14 if (!ctx.config.highlightingOn) return; 14 if (!ctx.config.highlightingOn()) return;
15 15
16 const targetEditor = vscode.window.visibleTextEditors.find( 16 const targetEditor = vscode.window.visibleTextEditors.find(
17 editor => { 17 editor => {
@@ -39,7 +39,7 @@ export function activateHighlighting(ctx: Ctx) {
39 vscode.window.onDidChangeActiveTextEditor( 39 vscode.window.onDidChangeActiveTextEditor(
40 async (editor: vscode.TextEditor | undefined) => { 40 async (editor: vscode.TextEditor | undefined) => {
41 if (!editor || editor.document.languageId !== 'rust') return; 41 if (!editor || editor.document.languageId !== 'rust') return;
42 if (!ctx.config.highlightingOn) return; 42 if (!ctx.config.highlightingOn()) return;
43 const client = ctx.client; 43 const client = ctx.client;
44 if (!client) return; 44 if (!client) return;
45 45
@@ -122,7 +122,7 @@ class Highlighter {
122 string, 122 string,
123 [vscode.Range[], boolean] 123 [vscode.Range[], boolean]
124 > = new Map(); 124 > = new Map();
125 const rainbowTime = this.ctx.config.rainbowHighlightingOn; 125 const rainbowTime = this.ctx.config.rainbowHighlightingOn();
126 126
127 for (const tag of this.decorations.keys()) { 127 for (const tag of this.decorations.keys()) {
128 byTag.set(tag, []); 128 byTag.set(tag, []);
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts
index 1c019a51b..3ff45a625 100644
--- a/editors/code/src/inlay_hints.ts
+++ b/editors/code/src/inlay_hints.ts
@@ -22,12 +22,12 @@ export function activateInlayHints(ctx: Ctx) {
22 ); 22 );
23 23
24 vscode.workspace.onDidChangeConfiguration( 24 vscode.workspace.onDidChangeConfiguration(
25 async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints), 25 async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints()),
26 null, 26 null,
27 ctx.subscriptions 27 ctx.subscriptions
28 ); 28 );
29 29
30 ctx.onDidRestart(_ => hintsUpdater.setEnabled(ctx.config.displayInlayHints)); 30 ctx.onDidRestart(_ => hintsUpdater.setEnabled(ctx.config.displayInlayHints()));
31} 31}
32 32
33interface InlayHintsParams { 33interface InlayHintsParams {
@@ -59,7 +59,7 @@ class HintsUpdater {
59 59
60 constructor(ctx: Ctx) { 60 constructor(ctx: Ctx) {
61 this.ctx = ctx; 61 this.ctx = ctx;
62 this.enabled = ctx.config.displayInlayHints; 62 this.enabled = ctx.config.displayInlayHints();
63 } 63 }
64 64
65 async setEnabled(enabled: boolean) { 65 async setEnabled(enabled: boolean) {
diff --git a/editors/code/src/status_display.ts b/editors/code/src/status_display.ts
index 51dbf388b..ae9a7b1b5 100644
--- a/editors/code/src/status_display.ts
+++ b/editors/code/src/status_display.ts
@@ -7,7 +7,7 @@ import { Ctx } from './ctx';
7const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']; 7const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
8 8
9export function activateStatusDisplay(ctx: Ctx) { 9export function activateStatusDisplay(ctx: Ctx) {
10 const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command); 10 const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions().command);
11 ctx.pushCleanup(statusDisplay); 11 ctx.pushCleanup(statusDisplay);
12 ctx.onDidRestart(client => ctx.pushCleanup(client.onProgress( 12 ctx.onDidRestart(client => ctx.pushCleanup(client.onProgress(
13 WorkDoneProgress.type, 13 WorkDoneProgress.type,
@@ -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