Skip to content

Commit

Permalink
Merge pull request #5 from rubocop/support_format_autocorrects_all_co…
Browse files Browse the repository at this point in the history
…mmand

Support `rubocop.formatAutocorrectsAll` command
  • Loading branch information
koic committed Aug 19, 2023
2 parents e8f3a34 + f9e3a32 commit cf8e289
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

- Support `rubocop.formatAutocorrectsAll` command, which requires RuboCop 1.56+. (@koic)

## 0.5.0 (2023-08-05)

### New features
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ the extension's behavior in other workspace folders.

In addition to the built-in VS Code Formatting API, you can trigger the
extension to format and autocorrect the current file listing by running
the command "RuboCop: Format with Autocorrects":
the command "RuboCop: Format with Autocorrects". This is equivalent to `rubocop -a`:

![Autocorrect command](/docs/autocorrect-command.png)

Expand All @@ -237,6 +237,15 @@ Or, in `keybindings.json`:
]
```

You can also trigger the extension to format and autocorrect all the current file listing by running
the command "RuboCop: Format All with Autocorrects". This is equivalent to `rubocop -A`:

![Autocorrect all command](/docs/autocorrect-all-command.png)

**This command "RuboCop: Format All with Autocorrects" requires RuboCop 1.56+ to be enabled.**

You can use two autocorrect commands depending on the purpose.

## Decoding the Status Bar item

The extension also includes a status bar item to convey the status of the
Expand Down
Binary file added docs/autocorrect-all-command.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
"command": "rubocop.formatAutocorrects",
"title": "RuboCop: Format with Autocorrects"
},
{
"command": "rubocop.formatAutocorrectsAll",
"title": "RuboCop: Format All with Autocorrects"
},
{
"command": "rubocop.showOutputChannel",
"title": "RuboCop: Show Output Channel"
Expand Down
13 changes: 11 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ function registerCommands(): Disposable[] {
commands.registerCommand('rubocop.stop', stopLanguageServer),
commands.registerCommand('rubocop.restart', restartLanguageServer),
commands.registerCommand('rubocop.showOutputChannel', () => outputChannel?.show()),
commands.registerCommand('rubocop.formatAutocorrects', formatAutocorrects)
commands.registerCommand('rubocop.formatAutocorrects', formatAutocorrects),
commands.registerCommand('rubocop.formatAutocorrectsAll', formatAutocorrectsAll)
];
}

Expand Down Expand Up @@ -400,12 +401,20 @@ async function restartLanguageServer(): Promise<void> {
}

async function formatAutocorrects(): Promise<void> {
await executeCommand('rubocop.formatAutocorrects');
}

async function formatAutocorrectsAll(): Promise<void> {
await executeCommand('rubocop.formatAutocorrectsAll');
}

async function executeCommand(command: string): Promise<void> {
const editor = window.activeTextEditor;
if (editor == null || languageClient == null || !supportedLanguage(editor.document.languageId)) return;

try {
await languageClient.sendRequest(ExecuteCommandRequest.type, {
command: 'rubocop.formatAutocorrects',
command,
arguments: [{
uri: editor.document.uri.toString(),
version: editor.document.version
Expand Down
4 changes: 4 additions & 0 deletions src/test/suite/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export async function formatAutocorrects(): Promise<void> {
return await commands.executeCommand('rubocop.formatAutocorrects');
}

export async function formatAutocorrectsAll(): Promise<void> {
return await commands.executeCommand('rubocop.formatAutocorrectsAll');
}

export async function restart(): Promise<void> {
return await commands.executeCommand('rubocop.restart');
}
Expand Down
23 changes: 19 additions & 4 deletions src/test/suite/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ const UNFORMATTED = `class Foo
end
`;

const FORMATTED = `class Foo
const SAFE_FORMATTED = `class Foo
def bar
puts 'baz'
end
end
`;

const UNSAFE_FORMATTED = `# frozen_string_literal: true
class Foo
def bar
puts 'baz'
end
Expand Down Expand Up @@ -48,13 +57,19 @@ suite('RuboCop', () => {
test('format', async() => {
const editor = await auto.createEditor(UNFORMATTED);
await auto.formatDocument();
assert.equal(editor.document.getText(), FORMATTED);
assert.equal(editor.document.getText(), SAFE_FORMATTED);
});

test('format with custom command', async() => {
test('format with custom command `rubocop.formatAutocorrects`', async() => {
const editor = await auto.createEditor(UNFORMATTED);
await auto.formatAutocorrects();
assert.equal(editor.document.getText(), FORMATTED);
assert.equal(editor.document.getText(), SAFE_FORMATTED);
});

test('format with custom command `rubocop.formatAutocorrectsAll`', async() => {
const editor = await auto.createEditor(UNFORMATTED);
await auto.formatAutocorrectsAll();
assert.equal(editor.document.getText(), UNSAFE_FORMATTED);
});
});
});

0 comments on commit cf8e289

Please sign in to comment.