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

Commit

Permalink
Use rustfmt when run rls mode (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
KalitaAlexey authored Jun 2, 2017
1 parent 9bab179 commit e05e66b
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 154 deletions.
4 changes: 2 additions & 2 deletions doc/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ The second one is recommended and at some point the first one will be removed.

But Legacy Mode should work just fine and if it doesn't, open an issue.

Unfortunately, sometimes RLS does not work as one can expect.
In this situation one can set `"rust.forceLegacyMode"` to `true` and the extension will function in Legacy Mode.
When the extension starts the first time, it asks to choose one of the modes.
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.

Expand Down
15 changes: 14 additions & 1 deletion doc/rls_mode/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ The type of the parameter is an object with the following fields:
* `"args"` - an array of strings. Arguments to pass to the executable
* `"env"` - an environment to append to the current environment to execute the executable
* `"revealOutputChannelOn"` - a string. Specifies the condition when the output channel should be revealed
* `"useRustfmt"` - either a boolean or `null`. Specifies whether [rustfmt] should be used for formatting

By default, it is `null`.

### The revealOutputChannelOn configuration parameter
### More about configuration parameter

#### revealOutputChannelOn

This determines when the Output channel is revealed.

Expand All @@ -26,6 +29,14 @@ The possible values are:
* `"error"` - revealed on each error line (default)
* `"never"` - the output channel never reveals automatically

#### useRustfmt

The possible values are:

* `null` - the extension will ask whether [rustfmt] should be used for formatting
* `false` - the extension will not use [rustfmt] for formatting
* `true` - the extension will use [rustfmt] for formatting

## Setting up

The recommended way to set RLS up is using rustup. You should use rustup unless rustup does not suit you.
Expand Down Expand Up @@ -157,3 +168,5 @@ Clicking on the indicator restarts RLS.

## Enabling formatting and renaming
Create a `rls.toml` file in your project's root and add `unstable_features = true` and RLS will be able to auto format on save and renaming.

[rustfmt]: https://github.com/rust-lang-nursery/rustfmt
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@
"never"
],
"type": "string"
},
"useRustfmt": {
"default": null,
"description": "Specified whether rustfmt should be used for formatting",
"type": [
"boolean",
"null"
]
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/components/configuration/RlsConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class RlsConfiguration {
private _userArgs: string[];
private _userEnv: object;
private _revealOutputChannelOn: RevealOutputChannelOn;
private _useRustfmt: boolean | undefined;

/**
* Creates a new instance of the class
Expand Down Expand Up @@ -71,20 +72,50 @@ export class RlsConfiguration {
return this._revealOutputChannelOn;
}

/**
* Returns whether rustfmt should be used for formatting
*/
public getUseRustfmt(): boolean | undefined {
return this._useRustfmt;
}

/**
* Updates the property "useRustfmt" in the user configuration
* @param value The new value
*/
public setUseRustfmt(value: boolean | undefined): void {
if (this._useRustfmt === value) {
return;
}
this._useRustfmt = value;
const suitableValue = typeof value === 'boolean' ? value : null;
updateUserConfigurationParameter(c => { c.useRustfmt = suitableValue; });
}

private constructor(rustup: Rustup | undefined, rustSource: RustSource, executableUserPath: string | undefined) {
this._rustup = rustup;
this._rustSource = rustSource;
this._executableUserPath = executableUserPath;
this._userArgs = getUserArgs();
this._userEnv = getUserEnv();
this._revealOutputChannelOn = getUserRevealOutputChannelOn();
this._useRustfmt = getUserUseRustfmt();
}
}

function getUserConfiguration(): any {
return Configuration.getConfiguration()['rls'];
}

function updateUserConfigurationParameter(updateParameter: (c: any) => void): void {
let configuration = getUserConfiguration();
if (!configuration) {
configuration = {};
}
updateParameter(configuration);
Configuration.getConfiguration().update('rls', configuration, true);
}

function getExecutableUserPath(): string | undefined {
const configuration = getUserConfiguration();
if (!configuration) {
Expand Down Expand Up @@ -140,6 +171,18 @@ function getUserRevealOutputChannelOn(): RevealOutputChannelOn {
}
}

function getUserUseRustfmt(): boolean | undefined {
const configuration = getUserConfiguration();
if (!configuration) {
return undefined;
}
const useRustfmt = configuration.useRustfmt;
if (typeof useRustfmt === 'boolean') {
return useRustfmt;
} else {
return undefined;
}
}

async function getCheckedExecutableUserPath(): Promise<string | undefined> {
const path = getExecutableUserPath();
Expand Down
Loading

0 comments on commit e05e66b

Please sign in to comment.