LoginSignup
0
0

More than 1 year has passed since last update.

NeovimのDiagnosticsに文字列でフィルタをかける

Last updated at Posted at 2022-12-03

やること

Neovimを使っていると、LSPが下記のようなDiagnostics("randint" is ...)を吐く。
image.png
このDiagnostics機能そのものを無効にするのではなく、「Warning以下非表示」等の制限をかけるのでもなく、特定の文字列を含むものだけを非表示にする。

やりかた

  • neovim/runtime/lua/vim/diagnostic.luaの785行目あたりに、下記を追加する。そしてNeovimを再起動。
neovim/runtime/lua/vim/diagnostic.lua
  for i = 1, #line_diags - 1 do
    table.insert(virt_texts, { prefix, virtual_text_highlight_map[line_diags[i].severity] })
  end
  local last = line_diags[#line_diags]

  -- ここから追加
  if string.find(last.message, "unbound") or string.find(last.message, "is not accessed") then
    return nil
  end
  -- ここまで

  -- TODO(tjdevries): Allow different servers to be shown first somehow?
  -- TODO(tjdevries): Display server name associated with these?
  if last.message then
    table.insert(virt_texts, {
      string.format('%s %s', prefix, last.message:gsub('\r', ''):gsub('\n', '  ')),
      virtual_text_highlight_map[last.severity],
    })
  • もちろんstring.find(last.message, "xxx")xxxを好きに変えて、orでつないであげれば、複数の文字列にフィルタをかけられる。
  • MacでNeovimを利用している人なら、Neovimを入れた場所を探して、Neovim/share/nvim/runtime/lua/vim/diagnostic.luaを編集するということ。
  • Neovim/share/nvim/runtime/lua/vim/lsp/diagnostic.luaではないので注意。
    • diagnostic.luaの前に/lsp/はいらない。

おまけ

  • 上述コードの少し上にあるlocal prefix = opts.prefix or '■'を書き換えれば、インラインDiagnosticの先頭部分を変えられる。
neovim/runtime/lua/vim/diagnostic.lua
  local prefix = opts.prefix or '👀'

image.png

ノート

  • 2022/12/03現在、日本語・英語問わずでDiagnosticsに文字列でフィルタをかける方法が、ネット上に見つけられない。
  • 私はNeovimのコードを読み込んだわけでも、普段からluaを使っているわけでもないので、ご利用は自己責任でお願いします。

追記

下記の書き方の方がスマート。

neovim/runtime/lua/vim/diagnostic.lua
  if last.message:find("unbound") or 
     last.message:find("is not accessed") or
     last.message:find("Cannot access member") or
     last.message:find("Argument of type") then
    return nil
  end
0
0
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
0
0