Skip to content
Open
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
24 changes: 9 additions & 15 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,15 @@ require('lazy').setup({
},
}

-- The following loop will configure each server with the capabilities we defined above.
-- This will ensure that all servers have the same base configuration, but also
-- allow for server-specific overrides.
for server_name, server_config in pairs(servers) do
server_config.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server_config.capabilities or {})
vim.lsp.config(server_name, server_config)
vim.lsp.enable(server_name)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I added this line to my config, it brought up every LSP I had installed without regard to if it's needed. Another approach is necessary. Using ftplugin or something else, maybe autocommands, something needs to trigger vim.lsp.enable for the appropriate LSP's only, at the appropriate time.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That simply isn't true, at least not with regards to "this line".

vim.lsp.enable() only sets an autocmd, which activates if you open a buffer with a 'filetype' that matches an enabled LSP config.

Copy link

@logrusx logrusx Oct 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, maybe I've made a mistake somewhere in my config which is almost identical in this part. Sorry for the comment and thanks for prompting me.

EDIT: actually I have bashls with config = {} , I have luals with filetypes={'lua'} and it enables both of them without me even opening a buffer, so what you say cannot be true because I don't enable them anywhere and when I comment this line, nothing is brought up.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of these claims is covered by tests, the other is a vague description that I can only guess at.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you even checked LspInfo when you fire up Neovim without opening any file?

Copy link

@logrusx logrusx Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vim.lsp.enable() only sets an autocmd, which activates if you open a buffer with a 'filetype' that matches an enabled LSP config.

After some more research I can say the above statement is misinformed:

vim.lsp.enable()
Auto-starts LSP when a buffer is opened, based on the lsp-config filetypes, root_markers, and root_dir fields.

Nowhere here it is said anything about auto commands. You need to set the auto commands yourself and they need to enable the LSP (call vim.lsp.enable) in case a buffer with a configured file type is opened. We don't want LSP's loaded for no reason. They need to be only loaded when they are needed.

Auto commands can be set right there instead of that line. However since nvim-lspconfig should go as well, this should not be done in its config function.

Copy link
Contributor

@rivenirvana rivenirvana Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- @param name string|string[] Name(s) of client(s) to enable.
--- @param enable? boolean `true|nil` to enable, `false` to disable (actively stops and detaches
--- clients as needed)
function lsp.enable(name, enable)
  validate('name', name, { 'string', 'table' })

  local names = vim._ensure_list(name) --[[@as string[] ]]
  for _, nm in ipairs(names) do
    if nm == '*' then
      error('Invalid name')
    end
    lsp._enabled_configs[nm] = enable ~= false and {} or nil
  end

  if not next(lsp._enabled_configs) then
    -- If there are no remaining LSPs enabled, remove the enable autocmd.
    if lsp_enable_autocmd_id then
      api.nvim_del_autocmd(lsp_enable_autocmd_id)
      lsp_enable_autocmd_id = nil
    end
  else
    -- Only ever create autocmd once to reuse computation of config merging.
    lsp_enable_autocmd_id = lsp_enable_autocmd_id
      or api.nvim_create_autocmd('FileType', {
        group = api.nvim_create_augroup('nvim.lsp.enable', {}),
        callback = function(args)
          lsp_enable_callback(args.buf)
        end,
      })
  end

  -- Ensure any pre-existing buffers start/stop their LSP clients.
  if enable ~= false then
    if vim.v.vim_did_enter == 1 then
      vim.cmd.doautoall('nvim.lsp.enable FileType')
    end
  else
    for _, nm in ipairs(names) do
      for _, client in ipairs(lsp.get_clients({ name = nm })) do
        client:stop()
      end
    end
  end
end

You can read the source code yourself.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You both seem to be unable to comprehend what I'm talking about. One doesn't need the LSP's to be enabled when they are not needed and vim.lsp.enable here does exactly that.

Did any of you run :LspInfo or :healthcheck vim.lsp on a blank buffer in a freshly started Neovim? You should not see anything there and yet, there's a list of enabled configurations.

However many times you may read the code, that's the result.

Copy link
Contributor

@rivenirvana rivenirvana Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing code suffices. The editor auto enables LSPs for the filetypes a user is concerned with. Enabled only means that the autocmd is set up, and the lsp process will spawn only when the filetype is detected, no other overhead. If you do not want or need an LSP, then remove its configuration. It should be fine for the vast majority of use cases, except "maybe" for those that have configs for LSPs counting far into the double digits, at which point it becomes a personal problem.

If you still want to overengineer this though, you can hijack lsp.enable or just make your own setup function to enable LSPs whenever you want, because it is pointless to wrap lsp.enable in an autocmd on the FileType event, I think you cannot have the autocmds that it sets up fire at that same event (I guess not really, but still pointless). But at that point, you're just going to end up rewriting what lsp.enable already does. Please read it properly.

Copy link

@logrusx logrusx Oct 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read it properly.

This one I didn't need to read twice. Thank you for the explanation. Actually I had already started doubting my understanding and your explanation just confirmed my doubts. Thank you!

I think you cannot have the autocmds that it sets up fire at that same event.

If I understand that properly, it means the FileType autocmd and the autocmd created from the wrapped vim.lsp.enable call cannot fire at the same event. Well, they do. I had taken exactly the path to wrap vim.lsp.enable in an autocmd of the FileType event and it worked that way. By worked, I mean it enabled and started the LSP's on opening a file from that type.

end

-- Ensure the servers and tools above are installed
--
-- To check the current status of installed tools and/or manually install
Expand All @@ -718,21 +727,6 @@ require('lazy').setup({
'stylua', -- Used to format Lua code
})
require('mason-tool-installer').setup { ensure_installed = ensure_installed }

require('mason-lspconfig').setup {
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
automatic_installation = false,
handlers = {
function(server_name)
local server = servers[server_name] or {}
-- This handles overriding only values explicitly passed
-- by the server configuration above. Useful when disabling
-- certain features of an LSP (for example, turning off formatting for ts_ls)
server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
require('lspconfig')[server_name].setup(server)
end,
},
}
end,
},

Expand Down