nvim: lsp: Fix error in LspDetach

Some language servers like HLS take longer to attach and if we exit
soon before LSP attach had a chance to finish (may be?), server
capabilities field does not seem to be valid in client object and
we get the below error. Check if it valid first.

Error detected while processing LspDetach Autocommands for "*":
Error executing lua callback: /home/core/.config/nvim/lua/lsp.lua:215: attempt to index field 'server_capabilities' (a nil value)
stack traceback:
        /home/core/.config/nvim/lua/lsp.lua:215: in function </home/core/.config/nvim/lua/lsp.lua:209>
        [C]: in function 'nvim_exec_autocmds'
        /usr/local/share/nvim/runtime/lua/vim/lsp.lua:1140: in function </usr/local/share/nvim/runtime/lua/vim/lsp.lua:1139>
Error executing lua callback: /home/core/.config/nvim/lua/lsp.lua:215: attempt to index field 'server_capabilities' (a nil value)
stack traceback:
        /home/core/.config/nvim/lua/lsp.lua:215: in function </home/core/.config/nvim/lua/lsp.lua:209>
        [C]: in function 'nvim_exec_autocmds'
        /usr/local/share/nvim/runtime/lua/vim/lsp.lua:1140: in function </usr/local/share/nvim/runtime/lua/vim/lsp.lua:1139>
This commit is contained in:
Sanchayan Maity 2022-11-28 12:00:05 +05:30
parent b752f2241f
commit 4d970ce70c

View file

@ -212,7 +212,7 @@ vim.api.nvim_create_autocmd("LspDetach", {
for _, mappings in pairs(lsp_key_mappings) do
local capability, mode, lhs, _ = unpack(mappings)
if client.server_capabilities[capability] then
if client.server_capabilities and client.server_capabilities[capability] then
vim.api.nvim_buf_del_keymap(bufnr, mode, lhs)
end
end