editor/format: refactor +format-buffer & users

This commit is contained in:
Henrik Lissner 2018-08-30 14:57:31 +02:00
parent 1b4040229e
commit 349fb49c7b
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -96,7 +96,7 @@ of the buffer to apply formatting to."
;; ;;
;;;###autoload ;;;###autoload
(defun +format-buffer () (defun +format-buffer (formatter mode-result)
"Format the source code in the current buffer. "Format the source code in the current buffer.
Returns any of the following values: Returns any of the following values:
@ -111,34 +111,31 @@ position of the first change in the buffer.
See `+format/buffer' for the interactive version of this function, and See `+format/buffer' for the interactive version of this function, and
`+format|buffer' to use as a `before-save-hook' hook." `+format|buffer' to use as a `before-save-hook' hook."
(require 'format-all) (unless formatter
(cl-destructuring-bind (formatter mode-result) (user-error "Don't know how to format '%s' code" major-mode))
(format-all-probe) (let ((f-function (gethash formatter format-all-format-table))
(unless formatter (executable (format-all-formatter-executable formatter)))
(error "Don't know how to format %S code" major-mode)) (cl-destructuring-bind (output errput first-diff)
(let ((f-function (gethash formatter format-all-format-table)) (+format--with-copy-of-buffer f-function executable mode-result)
(executable (format-all-formatter-executable formatter))) (unwind-protect
(cl-destructuring-bind (output errput first-diff) (cond ((null output) 'error)
(+format--with-copy-of-buffer f-function executable mode-result) ((eq output t) 'noop)
(unwind-protect ((let ((tmpfile (make-temp-file "doom-format"))
(cond ((null output) 'error) (patchbuf (get-buffer-create " *doom format patch*"))
((eq output t) 'noop) (coding-system-for-read 'utf-8)
((let ((tmpfile (make-temp-file "doom-format")) (coding-system-for-write 'utf-8))
(patchbuf (get-buffer-create " *doom format patch*")) (unwind-protect
(coding-system-for-read 'utf-8) (progn
(coding-system-for-write 'utf-8)) (with-current-buffer patchbuf (erase-buffer))
(unwind-protect (with-temp-file tmpfile (erase-buffer) (insert output))
(progn (if (zerop (call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "-" tmpfile))
(with-current-buffer patchbuf (erase-buffer)) 'noop
(with-temp-file tmpfile (erase-buffer) (insert output)) (+format--apply-rcs-patch patchbuf)
(if (zerop (call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "-" tmpfile)) (list output errput first-diff))))
'noop (kill-buffer patchbuf)
(+format--apply-rcs-patch patchbuf) (delete-file tmpfile))))
(list output errput first-diff)))) (unless (= 0 (length errput))
(kill-buffer patchbuf) (message "Formatter error output:\n%s" errput))))))
(delete-file tmpfile))))
(unless (= 0 (length errput))
(message "Formatter error output:\n%s" errput)))))))
;; ;;
@ -149,10 +146,7 @@ See `+format/buffer' for the interactive version of this function, and
(defun +format/buffer () (defun +format/buffer ()
"Format the source code in the current buffer." "Format the source code in the current buffer."
(interactive) (interactive)
(pcase (+format-buffer) (+format|buffer))
(`error (message "Failed to format buffer due to errors"))
(`noop (message "Buffer was already formatted"))
(_ (message "Formatted (%s)" (car (format-all-probe))))))
;;;###autoload ;;;###autoload
(defun +format/region (beg end) (defun +format/region (beg end)
@ -186,5 +180,13 @@ is selected)."
(add-hook 'before-save-hook #'+format|buffer nil t)) (add-hook 'before-save-hook #'+format|buffer nil t))
;;;###autoload ;;;###autoload
(defalias '+format|buffer #'+format-buffer) (defun +format|buffer ()
"Format the source code in the current buffer with minimal feedback.
Meant for `before-save-hook'."
(cl-destructuring-bind (formatter mode-result) (format-all-probe)
(pcase (+format-buffer formatter mode-result)
(`error (message "Failed to format buffer due to errors"))
(`noop (message "Buffer was already formatted"))
(_ (message "Formatted (%s)" formatter)))))