Skip to content
This repository was archived by the owner on Dec 8, 2020. It is now read-only.

Commit cc78970

Browse files
authored
Ask user to choose mode (#251)
1 parent 20e56e7 commit cc78970

File tree

4 files changed

+201
-141
lines changed

4 files changed

+201
-141
lines changed

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,18 @@
165165
"title": "Rust extension configuration",
166166
"type": "object",
167167
"properties": {
168-
"rust.forceLegacyMode": {
168+
"rust.mode": {
169169
"type": [
170-
"boolean",
170+
"string",
171171
"null"
172172
],
173173
"default": null,
174-
"description": "Flag indicating if the extension shouldn't try to run RLS. By default, it is false"
174+
"description": "The mode in which the extension will function",
175+
"enum": [
176+
"legacy",
177+
"rls",
178+
null
179+
]
175180
},
176181
"rust.racerPath": {
177182
"type": [

src/components/configuration/Configuration.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,25 @@ export enum Mode {
3535
RLS
3636
}
3737

38+
/**
39+
* Returns the representation of the specified mode suitable for being a value for the
40+
* configuration parameter
41+
* @param mode The mode which representation will be returned for
42+
* @return The representation of the specified mode
43+
*/
44+
export function asConfigurationParameterValue(mode: Mode | undefined): string | null {
45+
switch (mode) {
46+
case Mode.Legacy:
47+
return 'legacy';
48+
case Mode.RLS:
49+
return 'rls';
50+
case undefined:
51+
return null;
52+
}
53+
}
54+
3855
namespace Properties {
39-
export const forceLegacyMode = 'forceLegacyMode';
56+
export const mode = 'mode';
4057
}
4158

4259
/**
@@ -45,9 +62,7 @@ namespace Properties {
4562
*/
4663
export class Configuration {
4764
private _mode: Mode | undefined;
48-
private _isForcedLegacyMode: boolean;
4965
private logger: ChildLogger;
50-
5166
private rustInstallation: Rustup | NotRustup | undefined;
5267

5368
/**
@@ -99,7 +114,7 @@ export class Configuration {
99114
undefined,
100115
undefined
101116
);
102-
if (!configuration.isForcedLegacyMode()) {
117+
if (configuration._mode === Mode.RLS) {
103118
await configuration.updatePathToRlsExecutableSpecifiedByUser();
104119
}
105120
return configuration;
@@ -144,20 +159,26 @@ export class Configuration {
144159
);
145160
}
146161

162+
/**
163+
* Returns the mode which the extension runs in
164+
* @return The mode
165+
*/
147166
public mode(): Mode | undefined {
148167
return this._mode;
149168
}
150169

170+
/**
171+
* Saves the specified mode in both the object and the configuration
172+
* @param mode The mode
173+
*/
151174
public setMode(mode: Mode): void {
152175
if (this._mode !== undefined) {
153-
this.logger.createChildLogger(`setMode(${mode}): `).error('this._mode !== undefined. The method should not have been called');
176+
this.logger.error(`setMode(${mode}): this._mode(${this._mode}) !== undefined. The method should not have been called`);
154177
return;
155178
}
156179
this._mode = mode;
157-
}
158-
159-
public isForcedLegacyMode(): boolean {
160-
return this._isForcedLegacyMode;
180+
const configuration = Configuration.getConfiguration();
181+
configuration.update(Properties.mode, asConfigurationParameterValue(mode), true);
161182
}
162183

163184
/**
@@ -368,12 +389,6 @@ export class Configuration {
368389
}
369390
}
370391

371-
public setForceLegacyMode(value: boolean): void {
372-
const configuration = Configuration.getConfiguration();
373-
configuration.update(Properties.forceLegacyMode, value, true);
374-
this._isForcedLegacyMode = value;
375-
}
376-
377392
private static async loadRustcSysRoot(): Promise<string | undefined> {
378393
const executable = 'rustc';
379394

@@ -478,18 +493,23 @@ export class Configuration {
478493
rlsPathSpecifiedByUser: string | undefined,
479494
pathToRacer: string | undefined
480495
) {
481-
function isForcedLegacyMode(): boolean {
496+
function mode(): Mode | undefined {
482497
const configuration = Configuration.getConfiguration();
483-
const value: boolean | null | undefined = configuration[Properties.forceLegacyMode];
484-
if (value) {
485-
// It is actually `true`, but who knows how the code would behave later
486-
return value;
498+
const value: string | null | undefined = configuration[Properties.mode];
499+
if (typeof value === 'string') {
500+
switch (value) {
501+
case asConfigurationParameterValue(Mode.Legacy):
502+
return Mode.Legacy;
503+
case asConfigurationParameterValue(Mode.RLS):
504+
return Mode.RLS;
505+
default:
506+
return undefined;
507+
}
487508
} else {
488-
return false;
509+
return undefined;
489510
}
490511
}
491-
this._mode = undefined;
492-
this._isForcedLegacyMode = isForcedLegacyMode();
512+
this._mode = mode();
493513
this.logger = logger;
494514
this.rustInstallation = rustInstallation;
495515
this.pathToRustSourceCodeSpecifiedByUser = pathToRustSourceCodeSpecifiedByUser;

src/components/configuration/Rustup.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ export class Rustup {
272272
return this.isComponentInstalled(Rustup.getRlsComponentName());
273273
}
274274

275+
/**
276+
* Returns whether "rust-analysis" is installed
277+
* @return The flag indicating whether "rust-analysis" is installed
278+
*/
279+
public isRustAnalysisInstalled(): boolean {
280+
return this.isComponentInstalled(Rustup.getRustAnalysisComponentName());
281+
}
282+
275283
/**
276284
* Returns true if the component `rust-analysis` can be installed otherwise false.
277285
* If the component is already installed, the method returns false

0 commit comments

Comments
 (0)