williamboman / mason-lspconfig.nvim

Extension to mason.nvim that makes it easier to use lspconfig with mason.nvim.
Apache License 2.0
2.73k stars 167 forks source link

Option to have `mason-lspconfig` ask to install language server if none is detected for filetype #100

Open poperigby opened 1 year ago

poperigby commented 1 year ago

Describe the solution you'd like

It would be useful if I could get mason-lspconfig to ask me if I want to install a language server for the current filetype, if there isn't one already installed.

Additional context

I just copied this over from the discussion in mason.nvim https://github.com/williamboman/mason.nvim/discussions/661

heygarrett commented 1 year ago

Here's a super basic implementation I put together:

vim.api.nvim_create_autocmd("FileType", {
    group = vim.api.nvim_create_augroup("mason-lspconfig", { clear = true }),
    callback = function(t)
        if vim.bo[t.buf].buftype ~= "" then return end
        local mason_lspconfig = require("mason-lspconfig")
        local available_servers =
            mason_lspconfig.get_available_servers({ filetype = t.match })
        local installed_servers = mason_lspconfig.get_installed_servers()
        for _, available in ipairs(available_servers) do
            for _, installed in ipairs(installed_servers) do
                if available == installed then return end
            end
        end
        vim.schedule(vim.cmd.LspInstall)
    end,
})
heygarrett commented 1 year ago

@williamboman Would there be a way to disable the vim.notify messages in the context of this autocommand? ☝️

Currently I'm just adding to a list of file types to ignore:

local ignored_filetypes = setmetatable({
    "diff",
    "fish",
    "git%w+",
    "oil",
}, {
    __index = function(tbl, key)
        for _, ft in ipairs(tbl) do
            if key:match("^" .. ft .. "$") then return true end
        end
        return false
    end,
})

But I'm only doing that to silence the No LSP servers found message.

Edit: Never mind, I'm not sure why I didn't think to check the number of available servers and return early if there are none. 😅