diff --git a/lua/lspconfig/configs.lua b/lua/lspconfig/configs.lua index d101091d21..a7f9edcad5 100644 --- a/lua/lspconfig/configs.lua +++ b/lua/lspconfig/configs.lua @@ -101,7 +101,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( @@ -139,7 +143,11 @@ function configs.__newindex(t, config_name, config_def) api.nvim_create_autocmd({ 'BufReadPost', 'BufNewFile' }, { pattern = fn.fnameescape(root_dir) .. '/*', callback = function(arg) - 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(