Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #25 hook for post-processing formatter output #26

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions reformatter.el
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@
(require 'cl-lib))
(require 'ansi-color)

(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 (functionp output-processor))
(when (and input-file
(buffer-file-name)
(string= (file-truename input-file)
Expand Down Expand Up @@ -119,9 +120,8 @@ 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)))
(reformatter-replace-buffer-contents-from-file
(funcall output-processor (if stdout stdout-file input-file))))
;; If there are no errors then we hide the error buffer
(delete-windows-on error-buffer))
(if display-errors
Expand All @@ -131,7 +131,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 'identity))
"Define a reformatter command with NAME.

When called, the reformatter will use PROGRAM and any ARGS to
Expand Down Expand Up @@ -222,10 +222,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 :group :lighter :keymap :exit-code-success-p :output-post-processor)))
(cl-assert (symbolp name))
(cl-assert (functionp exit-code-success-p))
(cl-assert (functionp output-processor))
(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 @@ -271,7 +278,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