Skip to content

feat: add support for new LSP config API in Neovim 0.11+ #1475

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
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
33 changes: 13 additions & 20 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -655,12 +655,6 @@ require('lazy').setup({
},
}

-- LSP servers and clients are able to communicate to each other what features they support.
-- By default, Neovim doesn't support everything that is in the LSP specification.
-- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities.
-- So, we create new capabilities with blink.cmp, and then broadcast that to the servers.
local capabilities = require('blink.cmp').get_lsp_capabilities()

-- Enable the following language servers
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
--
Expand Down Expand Up @@ -699,6 +693,11 @@ require('lazy').setup({
},
},
}
---@type MasonLspconfigSettings
---@diagnostic disable-next-line: missing-fields
require('mason-lspconfig').setup {
automatic_enable = vim.tbl_keys(servers or {}),
Copy link

@SetsuikiHyoryu SetsuikiHyoryu May 17, 2025

Choose a reason for hiding this comment

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

@umutondersu @oriori1703

I looked into the source code of mason-lspconfig. When the keys of servers are passed as a table to automatic_enable, it causes mason-lspconfig to ignore any LSPs that are installed via Mason but not explicitly listed in servers.

In the mason-lspconfig.nvim\doc\mason-lspconfig.txtmason-lspconfig.nvim\doc\mason-lspconfig.txt document, it mentions that doing this will only start certain servers:

-- To only enable certain servers to be automatically enabled:
-- ```lua
--   automatic_enable = {
--     "lua_ls",
--     "vimls"
--   }
-- ```

In the setup that in the mason-lspconfig.nvim\lua\mason-lspconfig\init.lua,if automatic_enable is not set to false, both enable_all and init will be executed:

function M.setup(config)
    if config then
        settings.set(config)
    end

    --- ...

    local registry = require "mason-registry"
    registry.refresh(vim.schedule_wrap(function(success, updated_registries)
        if success and #updated_registries > 0 and settings.current.automatic_enable ~= false then
            require("mason-lspconfig.features.automatic_enable").enable_all()
        end
    end))

    if settings.current.automatic_enable ~= false then
        require("mason-lspconfig.features.automatic_enable").init()
    end

    -- ...
end

In mason-lspconfig\features\automatic_enable.lua, The logic for enabling servers in init and enable_all is the same:

return {
    init = function()
        enabled_servers = {}
        _.each(enable_server, registry.get_installed_package_names())
        -- We deregister the event handler primarily for testing purposes where .setup() is called multiple times in the
        -- same instance.
        registry:off("package:install:success", enable_server_scheduled)
        registry:on("package:install:success", enable_server_scheduled)
    end,
    enable_all = function()
        _.each(enable_server, registry.get_installed_package_names())
    end,
}

However, in enable_server, if automatic_enable is a table and an LSP installed by Mason is not listed in automatic_enable, it will not execute vim.lsp.config and vim.lsp.enable.

local function enable_server(mason_pkg)
    -- ...

    local lspconfig_name = mappings.get_mason_map().package_to_lspconfig[mason_pkg]

    -- ...

    local automatic_enable = settings.current.automatic_enable

    if type(automatic_enable) == "table" then
        local exclude = automatic_enable.exclude
        if exclude then
            if _.any(_.equals(lspconfig_name), exclude) then
                -- This server is explicitly excluded.
                return
            end
        else
            if not _.any(_.equals(lspconfig_name), automatic_enable) then
                -- This server is not explicitly enabled.
                return
            end
        end
    elseif automatic_enable == false then
        return
    end

    -- ...

    vim.lsp.enable(lspconfig_name)

    -- ...
end

In other words, even if I’ve already installed the LSPs via Mason, I still have to list all the LSPs I use in servers or in or {}. Isn't that kind of ... not very "lazy"?

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems you are right about automatic_enable.
If set to true it will enable all the LSPs, but when given a table it will only enable those LSPs.
After looking into it more, enabling all the LSPs also seems to be the old behaviour mason.

Despite that , I still prefer declarativly specifying all the LSPs in the config file, instead of downloading them using the mason interface.
This way I can commit those LSPs and have a reproducible setup.
This is the same reason I commit the lazy-lock.json file.

Though I guess this is a matter of taste, and could break setups for people that were used to the old way, so I'm ok with both options :)

@umutondersu @SetsuikiHyoryu what do you think?

Copy link

@SetsuikiHyoryu SetsuikiHyoryu May 23, 2025

Choose a reason for hiding this comment

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

@oriori1703 @umutondersu

Personally, I prefer the more “lazy” approach, but I tend to follow the method recommended by the Neovim official because I feel it’s more native. That means the operational habits won’t differ too much even when I have to work in a barebones environment. However, I’m not entirely sure what the official philosophy is on this. Judging from the existence of the vim.lsp.enable interface, it seems that the official intent is for LSP to be explicitly enabled.

Perhaps, if Mason provides an interface to retrieve the list of installed packages, a compromise could be to merge that list into the loop that calls vim.lsp.enable (the table passed to automatic_enable).

Sorry, it seems that
_.each(enable_server, registry.get_installed_package_names())
is already configuring only the installed packages. Setting automatic_enable = true doesn't appear to enable LSPs that haven't been installed.

}

-- Ensure the servers and tools above are installed
--
Expand All @@ -719,20 +718,14 @@ require('lazy').setup({
})
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,
},
}
-- Installed LSPs are configured and enabled automatically with mason-lspconfig
-- The loop below is for overriding the default configuration of LSPs with the ones in the servers table
for server_name, config in pairs(servers) do
vim.lsp.config(server_name, config)
end

-- NOTE: Some servers may require an old setup until they are updated. For the full list refer here: https://github.com/neovim/nvim-lspconfig/issues/3705
-- These servers will have to be manually set up with require("lspconfig").server_name.setup{}
end,
},

Expand Down