Skip to content

Commit

Permalink
fix purcell#25 hook for post-processing formatter output
Browse files Browse the repository at this point in the history
  • Loading branch information
wyuenho committed Nov 23, 2023
1 parent ebea795 commit 19f2024
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions reformatter.el
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ otherwise as it cannot create intermediate directories."
(make-temp-file
(replace-regexp-in-string "/" "_" (symbol-name sym))))

(defun reformatter--do-region (name beg end program args stdin stdout input-file exit-code-success-p display-errors)
(defun reformatter--do-region (name beg end program args stdin stdout input-file exit-code-success-p output-processor display-errors)
"Do the work of reformatter called NAME.
Reformats the current buffer's region from BEG to END using
PROGRAM and ARGS. For args STDIN, STDOUT, INPUT-FILE,
EXIT-CODE-SUCCESS-P and DISPLAY-ERRORS see the documentation of
the `reformatter-define' macro."
(cl-assert input-file)
(cl-assert (functionp exit-code-success-p))
(cl-assert (or (null output-processor) (functionp exit-code-success-p)))
(when (and input-file
(buffer-file-name)
(string= (file-truename input-file)
Expand Down Expand Up @@ -130,9 +131,19 @@ the `reformatter-define' macro."
;; disruption to marker positions and the
;; undo list
(narrow-to-region beg end)
(reformatter-replace-buffer-contents-from-file (if stdout
stdout-file
input-file)))
(let* ((output-file (if stdout stdout-file input-file))
result-file
(result-callback (lambda (output)
(setq result-file
(and output
(reformatter-temp-file-in-current-directory nil output))))))
(if (functionp output-processor)
(progn
(funcall output-processor output-file result-callback)
(when result-file
(reformatter-replace-buffer-contents-from-file result-file)
(delete-file result-file)))
(reformatter-replace-buffer-contents-from-file output-file))))
;; If there are no errors then we hide the error buffer
(delete-windows-on error-buffer))
(if display-errors
Expand All @@ -142,7 +153,7 @@ the `reformatter-define' macro."
(delete-file stdout-file))))

;;;###autoload
(cl-defmacro reformatter-define (name &key program args (mode t) (stdin t) (stdout t) input-file lighter keymap group (exit-code-success-p 'zerop))
(cl-defmacro reformatter-define (name &key program args (mode t) (stdin t) (stdout t) input-file lighter keymap group (exit-code-success-p 'zerop) output-processor)
"Define a reformatter command with NAME.
When called, the reformatter will use PROGRAM and any ARGS to
Expand Down Expand Up @@ -234,10 +245,17 @@ EXIT-CODE-SUCCESS-P
which accepts an integer process exit code, and returns non-nil
if that exit code is considered successful. This could be a
lambda, quoted symbol or sharp-quoted symbol. If not supplied,
the code is considered successful if it is `zerop'."
(declare (indent defun))
the code is considered successful if it is `zerop'.
OUTPUT-PROCESSOR
If provided, this is a function that takes the output PROGRAM,
do some arbitrary processing to it, and then return the final
output. If not supplied, the output is returned as is."
(declare (indent defun) (debug (name :program :args :mode :stdin :stdout :input-file :lighter :keymap :group :exit-code-success-p :output-processor)))
(cl-assert (symbolp name))
(cl-assert (functionp exit-code-success-p))
(cl-assert (or (null output-processor) (functionp exit-code-success-p)))
(cl-assert program)
;; Note: we skip using `gensym' here because the macro arguments are only
;; referred to once below, but this may have to change later.
Expand Down Expand Up @@ -285,7 +303,7 @@ DISPLAY-ERRORS, shows a buffer if the formatting fails."
(reformatter--do-region
',name beg end
,program ,args ,stdin ,stdout input-file
#',exit-code-success-p display-errors))
#',exit-code-success-p #',output-processor display-errors))
(when (file-exists-p input-file)
(delete-file input-file)))))

Expand Down

0 comments on commit 19f2024

Please sign in to comment.