biomejs-format is a function that formats the current buffer using Biome. The
package also exports a minor mode that applies (biomejs-format)
on save.
This is forked from https://github.com/prettier/prettier-emacs and made for users who switched from Prettier to Biome, to replace that easily.
Ensure that the biome program is installed:
which biome
If biome is not installed already, please refer https://biomejs.dev/guides/getting-started/#installation .
First require the package:
(require 'biomejs-format)
Then you can hook to your favorite javascript mode:
(add-hook 'js2-mode-hook 'biomejs-format-mode)
(add-hook 'web-mode-hook 'biomejs-format-mode)
...
To adjust the CLI args used for the biome command, you can customize the biomejs-format-biome-args
variable:
(setq biomejs-format-biome-args '(
"format"
"--trailing-comma" "all"
"--bracket-spacing" "false"
))
Web-mode is a popular mode for editing .js and .jsx files, but it is used to edit other template files too. If you want to hook biomejs-format to web-mode for .js and .jsx files only, you can define a helper function like this:
(defun enable-minor-mode (my-pair)
"Enable minor mode if filename match the regexp. MY-PAIR is a cons cell (regexp . minor-mode)."
(if (buffer-file-name)
(if (string-match (car my-pair) buffer-file-name)
(funcall (cdr my-pair)))))
And then hook to web-mode like this:
(add-hook 'web-mode-hook #'(lambda ()
(enable-minor-mode
'("\\.jsx?\\'" . biomejs-format-mode))))
This package requires the diff
tool which is already included on Unix platforms. The simplest way to install diff
on Windows is to use Chocolatey. The steps are as follows:
- Follow the Chocolatey install instructions: https://chocolatey.org/install
- Open an Admin Powershell session
- Install the
diff
program:choco install diffutils
You should now be able to open Emacs and successfully use this package.
This package is customizable via custom.el:
M-x customize-group biomejs-format
biomejs-format-biome-command
is the biome commandbiomejs-format-biome-args
are the args passed to the biome commandbiomejs-format-show-errors
customizes where to display the error output (buffer, echo or nil)biomejs-format-width-mode
customizes the width when formatting buffer contents (window, fill or nil)
If you want to use your project's biome version you can rely on https://github.com/codesuki/add-node-modules-path
(eval-after-load 'web-mode
'(progn
(add-hook 'web-mode-hook #'add-node-modules-path)
(add-hook 'web-mode-hook #'biomejs-format-mode)))
- https://github.com/cxa/lsp-biome provides integration for
lsp-mode
with more features. - https://github.com/purcell/emacs-reformatter provides smarter way to define reformatters.