Function: bazel-buildifier

bazel-buildifier is an interactive and natively compiled function defined in bazel.el.

Signature

(bazel-buildifier &optional TYPE)

Documentation

Format current buffer using Buildifier.

If TYPE is nil, detect the file type from the current major mode and visited filename, if available. Otherwise, TYPE must be one of the symbols ‘build’, ‘bzl’, ‘workspace’, ‘module’, or
‘default’, corresponding to the file types documented at URL
‘https://github.com/bazelbuild/buildtools/tree/master/buildifier#usage’.

Key Bindings

bazel-mode-map C-c C-f

Source Code

;; Defined in /nix/store/ws8pkszjvcad1rgzqw6wja7qxacylfhn-emacs-packages-deps/share/emacs/site-lisp/elpa/bazel-20261002.1510/bazel.el
(defun bazel-buildifier (&optional type)
  "Format current buffer using Buildifier.
If TYPE is nil, detect the file type from the current major mode
and visited filename, if available.  Otherwise, TYPE must be one
of the symbols ‘build’, ‘bzl’, ‘workspace’, ‘module’, or
‘default’, corresponding to the file types documented at URL
‘https://github.com/bazelbuild/buildtools/tree/master/buildifier#usage’."
  (declare (ftype (function
                   (&optional (member build bzl workspace module default))
                   null)))
  (interactive "*" bazel-mode)
  (cl-check-type type (member nil build bzl workspace module default))
  (let ((directory default-directory)
        (input-file buffer-file-name)
        (temp-buffer (temp-buffer-window-setup "*buildifier*"))
        (type (or type bazel--buildifier-type)))
    ;; Run Buildifier on a file to support remote BUILD files.
    (bazel--with-temp-file temp-file
        (make-nearby-temp-file "buildifier-input-")
      (write-region (point-min) (point-max) temp-file nil :silent)
      (let* ((default-directory directory)
             (inhibit-read-only t)
             (process-file-side-effects t)
             (return-code
              (apply #'process-file bazel-buildifier-command nil temp-buffer nil
                     `(,@(bazel--buildifier-file-flags type input-file)
                       "--" ,(file-name-unquote (file-local-name temp-file))))))
        (if (eq return-code 0)
            (progn
              (insert-file-contents temp-file nil nil nil :replace)
              (kill-buffer temp-buffer))
          (with-current-buffer temp-buffer
            (when-let* ((root (bazel--repository-root (or input-file directory))))
              ;; Files in Buildifier error messages are local to the repository
              ;; root.  Make sure that ‘next-error’ finds them.
              (setq-local default-directory root))
            (goto-char (point-max))
            (insert ?\n "Process buildifier "
                    (if (stringp return-code)
                        (downcase return-code)  ; signal name
                      (format "exited abnormally with code %d" return-code))
                    ?\n)
            (goto-char (point-min))
            (compilation-minor-mode))
          (temp-buffer-window-show temp-buffer)))))
  nil)