branch: elpa/rubocop commit 6c9c5a98beeee0abbae317184b1541d7defde126 Author: Bozhidar Batsov <bozhi...@batsov.com> Commit: Bozhidar Batsov <bozhi...@batsov.com>
Add a bunch of commands for code formatting --- README.md | 14 +++++++++++++- rubocop.el | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bdf5ec5..9335185 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,9 @@ Command | Description <kbd>M-x rubocop-autocorrect-project</kbd> | Runs auto-correct on the entire project | `C-c C-r P` <kbd>M-x rubocop-autocorrect-directory</kbd> | Prompts for a directory on which to run auto-correct | `C-c C-r D` <kbd>M-x rubocop-autocorrect-current-file</kbd> | Runs auto-correct on the currently visited file. | `C-c C-r F` +<kbd>M-x rubocop-format-project</kbd> | Runs format on the entire project | `C-c C-r X` +<kbd>M-x rubocop-format-directory</kbd> | Prompts for a directory on which to run format | `C-c C-r y` +<kbd>M-x rubocop-format-current-file</kbd> | Runs format on the currently visited file. | `C-c C-r x` If you use them often you might want to enable `rubocop-mode` which will added some keybindings for them: @@ -59,6 +62,13 @@ By default `rubocop-mode` uses the prefix `C-c C-r` for its commands, but you ca There are a couple of configuration variables that you can use to adjust RuboCop.el's behavior. +The variable `rubocop-autocorrect-on-save` controls whether to auto-correct automatically files on save when +`rubocop-mode` is active. It's disabled by default, but you can easily change this: + +``` emacs-lisp +(setq rubocop-autocorrect-on-save t) +``` + You can change the shell command used by `rubocop-check-*` commands via `rubocop-check-command`: ``` emacs-lisp @@ -66,13 +76,15 @@ You can change the shell command used by `rubocop-check-*` commands via `rubocop (setq rubocop-check-command "rubocop --lint --format emacs") ``` -You can change the shell command used by `rubocop-autocorrect-*` commands via `rubocop-autocorrect-command` +You can change the shell command used by `rubocop-autocorrect-*` commands via `rubocop-autocorrect-command`: ``` emacs-lisp ;; let's run all auto-corrections possible (by default it's only the safe ones) (setq rubocop-autocorrect-command "rubocop -A --format emacs") ``` +You can change the shell command used by `rubocop-format-*` commands via `rubocop-format-command`. + ## Known issues Check out the project's diff --git a/rubocop.el b/rubocop.el index 7a65b3a..546c93e 100644 --- a/rubocop.el +++ b/rubocop.el @@ -58,6 +58,12 @@ "The command used to run RuboCop's autocorrection." :type 'string) +(defcustom rubocop-format-command + "rubocop -x --format emacs" + "The command used to run RuboCop's code formatting. +It's basically auto-correction limited to layout cops." + :type 'string) + (defcustom rubocop-extensions '() "A list of extensions to be loaded by RuboCop." @@ -154,6 +160,12 @@ Alternatively prompt user for directory." (rubocop-autocorrect-directory (rubocop-project-root))) ;;;###autoload +(defun rubocop-format-project () + "Run format on current project." + (interactive) + (rubocop-format-directory (rubocop-project-root))) + +;;;###autoload (defun rubocop-check-directory (&optional directory) "Run check on DIRECTORY if present. Alternatively prompt user for directory." @@ -167,6 +179,12 @@ Alternatively prompt user for directory." (interactive) (rubocop--dir-command rubocop-autocorrect-command directory)) +(defun rubocop-format-directory (&optional directory) + "Run format on DIRECTORY if present. +Alternatively prompt user for directory." + (interactive) + (rubocop--dir-command rubocop-format-command directory)) + (defun rubocop--file-command (command) "Run COMMAND on currently visited file." (rubocop-ensure-installed) @@ -193,8 +211,16 @@ Alternatively prompt user for directory." (rubocop--file-command rubocop-autocorrect-command)) (defun rubocop-autocorrect-current-file-silent () - (if rubocop-autocorrect-on-save - (save-window-excursion (rubocop-autocorrect-current-file)))) + "This command is used by the minor mode to auto-correct on save. +See also `rubocop-autocorrect-on-save'." + (when rubocop-autocorrect-on-save + (save-window-excursion (rubocop-autocorrect-current-file)))) + +;;;###autoload +(defun rubocop-format-current-file () + "Run format on current file." + (interactive) + (rubocop--file-command rubocop-format-command)) (defun rubocop-bundled-p () "Check if RuboCop has been bundled." @@ -219,6 +245,9 @@ Alternatively prompt user for directory." (define-key prefix-map (kbd "P") #'rubocop-autocorrect-project) (define-key prefix-map (kbd "D") #'rubocop-autocorrect-directory) (define-key prefix-map (kbd "F") #'rubocop-autocorrect-current-file) + (define-key prefix-map (kbd "X") #'rubocop-format-project) + (define-key prefix-map (kbd "y") #'rubocop-format-directory) + (define-key prefix-map (kbd "x") #'rubocop-format-current-file) (define-key map rubocop-keymap-prefix prefix-map)) map)