Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: autostart=false: attach when editing new, nonexistent file (#2712 attempt 2) #3355

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lua/lspconfig/configs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ 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)
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(
Expand Down Expand Up @@ -139,13 +143,18 @@ 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
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(
Expand Down
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
Loading