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