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

Commit

Permalink
Enable users to specify toolchain to use (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
KalitaAlexey authored Jun 8, 2017
1 parent 6940183 commit fd01859
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 91 deletions.
12 changes: 12 additions & 0 deletions doc/common_configuration_parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Common Configuration Parameters

## rustup

Users should adjust properties of this configuration parameter to customize rustup.

### toolchain

This configuration parameter specifies which toolchain the extension will invoke rustup with.
It is used for getting sysroot, installing components.

However there are few exceptions. Currently RLS is available for nightly hence RLS and rust-analysis are installed for the nightly toolchain.
2 changes: 2 additions & 0 deletions doc/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ The chosen mode is stored in `"rust.mode"` and it can be changed by users.

Each mode is described in detail on its own page.

Some configuration parameters effect both modes. They are described [there](common_configuration_parameters.md).

Furthermore, the extension provides:

* [Linting (the showing of diagnostics in the active text editor)](linting.md)
Expand Down
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@
},
"type": "array"
},
"rust.rustup": {
"default": null,
"description": "rustup configuration",
"properties": {
"toolchain": {
"default": null,
"description": "The toolchain to use for installing components (rust-src), except RLS",
"type": [
"string",
"null"
]
}
}
},
"rust.rls": {
"default": null,
"description": "Rust Language Server configuration",
Expand Down
62 changes: 62 additions & 0 deletions src/Toolchain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export class Toolchain {
public static readonly defaultToolchainPrefix: string = ' (default)';

/**
* "stable" in "stable-x86_64-pc-windows-msvc (default)"
*/
public readonly channel: string;

/**
* "x86_64-pc-windows-msvc" in "stable-x86_64-pc-windows-msvc (default)"
* `undefined` in "stable"
*/
public readonly host: string | undefined;

/**
* true in "stable-x86_64-pc-windows-msvc (default)".
* false in "stable-x86_64-pc-windows-msvc"
*/
public readonly isDefault: boolean;

/**
* Tries to parse the text and if returns the toolchain parsed from the text
* @param text The text to parse
* @return the toolchain or undefined
*/
public static parse(text: string): Toolchain | undefined {
const sepIndex = text.indexOf('-');
const channelEnd = sepIndex === -1 ? undefined : sepIndex;
const channel = text.substring(0, channelEnd);
if (channelEnd === undefined) {
// The text represents the toolchain with the only channel.
return new Toolchain(channel, undefined, false);
}
const spaceIndex = text.indexOf(' ', sepIndex);
const hostEnd = spaceIndex === -1 ? undefined : spaceIndex;
const host = text.substring(sepIndex + 1, hostEnd);
const isDefault = text.endsWith(Toolchain.defaultToolchainPrefix);
return new Toolchain(channel, host, isDefault);
}

public equals(toolchain: Toolchain): boolean {
return this.channel === toolchain.channel && this.host === toolchain.host;
}

public toString(includeHost: boolean, includeIsDefault: boolean): string {
let s = this.channel.concat();
if (includeHost && this.host) {
s += '-';
s += this.host;
}
if (includeIsDefault && this.isDefault) {
s += Toolchain.defaultToolchainPrefix;
}
return s;
}

private constructor(channel: string, host: string | undefined, isDefault: boolean) {
this.channel = channel;
this.host = host;
this.isDefault = isDefault;
}
}
37 changes: 24 additions & 13 deletions src/components/completion/completion_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class CompletionManager {
logger.debug('racer is not installed');
return;
}
const isSourceCodeAvailable: boolean = this.ensureSourceCodeIsAvailable();
const isSourceCodeAvailable: boolean = await this.ensureSourceCodeIsAvailable();
if (!isSourceCodeAvailable) {
logger.debug('Rust\'s source is not installed');
return;
Expand Down Expand Up @@ -123,22 +123,33 @@ export class CompletionManager {
* Ensures that Rust's source code is available to use
* @returns flag indicating whether the source code if available or not
*/
private ensureSourceCodeIsAvailable(): boolean {
private async ensureSourceCodeIsAvailable(): Promise<boolean> {
const logger = this.logger.createChildLogger('ensureSourceCodeIsAvailable: ');
if (this._rustSource.getPath()) {
logger.debug('sources is available');
return true;
}
if (this._rustup) {
// tslint:disable-next-line
const message = 'You are using rustup, but don\'t have installed source code. Do you want to install it?';
window.showErrorMessage(message, 'Yes').then(chosenItem => {
if (chosenItem === 'Yes') {
const terminal = window.createTerminal('Rust source code installation');
terminal.sendText('rustup component add rust-src');
terminal.show();
}
});
if (!this._rustup) {
logger.error('rustup is undefined');
return false;
}
// tslint:disable-next-line
const message = 'You are using rustup, but don\'t have installed source code. Do you want to install it?';
const choice = await window.showErrorMessage(message, 'Yes');
if (choice === 'Yes') {
logger.debug('the user agreed to install rust-src');
const rustSrcInstalled = await this._rustup.installRustSrc();
if (rustSrcInstalled) {
logger.debug('rust-src has been installed');
return true;
} else {
logger.error('rust-src has not been installed');
return false;
}
} else {
logger.debug('the user dismissed the dialog');
return false;
}
return false;
}

/**
Expand Down
22 changes: 9 additions & 13 deletions src/components/configuration/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Configuration {
}

public static getPathConfigParameter(parameterName: string): string | undefined {
const parameter = this.getStringParameter(parameterName);
const parameter = this.getStringConfigParameter(parameterName);
if (parameter) {
return expandTilde(parameter);
} else {
Expand All @@ -76,6 +76,12 @@ export class Configuration {
}
}

public static getStringConfigParameter(parameterName: string): string | undefined {
const configuration = workspace.getConfiguration('rust');
const parameter = configuration.get<string>(parameterName);
return parameter;
}

/**
* Creates a new instance of the class.
* @param logger A value for the field `logger`
Expand Down Expand Up @@ -170,10 +176,8 @@ export class Configuration {
return shouldExecuteCargoCommandInTerminal;
}

public getActionOnSave(): string | null {
const actionOnSave = Configuration.getStringParameter('actionOnSave');

return actionOnSave;
public getActionOnSave(): string | undefined {
return Configuration.getStringConfigParameter('actionOnSave');
}

public shouldShowRunningCargoTaskOutputChannel(): boolean {
Expand Down Expand Up @@ -240,12 +244,4 @@ export class Configuration {
return ActionOnStartingCommandIfThereIsRunningCommand.IgnoreNewCommand;
}
}

private static getStringParameter(parameterName: string): string | null {
const configuration = workspace.getConfiguration('rust');

const parameter: string | null = configuration[parameterName];

return parameter;
}
}
Loading

0 comments on commit fd01859

Please sign in to comment.