Skip to content

Commit

Permalink
fix(lsp): reload cargo workspace broken when opening another buffer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Mar 3, 2024
1 parent 67c0970 commit e9ae15b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- LSP: Bug preventing reload workspace on save Cargo.toml
when opening another Rust buffer [[#270](https://github.com/mrcjkb/rustaceanvim/issues/270)].
- LSP: Don't try to delete `RustLsp` command on client exit
if it doesn't exist, and fail silently.

Expand Down
18 changes: 18 additions & 0 deletions ftplugin/toml.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local fname = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ':t')
if fname ~= 'Cargo.toml' then
return
end

local config = require('rustaceanvim.config.internal')
local ra = require('rustaceanvim.rust_analyzer')
if config.tools.reload_workspace_from_cargo_toml then
vim.api.nvim_create_autocmd('BufWritePost', {
buffer = vim.api.nvim_get_current_buf(),
callback = function()
if #ra.get_active_rustaceanvim_clients(nil) > 0 then
vim.cmd.RustLsp { 'reloadWorkspace', mods = { silent = true } }
end
end,
group = vim.api.nvim_create_augroup('RustaceanCargoReloadWorkspace', { clear = false }),

This comment has been minimized.

Copy link
@tomtomjhj

tomtomjhj Mar 3, 2024

Contributor

Duplicate autocmds will be created on each FileType (which is triggered by BufRead).

I suggest nvim_clear_autocmds-ing this augroup with the current buffer before nvim_create_autocmd.

This comment has been minimized.

Copy link
@mrcjkb

mrcjkb Mar 3, 2024

Author Owner

Good call, thanks.
Addressed with 1f2e522

})
end
12 changes: 0 additions & 12 deletions lua/rustaceanvim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,11 @@ M.start = function(bufnr)

lsp_start_config.handlers = vim.tbl_deep_extend('force', custom_handlers, lsp_start_config.handlers or {})

local augroup = vim.api.nvim_create_augroup('RustaceanAutoCmds', { clear = true })

local commands = require('rustaceanvim.commands')
local old_on_init = lsp_start_config.on_init
lsp_start_config.on_init = function(...)
override_apply_text_edits()
commands.create_rust_lsp_command()
if config.tools.reload_workspace_from_cargo_toml then
vim.api.nvim_create_autocmd('BufWritePost', {
pattern = '*/Cargo.toml',
callback = function()
vim.cmd.RustLsp { 'reloadWorkspace', mods = { silent = true } }
end,
group = augroup,
})
end
if type(old_on_init) == 'function' then
old_on_init(...)
end
Expand All @@ -181,7 +170,6 @@ M.start = function(bufnr)
-- on_exit runs in_fast_event
vim.schedule(function()
commands.delete_rust_lsp_command()
vim.api.nvim_del_augroup_by_id(augroup)
end)
if type(old_on_exit) == 'function' then
old_on_exit(...)
Expand Down

0 comments on commit e9ae15b

Please sign in to comment.