Function: apheleia--run-formatter-process

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

Signature

(apheleia--run-formatter-process COMMAND BUFFER REMOTE CALLBACK STDIN FORMATTER)

Documentation

Run a formatter using a shell command.

COMMAND should be a list of string or symbols for the formatter that will format the current buffer. See apheleia--run-formatters for a description of COMMAND, BUFFER, CALLBACK, REMOTE, and STDIN. FORMATTER is the symbol of the current formatter being run, for diagnostic purposes.

Source Code

;; Defined in /nix/store/lwcwryzabcmvjcwwzzs65jrwf7p7d7cs-emacs-packages-deps/share/emacs/site-lisp/elpa/apheleia-20260915.1628/apheleia-formatters.el
(defun apheleia--run-formatter-process
    (command buffer remote callback stdin formatter)
  "Run a formatter using a shell command.
COMMAND should be a list of string or symbols for the formatter that
will format the current buffer. See `apheleia--run-formatters' for a
description of COMMAND, BUFFER, CALLBACK, REMOTE, and STDIN. FORMATTER
is the symbol of the current formatter being run, for diagnostic
purposes."
  ;; NOTE: We switch to the original buffer both to format the command
  ;; correctly and also to ensure any buffer local variables correctly
  ;; resolve for the whole formatting process (for example
  ;; `apheleia--current-process').
  (with-current-buffer buffer
    (when-let* ((script-dir (expand-file-name
                             "scripts/formatters"
                             (file-name-directory
                              (file-truename
                               ;; Borrowed with love from Magit
                               (let ((load-suffixes '(".el")))
                                 (locate-library "apheleia"))))))
                ;; Gotta set both `exec-path' and the PATH env-var,
                ;; the former is for Emacs itself while the latter is
                ;; for subprocesses of the proc we start.
                (exec-path (cons script-dir exec-path))
                (process-environment
                 (cons (concat "PATH=" script-dir ":" (getenv "PATH"))
                       process-environment))
                (ctx
                 (apheleia--formatter-context formatter command remote stdin)))
      (if (executable-find (apheleia-formatter--arg1 ctx)
                           (eq apheleia-remote-algorithm 'remote))
          (apheleia--execute-formatter-process
           :ctx ctx
           :callback
           (lambda (err stdout)
             (if err
                 (funcall callback err stdout)
               (when-let*
                   ((output-fname (apheleia-formatter--output-fname ctx)))
                 ;; Load output-fname contents into the stdout buffer.
                 (with-current-buffer stdout
                   (erase-buffer)
                   (insert-file-contents-literally output-fname)))
               (funcall callback nil stdout)))
           :ensure
           (lambda ()
             (dolist (fname (list (apheleia-formatter--input-fname ctx)
                                  (apheleia-formatter--output-fname ctx)))
               (when fname
                 (ignore-errors (delete-file fname))))))
        (let ((errmsg
               (format
                "Could not find executable for formatter %s, skipping"
                formatter)))
          (apheleia--log 'process "%s" errmsg)
          (funcall callback (cons 'error errmsg) nil))))))