Skip to content

Commit db1ab0f

Browse files
committed
feat: add experimental support for native vim.lsp.config
1 parent 59a0228 commit db1ab0f

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

lua/astrolsp/config.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
---@field servers AstroLSPMasonLspconfigServers? a table of servers to register with mason-lspconfig.nvim
7171

7272
---@class AstroLSPOpts
73+
---_EXPERIMENTAL_ Use the native `vim.lsp.config` for LSP configuration
74+
---@field native_lsp_config boolean?
7375
---Configuration of auto commands
7476
---The key into the table is the group name for the auto commands (`:h augroup`) and the value
7577
---is a list of autocmd tables where `event` key is the event(s) that trigger the auto command
@@ -328,6 +330,7 @@
328330

329331
---@type AstroLSPOpts
330332
local M = {
333+
native_lsp_config = false,
331334
autocmds = {},
332335
commands = {},
333336
features = {

lua/astrolsp/init.lua

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ M.config = require "astrolsp.config"
2020
M.lsp_progress = {}
2121
--- A table of LSP clients that have been attached with AstroLSP
2222
M.attached_clients = {}
23+
-- A table of LSP clients that have been configured already
24+
M.is_configured = {}
2325

2426
local function lsp_event(name) vim.api.nvim_exec_autocmds("User", { pattern = "AstroLsp" .. name, modeline = false }) end
2527

@@ -52,29 +54,34 @@ end
5254
--- Helper function to set up a given server with the Neovim LSP client
5355
---@param server string The name of the server to be setup
5456
function M.lsp_setup(server)
55-
-- if server doesn't exist, set it up from user server definition
56-
local lspconfig_avail, lspconfig = pcall(require, "lspconfig")
57-
if lspconfig_avail then
58-
local config_avail, config = pcall(require, "lspconfig.configs." .. server)
59-
if not config_avail or not config.default_config then
60-
local server_definition = M.config.config[server]
61-
if server_definition and server_definition.cmd then
62-
require("lspconfig.configs")[server] = { default_config = server_definition }
57+
local opts, default_handler
58+
if M.config.native_lsp_config then
59+
opts = M.lsp_opts(server)
60+
default_handler = function(server_name) vim.lsp.enable(server_name) end
61+
else
62+
-- if server doesn't exist, set it up from user server definition
63+
local lspconfig_avail, lspconfig = pcall(require, "lspconfig")
64+
if lspconfig_avail then
65+
local config_avail, config = pcall(require, "lspconfig.configs." .. server)
66+
if not config_avail or not config.default_config then
67+
local server_definition = M.config.config[server]
68+
if server_definition and server_definition.cmd then
69+
require("lspconfig.configs")[server] = { default_config = server_definition }
70+
end
71+
end
72+
end
73+
opts = M.lsp_opts(server)
74+
default_handler = function(server_name, _opts)
75+
if lspconfig_avail then
76+
lspconfig[server_name].setup(_opts)
77+
else
78+
vim.notify(("No handler defined for `%s`"):format(server_name), vim.log.levels.WARN)
6379
end
6480
end
6581
end
66-
local opts = M.lsp_opts(server)
6782
local handler = M.config.handlers[server]
6883
if handler == nil then handler = M.config.handlers[1] end
69-
if handler then
70-
handler(server, opts)
71-
elseif handler == nil then
72-
if lspconfig_avail then
73-
lspconfig[server].setup(opts)
74-
else
75-
vim.notify(("No handler defined for `%s`"):format(server), vim.log.levels.WARN)
76-
end
77-
end
84+
(handler or default_handler)(server, opts)
7885
end
7986

8087
--- The `on_attach` function used by AstroNvim
@@ -184,10 +191,29 @@ function M.on_attach(client, bufnr)
184191
if not M.attached_clients[client.id] then M.attached_clients[client.id] = client end
185192
end
186193

194+
--- Configure the language server using `vim.lsp.config`
195+
---@param server_name string The name of the server
196+
function M.lsp_config(server_name)
197+
local config = M.config.config[server_name] or {}
198+
local existing_on_attach = (vim.lsp.config[server_name] or {}).on_attach
199+
local user_on_attach = config.on_attach
200+
config.on_attach = function(...)
201+
if type(existing_on_attach) == "function" then existing_on_attach(...) end
202+
M.on_attach(...)
203+
if type(user_on_attach) == "function" then user_on_attach(...) end
204+
end
205+
vim.lsp.config(server_name, config)
206+
end
207+
187208
--- Get the server configuration for a given language server to be provided to the server's `setup()` call
188209
---@param server_name string The name of the server
189210
---@return table # The table of LSP options used when setting up the given language server
190211
function M.lsp_opts(server_name)
212+
-- if native vim.lsp.config, then just return current configuration
213+
if M.config.native_lsp_config then
214+
if not M.is_configured[server_name] then M.lsp_config(server_name) end
215+
return vim.lsp.config[server_name]
216+
end
191217
local opts = { capabilities = M.config.capabilities, flags = M.config.flags }
192218
if M.config.config[server_name] then opts = vim.tbl_deep_extend("force", opts, M.config.config[server_name]) end
193219
assert(opts)
@@ -230,13 +256,22 @@ function M.setup(opts)
230256
normalize_mappings(opts.mappings)
231257
M.config = vim.tbl_deep_extend("force", M.config, opts)
232258

259+
if not vim.lsp.config then -- disable native `vim.lsp.config` if not available
260+
M.config.native_lsp_config = false
261+
end
262+
233263
-- enable necessary capabilities for enabled LSP file operations
234264
local fileOperations = vim.tbl_get(M.config, "file_operations", "operations")
235265
if fileOperations and not vim.tbl_isempty(fileOperations) then
236266
M.config.capabilities = vim.tbl_deep_extend("force", M.config.capabilities or {}, {
237267
workspace = { fileOperations = fileOperations },
238268
})
239269
end
270+
271+
if M.config.native_lsp_config then
272+
vim.lsp.config("*", { capabilities = M.config.capabilities, flags = M.config.flags })
273+
end
274+
240275
local rename_augroup = vim.api.nvim_create_augroup("astrolsp_rename_operations", { clear = true })
241276
vim.api.nvim_create_autocmd("User", {
242277
group = rename_augroup,

0 commit comments

Comments
 (0)