Skip to content
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"title": "Ruby: lint by rubocop"
},
{
"command": "editor.action.formatDocument",
"command": "ruby.rubocop.autocorrect",
"when": "editorLangId == 'ruby' || editorLangId == 'gemfile'",
"title": "Ruby: autocorrect by rubocop"
}
],
Expand Down
16 changes: 16 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,20 @@ export function activate(context: vscode.ExtensionContext): void {
'gemfile',
formattingProvider
);

const autocorrectDisposable = vscode.commands.registerCommand('ruby.rubocop.autocorrect', () => {
vscode.window.activeTextEditor.edit((editBuilder) => {
const document = vscode.window.activeTextEditor.document;
const edits = formattingProvider.provideDocumentFormattingEdits(document);
// We only expect one edit from our formatting provider.
if (edits.length === 1) {
const edit = edits[0];
editBuilder.replace(edit.range, edit.newText);
}
if (edits.length > 1) {
throw new Error("Unexpected error: Rubocop document formatter returned multiple edits.");
}
});
});
context.subscriptions.push(autocorrectDisposable);
}