nvim-lspconfig
nvim-lspconfig copied to clipboard
Consider attaching server when editing new file, not just existing files, when autostart = false
This fixes #2711 by hooking try_add()
and try_add_wrapper()
to BufNewFile
in addition to the existing hook on BufReadPost
.
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), and the upfront cost of adding this would be much greater than that of this two-line commit.
I can't think of a better solution than vim.schedule()
, but it seems to work fine in testing and in normal use.
It'd be nice to have an explicit guarantee that the callback will run only after filetype detection. The other idea I had was to create a one-shot FileType
autocommand when triggering on BufNewFile
, but this just seems generally worse and would break if FileType
fires before BufNewFile
, which does happen sometimes, seemingly depending on user configuration.
I looked at the Nvim source to try to confirm that vim.schedule()
callbacks couldn't fire between BufNewFile
and FileType
, but I couldn't make much sense of it. It should be enough to confirm that every code path that can trigger BufNewFile
also triggers (or has already triggered) filetype detection before the event loop runs. Intuitively, I would expect this to be the case, and it appears to be in practice.