Integrate LSP into +format/{buffer,region} commands

Relevant to #1652
This commit is contained in:
Henrik Lissner 2020-05-08 16:45:58 -04:00
parent dfab5b941f
commit 7472cffadd
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -198,7 +198,14 @@ See `+format/buffer' for the interactive version of this function, and
;;; Commands
;;;###autoload
(defalias '+format/buffer #'format-all-buffer)
(defun +format/buffer ()
"Reformat the current buffer using LSP or `format-all-buffer'."
(interactive)
(call-interactively
(if (and (bound-and-true-p lsp-mode)
(lsp-feature? "textDocument/formatting"))
#'lsp-format-buffer
#'format-all-buffer)))
;;;###autoload
(defun +format/region (beg end)
@ -208,10 +215,13 @@ WARNING: this may not work everywhere. It will throw errors if the region
contains a syntax error in isolation. It is mostly useful for formatting
snippets or single lines."
(interactive "rP")
(if (and (bound-and-true-p lsp-mode)
(lsp-feature? "textDocument/rangeFormatting"))
#'lsp-format-region
(save-restriction
(narrow-to-region beg end)
(let ((+format-region-p t))
(+format/buffer))))
(+format/buffer)))))
;;;###autoload
(defun +format/region-or-buffer ()
@ -220,14 +230,8 @@ is selected)."
(interactive)
(call-interactively
(if (doom-region-active-p)
(if (and (bound-and-true-p lsp-mode)
(lsp-feature? "textDocument/rangeFormatting"))
#'lsp-format-region
#'+format/region)
(if (and (bound-and-true-p lsp-mode)
(lsp-feature? "textDocument/formatting"))
#'lsp-format-buffer
#'+format/buffer))))
#'+format/region
#'+format/buffer)))
;;