やること
Neovimを使っていると、LSPが下記のようなDiagnostics("randint" is ...
)を吐く。
この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 '👀'
ノート
- 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