はじめに
普段slim-lintを使っていて、CIやlint-stagedよりも手前のタイミングでエラーを先に確認できるといいなと思い、efm-langserverを導入してslim-lintを動かせるようにしたので紹介します。
efm-langserverについて
Language ServerがないリントツールやフォーマッタをLanguage Serverにできるツールです。
インストールは、Homebrewだと以下のようにします。
詳しくはREADMEを参照してください。
$ brew install efm-langserver
slim-lintをefm-langserverで動かすための設定を追加する
Neovim, nvim-lspconfigでの設定例は以下です。
local lspconfig = require('lspconfig')
lspconfig.efm.setup({
init_options = { documentFormatting = true },
settings = {
languages = {
slim = {
lintCommand = "slim-lint --stdin-file-path ${INPUT}",
lintStdin = true,
lintFormats = { '%f:%l [%t] %m' },
}
}
}
})
lintFormatsには、slim-lintの出力に合わせてVimのerrorformats
を設定します。
Description: List of Vim errorformats to capture. See: https://vimhelp.org/quickfix.txt.html#errorformats. If this is not expressive enough, you can edit the lint-command to do some preprocessing, e.g. using sed or jq.
efm-langserver uses a Go implementation to parse the errors, which comes with a CLI for quick testing: https://github.com/reviewdog/errorformat
また、efmls-configs-nvimを使うと、基本的な設定を簡単に行なえます。
efmls-configs-nvimを使った場合、以下のようになります。
local lspconfig = require('lspconfig')
local slim_lint = require('efmls-configs.linters.slim_lint')
lspconfig.efm.setup({
init_options = { documentFormatting = true },
settings = {
languages = {
slim = { slim_lint },
}
}
})
実際に動かしてみると、こんな感じに表示できました。
VSCodeでもエラーを見えるようにしたい
VSCodeの拡張機能にefm-langserver-vscode
を使うとできます。
~/.config/efm-langserver/config.yaml
の設定は以下のようにします。
version: 2
tools:
slim_lint: &slim_lint
lint-command: 'slim-lint --stdin-file-path ${INPUT}'
lint-stdin: true
lint-formats:
- '%f:%l [%t] %m'
languages:
slim:
- <<: *slim_lint
実際に動かしてみると、こんな感じに表示できました。
さいごに
efm-langserverをさわってみて、ものすごく汎用的で便利だなと感じたので、他にも使えないか模索してみようと思います。