Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Added support for ruby.format, defaults to false (no formatting). #298

Merged
merged 2 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@
}
}
}
},
"ruby.format": {
"type": [ "boolean", "string" ],
"enum": [ false, "rubocop" ],
"default": false,
"description": "Which system to use for formatting, or false for no formatting"
}
}
},
Expand Down
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,20 @@ Settings available (in your VSCode workspace) for each of the linters:
```
## Formatting

The VS Code Ruby extension can automatically format your Ruby files whenever you save.

### Rubocop

Set `ruby.format` to `rubocop` to enable rubocop formatting on save.

Formatting requires the rubocop gem to be installed. Note that you may have to turn on some of the AutoCorrect functions in your `.rubocop.yml` file. See the [rubocop documentation](http://rubocop.readthedocs.io/en/latest/configuration/).

Important note: VS Code has a timeout that limits file formatters to 750ms. This is often not enough time for rubocop to complete. In the near future VS Code will allow customizing this timeout via the `editor.formatOnSaveTimeout` setting. See [#43702](https://github.com/Microsoft/vscode/pull/43702) for more details.

### Rufo

Rufo is an alternative Ruby formatting tool. See the [VS Code Rufo Extension](https://github.com/bessey/vscode-rufo) if you want to try it.

## Autocomplete

The `ruby.codeCompletion` setting lets you select a method for code completion and other intellisense features. Valid options are `solargraph`, `rcodetools`, and `none`.
Expand Down
16 changes: 9 additions & 7 deletions src/format/rubyFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import { AutoCorrect } from './RuboCop';
export class RubyDocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {
private autoCorrect: AutoCorrect;

constructor() {
this.autoCorrect = new AutoCorrect();
}

public register(ctx: vscode.ExtensionContext) {
// only attempt to format if ruby.format is set to rubocop
if (vscode.workspace.getConfiguration("ruby").get("format") !== "rubocop") {
return;
}

this.autoCorrect = new AutoCorrect();
this.autoCorrect.test().then(
() => ctx.subscriptions.push(
vscode.languages.registerDocumentFormattingEditProvider('ruby', this)
),
() => console.log("Rubocop not installed")
)
// silent failure - AutoCorrect will handle error messages
);
}

Expand All @@ -28,7 +30,7 @@ export class RubyDocumentFormattingEditProvider implements vscode.DocumentFormat
return [new vscode.TextEdit(document.validateRange(new vscode.Range(0, 0, Infinity, Infinity)), result)];
},
err => {
console.log("Failed to format:", err);
// silent failure - AutoCorrect will handle error messages
return [];
}
);
Expand Down