editor/format: refactor & reformat +format-buffer
Also switches cl-destructuring-bind to pcase-let to avoid breaking errors being thrown.
This commit is contained in:
parent
5e25cfd6a7
commit
c885cd2b9d
1 changed files with 17 additions and 20 deletions
|
@ -77,19 +77,6 @@ Stolen shamelessly from go-mode"
|
||||||
((error "Invalid rcs patch or internal error in +format--apply-rcs-patch")))))))
|
((error "Invalid rcs patch or internal error in +format--apply-rcs-patch")))))))
|
||||||
(move-to-column column)))
|
(move-to-column column)))
|
||||||
|
|
||||||
(defun +format--with-copy-of-buffer (formatter executable mode-result)
|
|
||||||
"Run formatter in a copy of the current buffer.
|
|
||||||
|
|
||||||
Since `format-all' functions (and various formatting functions, like `gofmt')
|
|
||||||
widen the buffer, in order to only format a region of text, we must make a copy
|
|
||||||
of the buffer to apply formatting to."
|
|
||||||
(if (buffer-narrowed-p)
|
|
||||||
(let ((output (buffer-substring-no-properties (point-min) (point-max))))
|
|
||||||
(with-temp-buffer
|
|
||||||
(insert output)
|
|
||||||
(funcall formatter executable mode-result)))
|
|
||||||
(funcall formatter executable mode-result)))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Public library
|
;; Public library
|
||||||
|
@ -115,8 +102,15 @@ See `+format/buffer' for the interactive version of this function, and
|
||||||
(user-error "Don't know how to format '%s' code" major-mode))
|
(user-error "Don't know how to format '%s' code" major-mode))
|
||||||
(let ((f-function (gethash formatter format-all-format-table))
|
(let ((f-function (gethash formatter format-all-format-table))
|
||||||
(executable (format-all-formatter-executable formatter)))
|
(executable (format-all-formatter-executable formatter)))
|
||||||
(cl-destructuring-bind (output errput first-diff)
|
(pcase-let
|
||||||
(+format--with-copy-of-buffer f-function executable mode-result)
|
((`(,output ,errput ,first-diff)
|
||||||
|
;; Since `format-all' functions (and various formatting functions,
|
||||||
|
;; like `gofmt') widen the buffer, in order to only format a region of
|
||||||
|
;; text, we must make a copy of the buffer to apply formatting to.
|
||||||
|
(let ((output (buffer-substring-no-properties (point-min) (point-max))))
|
||||||
|
(with-temp-buffer
|
||||||
|
(insert output)
|
||||||
|
(funcall f-function executable mode-result)))))
|
||||||
(unwind-protect
|
(unwind-protect
|
||||||
(cond ((null output) 'error)
|
(cond ((null output) 'error)
|
||||||
((eq output t) 'noop)
|
((eq output t) 'noop)
|
||||||
|
@ -126,14 +120,17 @@ See `+format/buffer' for the interactive version of this function, and
|
||||||
(coding-system-for-write 'utf-8))
|
(coding-system-for-write 'utf-8))
|
||||||
(unwind-protect
|
(unwind-protect
|
||||||
(progn
|
(progn
|
||||||
(with-current-buffer patchbuf (erase-buffer))
|
(with-current-buffer patchbuf
|
||||||
(with-temp-file tmpfile (erase-buffer) (insert output))
|
(erase-buffer))
|
||||||
|
(with-temp-file tmpfile
|
||||||
|
(erase-buffer)
|
||||||
|
(insert output))
|
||||||
(if (zerop (call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "-" tmpfile))
|
(if (zerop (call-process-region (point-min) (point-max) "diff" nil patchbuf nil "-n" "-" tmpfile))
|
||||||
'noop
|
'noop
|
||||||
(+format--apply-rcs-patch patchbuf)
|
(+format--apply-rcs-patch patchbuf)
|
||||||
(list output errput first-diff))))
|
(list output errput first-diff)))
|
||||||
(kill-buffer patchbuf)
|
(kill-buffer patchbuf)
|
||||||
(delete-file tmpfile))))
|
(delete-file tmpfile)))))
|
||||||
(unless (= 0 (length errput))
|
(unless (= 0 (length errput))
|
||||||
(message "Formatter error output:\n%s" errput))))))
|
(message "Formatter error output:\n%s" errput))))))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue