fix(format): disengage selection after +format-region

Fix: #7951
This commit is contained in:
Henrik Lissner 2024-07-16 11:34:14 -04:00
parent 3f66400d62
commit 13ed89f235
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -18,44 +18,47 @@
(cur-buffer (current-buffer))
(formatted-buffer (get-buffer-create " *apheleia-formatted*"))
(indent 0))
(with-current-buffer formatted-buffer
(erase-buffer)
(unless (featurep :system 'windows)
(setq-local coding-system-for-read 'utf-8)
(setq-local coding-system-for-write 'utf-8))
;; Ensure this temp buffer seems as much like the origin buffer as
;; possible, in case the formatter is an elisp function, like `gofmt'.
(cl-loop for (var . val)
in (cl-remove-if-not #'listp (buffer-local-variables cur-buffer))
;; `enable-multibyte-characters' can change how Emacs reads the
;; buffer's contents (or writes them to the formatters), which
;; can cause errors.
unless (eq var 'enable-multibyte-characters)
do (set (make-local-variable var) val))
;;
(insert-buffer-substring-no-properties cur-buffer start end)
;; Since we're piping a region of text to the formatter, remove any
;; leading indentation to make it look like a file.
(setq indent (+format--current-indentation))
(when (> indent 0)
(indent-rigidly (point-min) (point-max) (- indent)))
;;
(apheleia-format-buffer
command
(lambda ()
(with-current-buffer formatted-buffer
(when (> indent 0)
;; restore indentation without affecting new indentation
(indent-rigidly (point-min) (point-max)
(max 0 (- indent (+format--current-indentation)))))
(set-buffer-modified-p nil))
(with-current-buffer cur-buffer
(delete-region start end)
(goto-char start)
(save-excursion
(insert-buffer-substring-no-properties formatted-buffer)
(when callback (funcall callback)))
(kill-buffer formatted-buffer)))))))
(unwind-protect
(with-current-buffer formatted-buffer
(erase-buffer)
(unless (featurep :system 'windows)
(setq-local coding-system-for-read 'utf-8)
(setq-local coding-system-for-write 'utf-8))
;; Ensure this temp buffer seems as much like the origin buffer as
;; possible, in case the formatter is an elisp function, like `gofmt'.
(cl-loop for (var . val)
in (cl-remove-if-not #'listp (buffer-local-variables cur-buffer))
;; `enable-multibyte-characters' can change how Emacs reads the
;; buffer's contents (or writes them to the formatters), which
;; can cause errors.
unless (eq var 'enable-multibyte-characters)
do (set (make-local-variable var) val))
;;
(insert-buffer-substring-no-properties cur-buffer start end)
;; Since we're piping a region of text to the formatter, remove any
;; leading indentation to make it look like a file.
(setq indent (+format--current-indentation))
(when (> indent 0)
(indent-rigidly (point-min) (point-max) (- indent)))
;;
(apheleia-format-buffer
command
(lambda ()
(with-current-buffer formatted-buffer
(when (> indent 0)
;; restore indentation without affecting new indentation
(indent-rigidly (point-min) (point-max)
(max 0 (- indent (+format--current-indentation)))))
(set-buffer-modified-p nil))
(with-current-buffer cur-buffer
(delete-region start end)
(goto-char start)
(save-excursion
(insert-buffer-substring-no-properties formatted-buffer)
(when callback (funcall callback)))
(kill-buffer formatted-buffer)))))
(when (doom-region-active-p)
(setq deactivate-mark t)))))
;;