diff --git a/rubocopfmt.el b/rubocopfmt.el index 0d5fe2a..75e175b 100644 --- a/rubocopfmt.el +++ b/rubocopfmt.el @@ -94,6 +94,11 @@ inside a `before-save-hook'." :type '(repeat symbol) :group 'rubocopfmt) +(defcustom rubocopfmt-on-save-use-lsp-format-buffer nil + "EXPERIMENTAL: When set to t and lsp-mode is enabled, use `lsp-format-buffer' +to format buffer before saving, instead of `rubocopfmt'." + :type 'boolean + :group 'rubocopfmt) (defun rubocopfmt--apply-rcs-patch (patch-buffer) "Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer." @@ -287,9 +292,17 @@ If FILE is not found in DIRECTORY, the parent of DIRECTORY will be searched." (remove-hook 'before-save-hook 'rubocopfmt-before-save t))) (defun rubocopfmt-before-save () - "Format buffer via rubocopfmt if major mode is `ruby-mode'." + "Format buffer if major mode is one listed in `rubocopfmt-major-modes'. + +Formatting is done via `rubocopfmt', or if +`rubocopfmt-use-lsp-formatter' is t and `lsp-mode' is enabled in +the buffer, format with `lsp-format-buffer' instead." (interactive) - (when (member major-mode rubocopfmt-major-modes) (rubocopfmt))) + (when (and (member major-mode rubocopfmt-major-modes)) + (if (and rubocopfmt-on-save-use-lsp-format-buffer + (bound-and-true-p lsp-mode)) + (lsp-format-buffer) + (rubocopfmt)))) (provide 'rubocopfmt) ;;; rubocopfmt.el ends here