BiomeとNeovim

2023/12/06に公開

(多分)今話題のツールBiomeをNeovimで使っていきます。

はじめに

最近Biomeというツールを使っています。
一言で言えばRust製の超高速なLinter ✕ Formatterで、RomeというOSSから派生したツールです。
詳しくはこちらの記事に書かれています。

Biomeの使い方

インストール

下のコマンドでインストールできます。

npm i -g @biomejs/biome

初期化

biome init

上のコマンドで初期化すると、下のようなbiome.jsonが生成されます。

biome.json
{
  "$schema": "https://biomejs.dev/schemas/1.3.3/schema.json",
  "organizeImports": {
    "enabled": true
  },
  "linter": {
    "enabled": true,
    "rules": {
      "recommended": true
    }
  }
}

このbiome.jsonのカスタマイズはこのスクラップのように、とりあえず"all": trueにして逐一offにしていく方針で良さそうです。

lint

biome lint <file>

ファイルを修正するには

biome lint --apply <file>

format

biome format <file>

ファイルを実際にフォーマットするには

biome format --write <file>

筆者が実際に使ってみたところ、50行ほどのファイルのlintやformatがそれぞれ10~20ms程度でできる感じでした。
はやっ。

NeovimでBiomeを使う

プラグインマネージャーにはLazy.nvimを使います。

使うプラグイン

Mason

Mason.nvimではBiomeをLinterとして使います。
:Masonからbiomeをさがしてインストールするだけです。
また、ensure_installedに追加することで、自動でインストールさせることができます。

{
    "williamboman/mason.nvim",
    config = function()
        require("mason").setup({
            ensure_installed = {
                -- ...
                -- ...
+               "biome",
            }
        })
    end
}

formatter

formatter.nvimでBiomeをFormatterとして使います。
formatter.nvimのBiomeのデフォルトセットアップを使います。

{
  "mhartington/formatter.nvim",
  config = function()
    require("formatter").setup({
      filetype = {
        javascript = {require("formatter.filetypes.javascript").biome},
        javascriptreact = {require("formatter.filetypes.javascriptreact").biome},
        typescript = {require("formatter.filetypes.typescript").biome},
        typescriptreact = {require("formatter.filetypes.typescriptreact").biome},
      },
    })
  end,
}

また、カスタムのオプションをつけたいときは下のようにすることで設定できます。

{
  "mhartington/formatter.nvim",
  config = function()
    local biome = function()
      local util = require("formatter.util")
      return {
        exe = "biome",
        args = {
          "format",
+         "--indent-width=4",
+         "--indent-style=space",
          -- `--stdin-file-path`の後にutil.escape_path(...)が来るようにする
          "--stdin-file-path",
          util.escape_path(util.get_current_buffer_file_path()),
        },
        stdin = true,
      }
    end
      require("formatter").setup({
        filetype = {
          javascript = { biome },
          javascriptreat = { biome },
          typescriptreact = { biome },
          typescript = { biome },
        },
      })
  end,
}

このように設定して、:Formatを実行するとファイルがフォーマットされます。
また、:FormatWriteを実行するとファイルがフォーマットされて、保存されます。
Biomeではデフォルトでインデントを2つで行っていますが、--indent-width=4をオプションを付けて、インデントを4つで行っています。
Biomeでは--indent-sizeはdeprecatedになっているのでエラーを吐かれます。

またBiomeでは--indent-styles=tabになっていたので、--indent-style=spaceに変えています。

おわりに

ということで、Biomeの紹介が多くなりましたが、終わり。

Biomeのホームページ→ https://biomejs.dev
BiomeのGithubレポジトリ→ https://github.com/biomejs/biome
Biomeのdiscordサーバー→ https://discord.gg/BypW39g6Yc (japaneseのチャンネルもあるのでおすすめ)
BiomeのTwitter→ https://twitter.com/biomejs

参考文献

https://www.reddit.com/r/neovim/comments/16g6cei/neovim_biome_setup/

https://zenn.dev/voluntas/scraps/31de0e6155b43e

https://github.com/mhartington/formatter.nvim#readme

https://biomejs.dev

Discussion