diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b0b5c6..dfa5c9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 4c15b8f..5a5c639 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/docs/autocorrect-all-command.png b/docs/autocorrect-all-command.png new file mode 100644 index 0000000..8ba81b8 Binary files /dev/null and b/docs/autocorrect-all-command.png differ diff --git a/package.json b/package.json index 50da78f..dbd3197 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/extension.ts b/src/extension.ts index 45ad9e9..a9fe221 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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) ]; } @@ -400,12 +401,20 @@ async function restartLanguageServer(): Promise { } async function formatAutocorrects(): Promise { + await executeCommand('rubocop.formatAutocorrects'); +} + +async function formatAutocorrectsAll(): Promise { + await executeCommand('rubocop.formatAutocorrectsAll'); +} + +async function executeCommand(command: string): Promise { 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 diff --git a/src/test/suite/automation.ts b/src/test/suite/automation.ts index 1651494..3568ccd 100644 --- a/src/test/suite/automation.ts +++ b/src/test/suite/automation.ts @@ -32,6 +32,10 @@ export async function formatAutocorrects(): Promise { return await commands.executeCommand('rubocop.formatAutocorrects'); } +export async function formatAutocorrectsAll(): Promise { + return await commands.executeCommand('rubocop.formatAutocorrectsAll'); +} + export async function restart(): Promise { return await commands.executeCommand('rubocop.restart'); } diff --git a/src/test/suite/index.test.ts b/src/test/suite/index.test.ts index 2fa6a4c..70d3c06 100644 --- a/src/test/suite/index.test.ts +++ b/src/test/suite/index.test.ts @@ -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 @@ -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); }); }); });