From fec78d9965fdcdeb7e1a04e8195f9776dcb105f0 Mon Sep 17 00:00:00 2001 From: Diomendius <42310725+Diomendius@users.noreply.github.com> Date: Sun, 9 Jul 2023 20:52:56 +1200 Subject: [PATCH 1/3] fix: consider attaching when editing new file BufReadPost does not trigger when editing a nonexistent file, so language servers would not automatically attach when editing a new file if autostart was disabled or if the server had no associated filetypes. This fixes that by adding BufNewFile to autocommands hooked to BufReadPost. --- lua/lspconfig/configs.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/lspconfig/configs.lua b/lua/lspconfig/configs.lua index 5a5fc2e5b4..6a8b102c5c 100644 --- a/lua/lspconfig/configs.lua +++ b/lua/lspconfig/configs.lua @@ -98,7 +98,7 @@ function configs.__newindex(t, config_name, config_def) if config.autostart == true then local event_conf = config.filetypes and { event = 'FileType', pattern = config.filetypes } - or { event = 'BufReadPost' } + or { event = { 'BufReadPost', 'BufNewFile' } } api.nvim_create_autocmd(event_conf.event, { pattern = event_conf.pattern or '*', callback = function(opt) @@ -139,7 +139,7 @@ function configs.__newindex(t, config_name, config_def) end if root_dir then - api.nvim_create_autocmd('BufReadPost', { + api.nvim_create_autocmd({ 'BufReadPost', 'BufNewFile' }, { pattern = fn.fnameescape(root_dir) .. '/*', callback = function(arg) if #M.manager:clients() == 0 then From 52346b1d7b0ffa52c5bde62604b0858b94db8350 Mon Sep 17 00:00:00 2001 From: Diomendius <42310725+Diomendius@users.noreply.github.com> Date: Tue, 29 Aug 2023 15:53:16 +1200 Subject: [PATCH 2/3] fix: ensure try_add() runs after filetype detection Sometimes, BufNewFile triggers before 'filetype' is set. Using vim.schedule() should ensure filetype detection runs before the callback. --- lua/lspconfig/configs.lua | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lua/lspconfig/configs.lua b/lua/lspconfig/configs.lua index 6a8b102c5c..4859c058b1 100644 --- a/lua/lspconfig/configs.lua +++ b/lua/lspconfig/configs.lua @@ -102,7 +102,11 @@ function configs.__newindex(t, config_name, config_def) api.nvim_create_autocmd(event_conf.event, { pattern = event_conf.pattern or '*', callback = function(opt) - M.manager:try_add(opt.buf) + -- Use vim.schedule() to ensure filetype detection happens first. + -- Sometimes, BufNewFile triggers before 'filetype' is set. + vim.schedule(function() + M.manager:try_add(opt.buf) + end) end, group = lsp_group, desc = string.format( @@ -145,7 +149,12 @@ function configs.__newindex(t, config_name, config_def) if #M.manager:clients() == 0 then return true end - M.manager:try_add_wrapper(arg.buf, root_dir) + + -- Use vim.schedule() to ensure filetype detection happens first. + -- Sometimes, BufNewFile triggers before 'filetype' is set. + vim.schedule(function() + M.manager:try_add_wrapper(arg.buf, root_dir) + end) end, group = lsp_group, desc = string.format( From a673c83198c87eb0a4b9bb3b0335a8fb4c835a93 Mon Sep 17 00:00:00 2001 From: Diomendius <42310725+Diomendius@users.noreply.github.com> Date: Sat, 5 Oct 2024 22:38:52 +1300 Subject: [PATCH 3/3] fix: check buffer validity try_add()/try_add_wrapper() 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. --- lua/lspconfig/manager.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lua/lspconfig/manager.lua b/lua/lspconfig/manager.lua index 3457014854..51b145c636 100644 --- a/lua/lspconfig/manager.lua +++ b/lua/lspconfig/manager.lua @@ -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 @@ -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