nvim: lsp: Add support for reloading Cargo workspace

While at it, refactor the custom setup and clean up for clients in
a single function.
This commit is contained in:
Sanchayan Maity 2023-05-24 13:50:27 +05:30
parent 269382b363
commit 5674bbdc06
Signed by: sanchayanmaity
GPG key ID: 6F6A0609C12038F3
2 changed files with 31 additions and 14 deletions

View file

@ -11,6 +11,7 @@ vim.keymap.set('n', 'cbc', ":Dispatch! cargo clean<CR>" , { noremap=true, bu
vim.keymap.set('n', 'cbd', ":Dispatch! cargo doc<CR>" , { noremap=true, buffer=0 }) vim.keymap.set('n', 'cbd', ":Dispatch! cargo doc<CR>" , { noremap=true, buffer=0 })
vim.keymap.set('n', 'cdD', ":Dispatch! cargo doc --open<CR>", { noremap=true, buffer=0 }) vim.keymap.set('n', 'cdD', ":Dispatch! cargo doc --open<CR>", { noremap=true, buffer=0 })
vim.keymap.set('n', 'cbu', ":Dispatch! cargo update<CR>" , { noremap=true, buffer=0 }) vim.keymap.set('n', 'cbu', ":Dispatch! cargo update<CR>" , { noremap=true, buffer=0 })
vim.keymap.set('n', ',r' , ":CargoReload<CR>" , { noremap=true, buffer=0 })
vim.api.nvim_create_autocmd({ "BufReadPre", "BufWinEnter" }, { vim.api.nvim_create_autocmd({ "BufReadPre", "BufWinEnter" }, {
pattern = { "*/git/checkouts/*", "*/toolchains/*", "*cargo/registry/*" }, pattern = { "*/git/checkouts/*", "*/toolchains/*", "*cargo/registry/*" },

View file

@ -80,12 +80,18 @@ local lsp_key_mappings = {
{ "codeLensProvider", 'n', '<LocalLeader>L', '<cmd>lua vim.lsp.codelens.clear()<CR>' }, { "codeLensProvider", 'n', '<LocalLeader>L', '<cmd>lua vim.lsp.codelens.clear()<CR>' },
} }
local tsserver_setup = function(client) local cargo_reload_workspace = function()
if client.name == 'tsserver' then vim.lsp.buf_request(0, 'rust-analyzer/reloadWorkspace', nil, function(err)
-- Disable tsserver formatting, we want formatting via prettier if err then
client.server_capabilities.documentFormattingProvider = false vim.schedule(function()
client.server_capabilities.documentRangeFormattingProvider = false vim.notify('Cargo reload workspace gave error: ' .. tostring(err), vim.log.levels.ERROR)
end)
else
vim.schedule(function()
vim.notify 'Cargo workspace reloaded'
end)
end end
end)
end end
local get_active_client_by_name = function(bufnr, servername) local get_active_client_by_name = function(bufnr, servername)
@ -124,16 +130,30 @@ local switch_source_header = function()
end end
end end
local clangd_setup = function(client) local client_custom_setup = function(client)
if client.name == 'clangd' then if client.name == 'clangd' then
vim.api.nvim_create_user_command("ClangdSwitchSourceHeader", switch_source_header, {desc = 'Switch between source/header'}) vim.api.nvim_create_user_command("ClangdSwitchSourceHeader", switch_source_header, {desc = 'Switch between source/header'})
end end
if client.name == 'rust-analyzer' then
vim.api.nvim_create_user_command("CargoReload", cargo_reload_workspace, {desc = 'Reload current cargo workspace'})
end end
local clangd_cleanup = function(client) if client.name == 'tsserver' then
-- Disable tsserver formatting, we want formatting via prettier
client.server_capabilities.documentFormattingProvider = false
client.server_capabilities.documentRangeFormattingProvider = false
end
end
local client_custom_cleanup = function(client)
if client.name == 'clangd' then if client.name == 'clangd' then
vim.api.nvim_del_user_command("ClangdSwitchSourceHeader") vim.api.nvim_del_user_command("ClangdSwitchSourceHeader")
end end
if client.name == 'rust-analyzer' then
vim.api.nvim_del_user_command("CargoReload")
end
end end
local on_attach = function(client, bufnr) local on_attach = function(client, bufnr)
@ -146,11 +166,7 @@ local on_attach = function(client, bufnr)
local opts = { noremap=true, silent=true } local opts = { noremap=true, silent=true }
-- This needs to be here, so we disable formatting with tsserver before client_custom_setup(client)
-- actually checking it below.
tsserver_setup(client)
clangd_setup(client)
for _, mappings in pairs(lsp_key_mappings) do for _, mappings in pairs(lsp_key_mappings) do
local capability, mode, lhs, rhs = unpack(mappings) local capability, mode, lhs, rhs = unpack(mappings)
@ -219,6 +235,6 @@ vim.api.nvim_create_autocmd("LspDetach", {
end end
end end
clangd_cleanup(client) client_custom_cleanup(client)
end, end,
}) })