LoginSignup
3
0

More than 1 year has passed since last update.

coc.nvim + efm-langserver でerbファイルの文法チェックをおこなう

Last updated at Posted at 2022-08-17

Vimで.erbファイルを書きたい

Rails7のHotwireを試したくて、(nvimで)何年かぶりにeRubyを書いてみてるのだけど、eRuby向けのLanguage Serverが見当たらなくて泣きそうになった。
せめて文法エラーのチェックくらいはできたら・・・1

そこで、どんなコマンドでもLanguage Serverにしてしまえる efm-langserver を使ってみることにした。

efm-langserverのREADMEには、

lint-command: 'erb -x -T - | ruby -c'

書いてあるが、これだと以下のようなブロックを含むeRubyのコードを扱えない。

<%= simple_form_for(quote, html: { class: "quote form" }) do |f| %>
  <%= f.input :name, input_html: { autofocus: true } %>
  <%= f.submit class: "btn btn--secondary" %>
<% end %>

ブロックがあると常に SyntaxError になってしまう。

$ (erb -x -T - | ruby -c) <<'EOF'
> <%= simple_form_for(quote, html: { class: "quote form" }) do |f| %>
  <%= f.input :name, input_html: { autofocus: true } %>
  <%= f.submit class: "btn btn--secondary" %>
<% end %>
> EOF
-:2: syntax error, unexpected ')'
...class: "quote form" }) do |f| ).to_s); _erbout.<< "\n".freeze
-:5: syntax error, unexpected `end', expecting ')'
;  end ; _erbout.<< "\n".freeze
-:6: syntax error, unexpected end-of-input, expecting ')'
; _erbout

これはRubyの標準ライブラリのERBがブロックを含むeRubyをハンドリングしないのが原因。
Railsで使われているeRubyライブラリのerubiも同様だった。

かわりに使えるものを探してみると、baby_erubisというのを見つけた。

もう長らくメンテされていないが、

(Experimental) Block argument expression supported

書いてある

こんな感じで試してみると、ブロックの含まれるeRubyタグのハンドリングが期待通りにできた。

gem install baby_erubis
cat ./app/views/quotes/index.html.erb | ruby -r baby_erubis -e 'puts BabyErubis::Html.new.from_str(STDIN.read).src'

そこで、erb -x -T - | ruby -c のかわりにためしに使ってみることにした。

セットアップ

coc.nvim は導入済みの前提。

efm-langserver と baby_erubis をいれる。

brew install efm-langserver
gem install baby_erubis

ファイルタイプがerubyのときにefm-langserverを呼び出すようにする。
(ちなみに、FileTypeがerubyのときにはcoc-htmlも使われるようになっている)

.config/nvim/coc-settings.json
{
  "html.filetypes": ["html", "eruby"],
  "html.format.templating": true,
  "languageserver": {
    "efm": {
      "command": "efm-langserver",
      "args": [],
      "filetypes": ["eruby"]
    }
  }
}

以下のRubyスクリプトをパスの通った場所に置いて実行権限をつける。
これが erb -x -T - | ruby -c のかわりになる。

erbcheck
#!/usr/bin/env ruby

# https://github.com/kwatch/baby_erubis
require "baby_erubis"

begin
  BabyErubis::Html.new.from_str(STDIN.read, '-').src
  puts "Syntax OK"
rescue SyntaxError => se
  STDERR.puts se.message
  exit 1
end

.config/efm-langserver/config.yaml を作成して、以下の内容にする。
(efm-langserver のREADMEに書いてあるものとほぼ一緒)

.config/efm-langserver/config.yaml
version: 2
tools:
  eruby-erb: &eruby-erb
    lint-debounce: 2s
    lint-command: 'erbcheck'
    lint-stdin: true
    lint-offset: 0
    format-stdin: true
    format-command: htmlbeautifier

languages:
  eruby:
    - <<: *eruby-erb

あとはvimでてきとうな.erbファイルを開くと、文法チェックをおこなってくれるようになる。

スクリーンショット 2022-08-18 4.55.00.png
スクリーンショット 2022-08-18 4.55.04.png

これにて一件落着。

今後の課題

文法チェックはできるようになったけど、欲を言えばeRubyのRuby部分の自動補完とかもやってほしいので、solargraphと組み合わせてどうにかできないか試してみたい。

  1. Ruby自体のLanguage Serverはsolargraphというのがあり、だましだましだがRailsもサポートされている

3
0
3

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
3
0