9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

エディタ上でslim-lintのエラーを見えるようにした

Last updated at Posted at 2023-08-30

はじめに

普段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での設定例は以下です。

lsp.lua
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を使った場合、以下のようになります。

lsp.lua
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 },
    }
  }
})

実際に動かしてみると、こんな感じに表示できました。

image.png

VSCodeでもエラーを見えるようにしたい

VSCodeの拡張機能にefm-langserver-vscodeを使うとできます。

~/.config/efm-langserver/config.yamlの設定は以下のようにします。

~/.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

実際に動かしてみると、こんな感じに表示できました。

image.png

さいごに

efm-langserverをさわってみて、ものすごく汎用的で便利だなと感じたので、他にも使えないか模索してみようと思います。

9
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?