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 ;;; Commands
;;;###autoload ;;;###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 ;;;###autoload
(defun +format/region (beg end) (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 contains a syntax error in isolation. It is mostly useful for formatting
snippets or single lines." snippets or single lines."
(interactive "rP") (interactive "rP")
(save-restriction (if (and (bound-and-true-p lsp-mode)
(narrow-to-region beg end) (lsp-feature? "textDocument/rangeFormatting"))
(let ((+format-region-p t)) #'lsp-format-region
(+format/buffer)))) (save-restriction
(narrow-to-region beg end)
(let ((+format-region-p t))
(+format/buffer)))))
;;;###autoload ;;;###autoload
(defun +format/region-or-buffer () (defun +format/region-or-buffer ()
@ -220,14 +230,8 @@ is selected)."
(interactive) (interactive)
(call-interactively (call-interactively
(if (doom-region-active-p) (if (doom-region-active-p)
(if (and (bound-and-true-p lsp-mode) #'+format/region
(lsp-feature? "textDocument/rangeFormatting")) #'+format/buffer)))
#'lsp-format-region
#'+format/region)
(if (and (bound-and-true-p lsp-mode)
(lsp-feature? "textDocument/formatting"))
#'lsp-format-buffer
#'+format/buffer))))
;; ;;