Function: apheleia--run-formatter-function

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

Signature

(apheleia--run-formatter-function FUNC BUFFER REMOTE CALLBACK STDIN FORMATTER)

Documentation

Run a formatter using a Lisp function FUNC.

See apheleia--run-formatters for a description of BUFFER, REMOTE, CALLBACK 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-function
    (func buffer remote callback stdin formatter)
  "Run a formatter using a Lisp function FUNC.
See `apheleia--run-formatters' for a description of BUFFER, REMOTE,
CALLBACK and STDIN. FORMATTER is the symbol of the current formatter
being run, for diagnostic purposes."
  (let* ((formatter-name (if (symbolp func) (symbol-name func) "lambda"))
         (scratch (generate-new-buffer
                   (format " *apheleia-%s-scratch*" formatter-name))))
    (with-current-buffer scratch
      ;; We expect FUNC to modify scratch in place so we can't simply pass
      ;; STDIN to it. When STDIN isn't nil, it's the output of a previous
      ;; formatter and we want to keep it alive so we can debug any issues
      ;; with it.
      (insert-buffer-substring (or stdin buffer))
      (funcall func
               ;; Original buffer being formatted. This shouldn't be
               ;; modified. You can use it to check things like the
               ;; current major mode, or the buffer filename. If you
               ;; use it as input for the formatter, your formatter
               ;; won't work when chained after another formatter.
               :buffer buffer
               ;; Buffer the formatter should modify. This starts out
               ;; containing the original file contents, which will be
               ;; the same as `buffer' except it has already been
               ;; transformed by any formatters that ran previously.
               :scratch scratch
               ;; Name of the current formatter symbol, e.g. `black'.
               :formatter formatter
               ;; Callback. Should pass an error value (cons of symbol
               ;; and data, like for `signal') or nil. For backwards
               ;; compatibility it can also invoke only on success,
               ;; with no args.
               :callback
               (lambda (&optional err)
                 (unwind-protect
                     (funcall callback err (when (not err) scratch))
                   (kill-buffer scratch)))
               ;; The remote part of the buffers file-name or directory.
               :remote remote
               ;; Whether the formatter should be run async or not.
               :async (not remote)
               ;; Callback when formatting scratch has failed.
               :callback
               (apply-partially #'kill-buffer scratch)))))