refactor(format): improve lsp/eglot formatter dispatchers

This commit is contained in:
Henrik Lissner 2024-02-14 18:09:15 -05:00 committed by Yann Esposito (Yogsototh)
parent 02ce4ee2b9
commit 773bf46f5e
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646

View file

@ -95,17 +95,19 @@ is selected)."
Does nothing if `+format-with-lsp' is nil or the active server doesn't support Does nothing if `+format-with-lsp' is nil or the active server doesn't support
the requested feature." the requested feature."
(when (and +format-with-lsp (and +format-with-lsp
(bound-and-true-p lsp-mode) (bound-and-true-p lsp-mode)
(lsp-feature? (pcase op
(if (eq op 'buffer) ('buffer (condition-case _
"textDocument/formatting" ;; Avoid lsp-feature? checks for this, since
"textDocument/rangeFormatting"))) ;; `lsp-format-buffer' does its own, and allows clients
(call-interactively ;; without formatting support (but with rangeFormatting,
(if (eq op 'buffer) ;; for some reason) to work.
#'lsp-format-buffer (always (lsp-format-buffer))
#'lsp-format-region)) ('lsp-capability-not-supported nil)))
t)) ('region (if (lsp-feature? "textDocument/rangeFormatting")
(always (lsp-format-region beg end))))
(_ (error "Invalid formatter operation: %s" op)))))
;;;###autoload ;;;###autoload
(defun +format-with-eglot-fn (beg end op) (defun +format-with-eglot-fn (beg end op)
@ -113,16 +115,14 @@ the requested feature."
Does nothing if `+format-with-lsp' is nil or the active server doesn't support Does nothing if `+format-with-lsp' is nil or the active server doesn't support
the requested feature." the requested feature."
(when (and +format-with-lsp (and +format-with-lsp
(bound-and-true-p eglot-managed-mode) (bound-and-true-p eglot-managed-mode)
(eglot--server-capable (pcase op
(if (eq op 'buffer) ('buffer (if (eglot--server-capable :documentFormattingProvider)
:documentFormattingProvider (always (eglot-format-buffer))))
:documentRangeFormattingProvider))) ('region (if (eglot--server-capable :documentRangeFormattingProvider)
(if (eq op 'buffer) (always (eglot-format beg end))))
(eglot-format-buffer) (_ (error "Invalid formatter operation: %s" op)))))
(eglot-format beg end))
t))
;;;###autoload ;;;###autoload
(defun +format-in-org-src-blocks-fn (beg end _op) (defun +format-in-org-src-blocks-fn (beg end _op)