diff --git a/nvim/.config/nvim/after/ftplugin/rust.lua b/nvim/.config/nvim/after/ftplugin/rust.lua index b8be7c9..60e6846 100644 --- a/nvim/.config/nvim/after/ftplugin/rust.lua +++ b/nvim/.config/nvim/after/ftplugin/rust.lua @@ -11,6 +11,7 @@ vim.keymap.set('n', 'cbc', ":Dispatch! cargo clean" , { noremap=true, bu vim.keymap.set('n', 'cbd', ":Dispatch! cargo doc" , { noremap=true, buffer=0 }) vim.keymap.set('n', 'cdD', ":Dispatch! cargo doc --open", { noremap=true, buffer=0 }) vim.keymap.set('n', 'cbu', ":Dispatch! cargo update" , { noremap=true, buffer=0 }) +vim.keymap.set('n', ',r' , ":CargoReload" , { noremap=true, buffer=0 }) vim.api.nvim_create_autocmd({ "BufReadPre", "BufWinEnter" }, { pattern = { "*/git/checkouts/*", "*/toolchains/*", "*cargo/registry/*" }, diff --git a/nvim/.config/nvim/lua/lsp.lua b/nvim/.config/nvim/lua/lsp.lua index 94caf58..c562b56 100644 --- a/nvim/.config/nvim/lua/lsp.lua +++ b/nvim/.config/nvim/lua/lsp.lua @@ -80,12 +80,18 @@ local lsp_key_mappings = { { "codeLensProvider", 'n', 'L', 'lua vim.lsp.codelens.clear()' }, } -local tsserver_setup = 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 +local cargo_reload_workspace = function() + vim.lsp.buf_request(0, 'rust-analyzer/reloadWorkspace', nil, function(err) + if err then + vim.schedule(function() + 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 local get_active_client_by_name = function(bufnr, servername) @@ -124,16 +130,30 @@ local switch_source_header = function() end end -local clangd_setup = function(client) +local client_custom_setup = function(client) if client.name == 'clangd' then vim.api.nvim_create_user_command("ClangdSwitchSourceHeader", switch_source_header, {desc = 'Switch between source/header'}) end + + if client.name == 'rust-analyzer' then + vim.api.nvim_create_user_command("CargoReload", cargo_reload_workspace, {desc = 'Reload current cargo workspace'}) + end + + 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 clangd_cleanup = function(client) +local client_custom_cleanup = function(client) if client.name == 'clangd' then vim.api.nvim_del_user_command("ClangdSwitchSourceHeader") end + + if client.name == 'rust-analyzer' then + vim.api.nvim_del_user_command("CargoReload") + end end local on_attach = function(client, bufnr) @@ -146,11 +166,7 @@ local on_attach = function(client, bufnr) local opts = { noremap=true, silent=true } - -- This needs to be here, so we disable formatting with tsserver before - -- actually checking it below. - tsserver_setup(client) - - clangd_setup(client) + client_custom_setup(client) for _, mappings in pairs(lsp_key_mappings) do local capability, mode, lhs, rhs = unpack(mappings) @@ -219,6 +235,6 @@ vim.api.nvim_create_autocmd("LspDetach", { end end - clangd_cleanup(client) + client_custom_cleanup(client) end, })