From d7d6d81b520e9d83e2411fc49bd5923cdb0c4aae Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Tue, 19 May 2020 21:08:35 +0100 Subject: [PATCH] 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. --- rubocopfmt.el | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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