editor/format: Use eglot LSP formatter

This commit is contained in:
Seong Yong-ju 2020-05-29 13:09:30 +09:00
parent d3b1664081
commit 6798966f78
3 changed files with 21 additions and 15 deletions

View file

@ -55,7 +55,6 @@
:desc "Jump to symbol in any workspace" "J" #'helm-lsp-global-workspace-symbol)) :desc "Jump to symbol in any workspace" "J" #'helm-lsp-global-workspace-symbol))
(:when (featurep! :tools lsp +eglot) (:when (featurep! :tools lsp +eglot)
:desc "LSP Execute code action" "a" #'eglot-code-actions :desc "LSP Execute code action" "a" #'eglot-code-actions
:desc "LSP Format buffer/region" "F" #'eglot-format
:desc "LSP Rename" "r" #'eglot-rename :desc "LSP Rename" "r" #'eglot-rename
:desc "LSP Find declaration" "j" #'eglot-find-declaration :desc "LSP Find declaration" "j" #'eglot-find-declaration
:desc "LSP Find implementation" "J" #'eglot-find-implementation)) :desc "LSP Find implementation" "J" #'eglot-find-implementation))

View file

@ -358,7 +358,6 @@
:desc "LSP" "l" lsp-command-map)) :desc "LSP" "l" lsp-command-map))
(:when (featurep! :tools lsp +eglot) (:when (featurep! :tools lsp +eglot)
:desc "LSP Execute code action" "a" #'eglot-code-actions :desc "LSP Execute code action" "a" #'eglot-code-actions
:desc "LSP Format buffer/region" "F" #'eglot-format
:desc "LSP Rename" "r" #'eglot-rename :desc "LSP Rename" "r" #'eglot-rename
:desc "LSP Find declaration" "j" #'eglot-find-declaration :desc "LSP Find declaration" "j" #'eglot-find-declaration
:desc "LSP Find implementation" "J" #'eglot-find-implementation) :desc "LSP Find implementation" "J" #'eglot-find-implementation)

View file

@ -206,11 +206,15 @@ See `+format/buffer' for the interactive version of this function, and
"Reformat the current buffer using LSP or `format-all-buffer'." "Reformat the current buffer using LSP or `format-all-buffer'."
(interactive) (interactive)
(call-interactively (call-interactively
(if (and +format-with-lsp (cond ((and +format-with-lsp
(bound-and-true-p lsp-mode) (bound-and-true-p lsp-mode)
(lsp-feature? "textDocument/formatting")) (lsp-feature? "textDocument/formatting"))
#'lsp-format-buffer #'lsp-format-buffer)
#'format-all-buffer))) ((and +format-with-lsp
(bound-and-true-p eglot--managed-mode)
(eglot--server-capable :documentFormattingProvider))
#'eglot-format-buffer)
(t #'format-all-buffer))))
;;;###autoload ;;;###autoload
(defun +format/region (beg end) (defun +format/region (beg end)
@ -220,14 +224,18 @@ 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")
(if (and +format-with-lsp (cond ((and +format-with-lsp
(bound-and-true-p lsp-mode) (bound-and-true-p lsp-mode)
(lsp-feature? "textDocument/rangeFormatting")) (lsp-feature? "textDocument/rangeFormatting"))
(call-interactively #'lsp-format-region) (call-interactively #'lsp-format-region))
(save-restriction ((and +format-with-lsp
(narrow-to-region beg end) (bound-and-true-p eglot--managed-mode)
(let ((+format-region-p t)) (eglot--server-capable :documentRangeFormattingProvider))
(+format/buffer))))) (call-interactively #'eglot-format))
(t (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 ()