Skip to content

Commit

Permalink
fix: check buffer validity try_add()/try_add_wrapper()
Browse files Browse the repository at this point in the history
Currently avoids an issue where a `FileType` autocommand schedules a
call to one of these functions but the buffer gets wiped out before the
next event loop. `nvim_get_option_value()` with the `filetype` argument
can cause this as it creates a transient buffer that only lasts for a
single function call.
  • Loading branch information
Diomendius committed Oct 5, 2024
1 parent 52346b1 commit a673c83
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lua/lspconfig/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ end
function M:try_add(bufnr, project_root)
bufnr = bufnr or api.nvim_get_current_buf()

if not api.nvim_buf_is_valid(bufnr) then
return
end

if vim.bo[bufnr].buftype == 'nofile' then
return
end
Expand Down Expand Up @@ -294,6 +298,10 @@ end
--- @param bufnr integer
--- @param project_root? string
function M:try_add_wrapper(bufnr, project_root)
if not api.nvim_buf_is_valid(bufnr) then
return
end

local config = self.config
-- `config.filetypes = nil` means all filetypes are valid.
if not config.filetypes or vim.tbl_contains(config.filetypes, vim.bo[bufnr].filetype) then
Expand Down

0 comments on commit a673c83

Please sign in to comment.