feat(lsp): Add experimental use of lsp-format-buffer

Add support for using `lsp-format-buffer` to format Ruby buffers instead
of `rubocopfmt` when `rubocopfmt-mode` and `lsp-mode` are both enabled.

Disabled by default, and must manually be enabled by setting
`rubocopfmt-on-save-use-lsp-format-buffer` to `t`.

I consider this feature EXPERIMENTAL, meaning it "works on my machine",
with no guarantees of it working elsewhere, nor if it will remain part
of rubocopfmt in the future.
This commit is contained in:
2020-05-19 21:08:35 +01:00
parent fc96145719
commit d7d6d81b52

View File

@@ -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