Skip to content

Commit 068e074

Browse files
committed
feat(lsp): unify language server setup
Signed-off-by: Jint-lzxy <[email protected]>
1 parent 6a5f462 commit 068e074

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

lua/modules/configs/completion/lsp.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,20 @@ return function()
66
local opts = {
77
capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities()),
88
}
9-
-- Configure LSPs that are not supported by `mason.nvim` but are available in `nvim-lspconfig` here.
10-
-- Call |vim.lsp.config()| FOLLOWED BY |vim.lsp.enable()| to ensure the server starts automatically.
9+
-- Configure LSPs that are not supported by `mason.nvim` but are available in `nvim-lspconfig`.
10+
-- First call |vim.lsp.config()|, then |vim.lsp.enable()| (or use `register_server`, see below)
11+
-- to ensure the language server is properly configured and starts automatically.
1112
if vim.fn.executable("dart") == 1 then
1213
local ok, _opts = pcall(require, "user.configs.lsp-servers.dartls")
1314
if not ok then
1415
_opts = require("completion.servers.dartls")
1516
end
1617
local final_opts = vim.tbl_deep_extend("keep", _opts, opts)
17-
vim.lsp.config("dartls", final_opts)
18-
vim.lsp.enable("dartls")
18+
require("modules.utils").register_server("dartls", final_opts)
1919
end
2020

2121
pcall(require, "user.configs.lsp")
2222

2323
-- Start LSPs
24-
pcall(function()
25-
local matching_configs = require("lspconfig").util.get_config_by_ft(vim.bo.filetype)
26-
for _, config in ipairs(matching_configs) do
27-
config.launch()
28-
end
29-
end)
24+
pcall(vim.cmd.LspStart)
3025
end

lua/modules/configs/completion/mason-lspconfig.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ M.setup = function()
1010
require("lspconfig.ui.windows").default_options.border = "rounded"
1111
require("modules.utils").load_plugin("mason-lspconfig", {
1212
ensure_installed = lsp_deps,
13-
automatic_enable = true,
13+
-- Skip auto enable because we are loading language servers lazily
14+
automatic_enable = false,
1415
})
1516

1617
vim.diagnostic.config({
@@ -56,15 +57,16 @@ please REMOVE your LSP configuration (rust_analyzer.lua) from the `servers` dire
5657

5758
if not ok then
5859
-- Default to use factory config for server(s) that doesn't include a spec
59-
vim.lsp.config(lsp_name, opts)
60+
require("modules.utils").register_server(lsp_name, opts)
6061
elseif type(custom_handler) == "function" then
6162
-- Case where language server requires its own setup
6263
-- Be sure to call `vim.lsp.config()` within the setup function.
6364
-- Refer to |vim.lsp.config()| for documentation.
6465
-- For an example, see `clangd.lua`.
6566
custom_handler(opts)
67+
vim.lsp.enable(lsp_name)
6668
elseif type(custom_handler) == "table" then
67-
vim.lsp.config(
69+
require("modules.utils").register_server(
6870
lsp_name,
6971
vim.tbl_deep_extend(
7072
"force",
@@ -84,7 +86,6 @@ please REMOVE your LSP configuration (rust_analyzer.lua) from the `servers` dire
8486
{ title = "nvim-lspconfig" }
8587
)
8688
end
87-
return
8889
end
8990

9091
---A simplified mimic of <mason-lspconfig 1.x>'s `setup_handlers` callback.

lua/modules/utils/init.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ function M.gen_cursorword_hl()
263263
set_global_hl("MiniCursorwordCurrent", nil)
264264
end
265265

266+
---Setup and enable a language server in one call.
267+
---@param server string @Name of the language server
268+
---@param config? vim.lsp.Config @Optional config to apply
269+
function M.register_server(server, config)
270+
vim.validate("server", server, "string", false)
271+
vim.validate("config", config, "table", true)
272+
273+
if config then
274+
vim.lsp.config(server, config)
275+
end
276+
vim.lsp.enable(server)
277+
end
278+
266279
---Convert number (0/1) to boolean
267280
---@param value number @The value to check
268281
---@return boolean|nil @Returns nil if failed

0 commit comments

Comments
 (0)