Function: apheleia--run-formatters

apheleia--run-formatters is a natively compiled function defined in apheleia-formatters.el.

Signature

(apheleia--run-formatters FORMATTERS BUFFER REMOTE CALLBACK &optional STDIN)

Documentation

Run one or more code formatters on the current buffer.

FORMATTERS is a list of symbols that appear as keys in apheleia-formatters. BUFFER is the current-buffer when this function was first called.

CALLBACK is always invoked unless there is a synchronous nonlocal exit, the first argument is nil or an error. In the case of no error, the second argument is a buffer containing the output of all the formatters, otherwise it is nil.

REMOTE asserts whether the buffer being formatted is on a remote machine or the current machine. It should be the output of file-remote-p on the current variable buffer-file-name(var)/buffer-file-name(fun). REMOTE is the remote part of the original buffers file-name or directory'. It's used alongside apheleia-remote-algorithm to determine where the formatter process and any temporary files it may need should be placed.

STDIN is a buffer containing the standard input for the first formatter in COMMANDS. This should not be supplied by the caller and instead is supplied by this command when invoked recursively. The stdout of the previous formatter becomes the stdin of the next formatter.

Source Code

;; Defined in /nix/store/lwcwryzabcmvjcwwzzs65jrwf7p7d7cs-emacs-packages-deps/share/emacs/site-lisp/elpa/apheleia-20260915.1628/apheleia-formatters.el
(cl-defun apheleia--run-formatters
    (formatters buffer remote callback &optional stdin)
  "Run one or more code formatters on the current buffer.
FORMATTERS is a list of symbols that appear as keys in
`apheleia-formatters'. BUFFER is the `current-buffer' when this
function was first called.

CALLBACK is always invoked unless there is a synchronous nonlocal
exit, the first argument is nil or an error. In the case of no
error, the second argument is a buffer containing the output of
all the formatters, otherwise it is nil.

REMOTE asserts whether the buffer being formatted is on a remote
machine or the current machine. It should be the output of
`file-remote-p' on the current variable `buffer-file-name'.
REMOTE is the remote part of the original buffers file-name or
directory'. It's used alongside `apheleia-remote-algorithm' to
determine where the formatter process and any temporary files it
may need should be placed.

STDIN is a buffer containing the standard input for the first
formatter in COMMANDS. This should not be supplied by the caller
and instead is supplied by this command when invoked recursively.
The stdout of the previous formatter becomes the stdin of the
next formatter."
  (apheleia--log
   'run-formatter
   "Running formatters %S on buffer %S" formatters buffer)
  (let ((command (alist-get (car formatters) apheleia-formatters)))
    (funcall
     (cond
      ((consp command)
       #'apheleia--run-formatter-process)
      ((or (functionp command)
           (symbolp command))
       #'apheleia--run-formatter-function)
      (t
       (error "Formatter must be a shell command or a Lisp \
function: %s" command)))
     command
     buffer
     remote
     (lambda (err stdout)
       (if err
           (funcall callback err stdout)
         (condition-case-unless-debug err
             (unless (string-empty-p
                      (with-current-buffer stdout (buffer-string)))
               (if (cdr formatters)
                   ;; Forward current stdout to remaining formatters,
                   ;; passing along the current callback and using the
                   ;; current formatters output as stdin.
                   (apheleia--run-formatters
                    (cdr formatters) buffer remote callback stdout)
                 (funcall callback nil stdout)))
           (error (funcall callback err nil)))))
     stdin
     (car formatters))))