Call apheleia-format-buffer normally when there's no region

This commit is contained in:
Jeremy Dormitzer 2024-08-29 13:25:24 -04:00
parent d5030f749f
commit 47727d73f9

View File

@ -291,6 +291,8 @@
(defun apheleia () (defun apheleia ()
"Format the region or current buffer using Apheleia." "Format the region or current buffer using Apheleia."
(interactive) (interactive)
(if (not (region-active-p))
(call-interactively #'apheleia-format-buffer)
(let* ((buf (current-buffer)) (let* ((buf (current-buffer))
(name (buffer-file-name)) (name (buffer-file-name))
(temp-file (make-temp-file "apheleia" nil (format ".%s" (file-name-extension name)))) (temp-file (make-temp-file "apheleia" nil (format ".%s" (file-name-extension name))))
@ -298,13 +300,8 @@
(formatters (apheleia--get-formatters)) (formatters (apheleia--get-formatters))
(line (line-number-at-pos)) (line (line-number-at-pos))
(col (current-column)) (col (current-column))
start (start (region-beginning))
end) (end (region-end)))
(if (region-active-p)
(setq start (region-beginning)
end (region-end))
(setq start (point-min)
end (point-max)))
(with-current-buffer temp-buffer (with-current-buffer temp-buffer
(erase-buffer) (erase-buffer)
(insert-buffer-substring buf start end) (insert-buffer-substring buf start end)
@ -314,8 +311,7 @@
(with-current-buffer buf (with-current-buffer buf
(delete-region start end) (delete-region start end)
(insert (with-current-buffer temp-buffer (insert (with-current-buffer temp-buffer
(string-trim (buffer-substring-no-properties (point-min) (point-max))))
(buffer-substring-no-properties (point-min) (point-max)))))
(goto-char (point-min)) (goto-char (point-min))
(forward-line (1- line)) (forward-line (1- line))
(move-to-column col) (move-to-column col)
@ -332,6 +328,7 @@
(set-buffer-modified-p nil)) (set-buffer-modified-p nil))
(kill-buffer temp-buffer)) (kill-buffer temp-buffer))
(error "Formatting failed: %s" error))))))) (error "Formatting failed: %s" error)))))))
)
(keymap-set prog-mode-map "C-c f" #'apheleia) (keymap-set prog-mode-map "C-c f" #'apheleia)