Skip to content

Commit fb453a1

Browse files
authored
fix: autostart=false: attach when editing new, nonexistent file #2712
## Problem Currently, `nvim-lspconfig` tries to attach servers automatically via two autocommands: 1. Created if `config.autostart == true` and triggered by `FileType`, if `config.filetypes` is set, else `BufReadPost`. Calls `try_add()`. 2. Created for each workspace root, triggered by `BufReadPost` matching paths starting with the root. Calls `try_add_wrapper()`. `BufReadPost` does not fire when creating a buffer for a file that doesn't exist. This means that if `config.autostart == true` and `config.filetypes` is set and includes the detected filetype for the buffer, the server is attached automatically regardless of whether the file exists, but in all other cases the server is only attached for existing files. ## Solution 1. Where these autocommands trigger on `BufReadPost`, also trigger on `BufNewFile`. 2. Wrap the autocommand callbacks in `vim.schedule()` to ensure `filetype` is set first, as the `BufReadPost`/`BufNewFile` autocommands will trigger before `FileType` if `nvim-lspconfig` is set up early enough during Nvim init (see neovim/neovim#7367 and #2712 (comment)). I did consider including a test with this PR, but there doesn't seem to be any existing test infrastructure for tests involving actually running a language server (or a mock of one). Fixes #2711
1 parent 5408121 commit fb453a1

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lua/lspconfig/configs.lua

+13-4
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,15 @@ function configs.__newindex(t, config_name, config_def)
101101

102102
if config.autostart == true then
103103
local event_conf = config.filetypes and { event = 'FileType', pattern = config.filetypes }
104-
or { event = 'BufReadPost' }
104+
or { event = { 'BufReadPost', 'BufNewFile' } }
105105
api.nvim_create_autocmd(event_conf.event, {
106106
pattern = event_conf.pattern or '*',
107107
callback = function(opt)
108-
M.manager:try_add(opt.buf)
108+
-- Use vim.schedule() to ensure filetype detection happens first.
109+
-- Sometimes, BufNewFile triggers before 'filetype' is set.
110+
vim.schedule(function()
111+
M.manager:try_add(opt.buf)
112+
end)
109113
end,
110114
group = lsp_group,
111115
desc = string.format(
@@ -142,13 +146,18 @@ function configs.__newindex(t, config_name, config_def)
142146
end
143147

144148
if root_dir then
145-
api.nvim_create_autocmd('BufReadPost', {
149+
api.nvim_create_autocmd({ 'BufReadPost', 'BufNewFile' }, {
146150
pattern = fn.fnameescape(root_dir) .. '/*',
147151
callback = function(arg)
148152
if #M.manager:clients() == 0 then
149153
return true
150154
end
151-
M.manager:try_add_wrapper(arg.buf, root_dir)
155+
156+
-- Use vim.schedule() to ensure filetype detection happens first.
157+
-- Sometimes, BufNewFile triggers before 'filetype' is set.
158+
vim.schedule(function()
159+
M.manager:try_add_wrapper(arg.buf, root_dir)
160+
end)
152161
end,
153162
group = lsp_group,
154163
desc = string.format(

0 commit comments

Comments
 (0)