fix(format): lsp-mode/eglot formatters

There seems to be context that eglot needs that isn't available in
Apheleia's scratch buffer.

Fix: #7962
This commit is contained in:
Henrik Lissner 2024-08-21 18:47:14 -04:00
parent 879c0b06a4
commit a0c901cca7
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
2 changed files with 45 additions and 45 deletions

View file

@ -8,6 +8,9 @@
;;;###autoload (autoload 'apheleia--get-formatters "apheleia-formatters") ;;;###autoload (autoload 'apheleia--get-formatters "apheleia-formatters")
;;;###autoload
(defvar +format--region-p nil)
;;;###autoload ;;;###autoload
(defun +format-region (start end &optional callback) (defun +format-region (start end &optional callback)
"Format from START to END with `apheleia'." "Format from START to END with `apheleia'."
@ -41,22 +44,21 @@
(when (> indent 0) (when (> indent 0)
(indent-rigidly (point-min) (point-max) (- indent))) (indent-rigidly (point-min) (point-max) (- indent)))
;; ;;
(apheleia-format-buffer (let ((+format--region-p (cons start end)))
command (apheleia-format-buffer
(lambda () command
(with-current-buffer formatted-buffer (lambda ()
(when (> indent 0) (with-current-buffer formatted-buffer
;; restore indentation without affecting new indentation (when (> indent 0)
(indent-rigidly (point-min) (point-max) ;; restore indentation without affecting new indentation
(max 0 (- indent (+format--current-indentation))))) (indent-rigidly (point-min) (point-max)
(set-buffer-modified-p nil)) (max 0 (- indent (+format--current-indentation)))))
(with-current-buffer cur-buffer (set-buffer-modified-p nil))
(delete-region start end) (with-current-buffer cur-buffer
(goto-char start) (with-silent-modifications
(save-excursion (replace-region-contents start end (lambda () formatted-buffer) 5))
(insert-buffer-substring-no-properties formatted-buffer) (when callback (funcall callback))
(when callback (funcall callback))) (kill-buffer formatted-buffer))))))
(kill-buffer formatted-buffer)))))
(when (doom-region-active-p) (when (doom-region-active-p)
(setq deactivate-mark t))))) (setq deactivate-mark t)))))

View file

@ -46,40 +46,38 @@ mode unconditionally, call `+format-with-lsp-mode' instead."
(cl-defun +format-lsp-buffer (&rest plist &key buffer callback &allow-other-keys) (cl-defun +format-lsp-buffer (&rest plist &key buffer callback &allow-other-keys)
"Format the current buffer with any available lsp-mode or eglot formatter." "Format the current buffer with any available lsp-mode or eglot formatter."
(if-let* ((fn (with-current-buffer buffer (+format--lsp-fn))) (if-let* ((fn (with-current-buffer buffer (+format--lsp-fn)))
((apply fn plist))) ((apply fn (car +format--region-p) (cdr +format--region-p)
plist)))
(funcall callback) (funcall callback)
(funcall callback "LSP server doesn't support formatting"))) (funcall callback "LSP server doesn't support formatting")))
(cl-defun +format--with-lsp-mode (&key buffer scratch &allow-other-keys) (cl-defun +format--with-lsp-mode (beg end &key buffer &allow-other-keys)
"Format the current buffer with any available lsp-mode formatter." "Format the current buffer or region with any available lsp-mode formatter.
Won't forward the buffer to chained formatters if successful."
(with-current-buffer buffer (with-current-buffer buffer
(let ((edits (let ((edits
(cond (cond ((and (null beg) (lsp-feature? "textDocument/formatting"))
((lsp-feature? "textDocument/formatting") (lsp-request "textDocument/formatting"
(lsp-request "textDocument/formatting" (lsp--make-document-formatting-params)))
(lsp--make-document-formatting-params))) ((lsp-feature? "textDocument/rangeFormatting")
((lsp-feature? "textDocument/rangeFormatting") (lsp-request "textDocument/rangeFormatting"
(lsp-request "textDocument/rangeFormatting" (lsp--make-document-range-formatting-params
(lsp--make-document-range-formatting-params (or beg (point-min)) (or end (point-max)))))
(point-min) (point-max)))) ;; try next chained formatter(s)
(:err)))) ((cl-return (ignore (funcall callback)))))))
(unless (eq edits :err) (unless (seq-empty-p edits)
(unless (seq-empty-p edits) (lsp--apply-text-edits edits 'format))
(with-current-buffer scratch
(lsp--apply-text-edits edits 'format)))
t))))
(cl-defun +format--with-eglot (&key buffer scratch &allow-other-keys)
"Format the current buffer with any available eglot formatter."
(with-current-buffer scratch
(when (setq-local
eglot--cached-server
(with-current-buffer buffer
(when (or (eglot-server-capable :documentFormattingProvider)
(eglot-server-capable :documentRangeFormattingProvider))
(eglot-current-server))))
(let ((buffer-file-name (buffer-file-name buffer)))
(eglot-format-buffer))
t))) t)))
(cl-defun +format--with-eglot (beg end &key buffer callback &allow-other-keys)
"Format the current buffer or region with any available eglot formatter.
Won't forward the buffer to chained formatters if successful."
(with-current-buffer buffer
(or (with-demoted-errors "%s"
(always (eglot-format beg end)))
;; try next chained formatter(s)
(ignore (funcall callback)))))
;;; lsp.el ends here ;;; lsp.el ends here