diff --git a/README.md b/README.md index a174358..59abddd 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ AstroLSP provides a simple API for configuring and setting up language servers i ## ⚡️ Requirements -- Neovim >= 0.10 +- Neovim >= 0.11 ## 📦 Installation @@ -89,29 +89,24 @@ local opts = { desc = "Format file with LSP", }, }, - -- Configure default capabilities for language servers (`:h vim.lsp.protocol.make_client.capabilities()`) - capabilities = { - textDocument = { - foldingRange = { dynamicRegistration = false }, - }, - }, - -- Configure language servers for `lspconfig` (`:h lspconfig-setup`) + -- Configure language servers with `vim.lsp.config` (`:h vim.lsp.config`) config = { - lua_ls = { - settings = { - Lua = { - hint = { enable = true, arrayIndex = "Disable" }, + -- Configure LSP defaults + ["*"] = { + -- Configure default capabilities + capabilities = { + textDocument = { + foldingRange = { dynamicRegistration = false }, }, }, - }, - clangd = { - capabilities = { - offsetEncoding = "utf-8", + -- Custom flags table to be passed to all language servers + flags = { + exit_timeout = 5000, }, }, }, defaults = { - hover = { border = "rounded", silent = true } -- customize lsp hover window + hover = { border = "rounded", silent = true }, -- customize lsp hover window signature_help = false, -- disable any default customizations }, -- Configuration of LSP file operation functionality @@ -128,10 +123,6 @@ local opts = { didDelete = true, }, }, - -- A custom flags table to be passed to all language servers (`:h lspconfig-setup`) - flags = { - exit_timeout = 5000, - }, -- Configuration options for controlling formatting with language servers formatting = { -- control auto formatting on save @@ -158,10 +149,10 @@ local opts = { }, -- Configure how language servers get set up handlers = { - -- default handler, first entry with no key - function(server, opts) require("lspconfig")[server].setup(opts) end, + -- default handler uses key "*" + ["*"] = vim.lsp.enable, -- custom function handler for pyright - pyright = function(_, opts) require("lspconfig").pyright.setup(opts) end, + pyright = function() vim.lsp.enable "pyright" end, -- set to false to disable the setup of a language server rust_analyzer = false, }, @@ -198,25 +189,12 @@ local opts = { }, }, }, - -- Extra configuration for the `mason-lspconfig.nvim` plugin - mason_lspconfig = { - -- Allow registering more Mason packages as language servers for autodetection/setup - servers = { - -- The key is the lspconfig server name to register a package for - nextflow_ls = { - -- The Mason package name to register to the language server - package = "nextflow-language-server", - -- The filetypes that apply to the package and language server - filetypes = { "nextflow" }, - -- (Optional) any default configuration changes that may need to happen (can be a table or a function that returns a table) - config = { cmd = { "nextflow-language-server" } } - } - } - } -- A list like table of servers that should be setup, useful for enabling language servers not installed with Mason. servers = { "dartls" }, -- A custom `on_attach` function to be run after the default `on_attach` function, takes two parameters `client` and `bufnr` (`:h lspconfig-setup`) - on_attach = function(client, bufnr) client.server_capabilities.semanticTokensProvider = nil end, + on_attach = function(client, bufnr) + -- custom on_attach code to run on all servers + end, } ``` @@ -229,13 +207,8 @@ local opts = { ```lua { "neovim/nvim-lspconfig", - dependencies = { - { "AstroNvim/astrolsp", opts = {} }, - }, - config = function() - -- set up servers configured with AstroLSP - vim.tbl_map(require("astrolsp").lsp_setup, require("astrolsp").config.servers) - end, + dependencies = { "AstroNvim/astrolsp", opts = {} }, + opts = {} } ``` @@ -243,28 +216,12 @@ local opts = { ```lua { - "neovim/nvim-lspconfig", + "williamboman/mason-lspconfig.nvim", dependencies = { - { "AstroNvim/astrolsp", opts = {} }, - { - "williamboman/mason-lspconfig.nvim", -- MUST be set up before `nvim-lspconfig` - dependencies = { "williamboman/mason.nvim" }, - opts = { - -- use AstroLSP setup for mason-lspconfig - handlers = { function(server) require("astrolsp").lsp_setup(server) end }, - }, - config = function(_, opts) - -- Optionally tell AstroLSP to register new language servers before calling the `setup` function - -- this enables the `mason-lspconfig.servers` option in the AstroLSP configuration - require("astrolsp.mason-lspconfig").register_servers() - require("mason-lspconfig").setup(opts) - end - }, + "williamboman/mason.nvim", + { "neovim/nvim-lspconfig", dependencies = { "AstroNvim/astrolsp", opts = {} } }, }, - config = function() - -- set up servers configured with AstroLSP - vim.tbl_map(require("astrolsp").lsp_setup, require("astrolsp").config.servers) - end, + opts = {} } ``` @@ -276,7 +233,9 @@ local opts = { dependencies = { { "AstroNvim/astrolsp", opts = {} }, }, - opts = function() return { on_attach = require("astrolsp").on_attach } end, + opts = function(_, opts) + opts.on_attach = require("astrolsp").on_attach + end } ``` diff --git a/lua/astrolsp/config.lua b/lua/astrolsp/config.lua index 7164300..aee6e51 100644 --- a/lua/astrolsp/config.lua +++ b/lua/astrolsp/config.lua @@ -60,19 +60,7 @@ ---@field hover vim.lsp.buf.hover.Opts|false? control the default options for `vim.lsp.buf.hover()` (`:h vim.lsp.buf.hover.Opts`) ---@field signature_help vim.lsp.buf.signature_help.Opts|false? control the default options for `vim.lsp.buf.signature_help()` (`:h vim.lsp.buf.signature_help.Opts`) ----@class AstroLSPMasonLspconfigServer ----@field public package string the Mason package name ----@field filetypes string|string[] the filetype(s) that the server applies to ----@field config? table|(fun(): table) extensions tothe default language server configuration - ----@alias AstroLSPMasonLspconfigServers { [string]: AstroLSPMasonLspconfigServer } - ----@class AstroLSPMasonLspconfigOpts ----@field servers AstroLSPMasonLspconfigServers? a table of servers to register with mason-lspconfig.nvim - ---@class AstroLSPOpts ----_EXPERIMENTAL_ Use the native `vim.lsp.config` for LSP configuration ----@field native_lsp_config boolean? ---Configuration of auto commands ---The key into the table is the group name for the auto commands (`:h augroup`) and the value ---is a list of autocmd tables where `event` key is the event(s) that trigger the auto command @@ -133,33 +121,22 @@ ---} ---``` ---@field features AstroLSPFeatureOpts? ----Configure default capabilities for language servers (`:h vim.lsp.protocol.make_client.capabilities()`) +---Configure options for language servers passed to `vim.lsp.config(key, config)` (`:h vim.lsp.config`) ---Example -- ---```lua ----capabilities = { ---- textDocument = { ---- foldingRange = { dynamicRegistration = false } ---- } ----} ----``` ----@field capabilities lsp.ClientCapabilities? ----Configure language servers for `lspconfig` (`:h lspconfig-setup`) ----Example: --- ----```lua ---config = { ---- lua_ls = { ---- settings = { ---- Lua = { ---- hint = { enable = true, arrayIndex = "Disable" } +--- ["*"] = { +--- capabilities = { +--- textDocument = { +--- foldingRange = { dynamicRegistration = false } --- } ---- } +--- }, +--- flags = { exit_timeout = 5000 }, --- }, ---- clangd = { capabilities = { offsetEncoding = "utf-8" } }, ---} ---``` ----@field config lspconfig.options? +---@field config table? ---Configure default options passed to `vim.lsp.buf` functions ---Example: --- @@ -196,13 +173,6 @@ --- } ---``` ---@field file_operations AstroLSPFileOperationsOpts|false? ----A custom flags table to be passed to all language servers (`:h lspconfig-setup`) ----Example: --- ----```lua ----flags = { exit_timeout = 5000 } ----``` ----@field flags table? ---Configuration options for controlling formatting with language servers ---Example: -- @@ -240,18 +210,16 @@ ---```lua ---handlers = { --- -- default handler ---- function(server, opts) ---- require("lspconfig")[server].setup(opts) ---- end, +--- ["*"] = vim.lsp.enable, --- -- custom function handler for pyright ---- pyright = function(_, opts) ---- require("lspconfig").pyright.setup(opts) +--- pyright = function() +--- -- some custom logic --- end, --- -- set to false to disable the setup of a language server --- rust_analyzer = false, ---} ---``` ----@field handlers table? +---@field handlers table? ---Configure global LSP handlers, set a method to `false` to use the Neovim default (`:h vim.lsp.handlers`) ---Example: -- @@ -297,21 +265,6 @@ ---} ---``` ---@field mappings AstroLSPMappings? ----Extra options for the `mason-lspconfig.nvim` plugin such as registering new packages as language servers. ----Example: --- ----```lua ----mason_lspconfig = { ---- servers = { ---- nextflow_ls = { ---- package = "nextflow-language-server", ---- filetypes = "nextflow", ---- config = { cmd = { "nextflow-language-server" } } ---- } ---- } ----} ----``` ----@field mason_lspconfig AstroLSPMasonLspconfigOpts? ---A list like table of servers that should be setup, useful for enabling language servers not installed with Mason. ---Example: -- @@ -331,7 +284,6 @@ ---@type AstroLSPOpts local M = { - native_lsp_config = false, autocmds = {}, commands = {}, features = { @@ -340,17 +292,13 @@ local M = { semantic_tokens = true, signature_help = false, }, - capabilities = {}, - ---@diagnostic disable-next-line: missing-fields config = {}, defaults = {}, file_operations = { timeout = 10000, operations = {} }, - flags = {}, formatting = { format_on_save = { enabled = true }, disabled = {} }, handlers = {}, lsp_handlers = {}, mappings = {}, - mason_lspconfig = {}, servers = {}, on_attach = nil, } diff --git a/lua/astrolsp/file_operations.lua b/lua/astrolsp/file_operations.lua index c07469e..1788cc4 100644 --- a/lua/astrolsp/file_operations.lua +++ b/lua/astrolsp/file_operations.lua @@ -12,8 +12,6 @@ ---@class astrolsp.file_operations local M = {} -local utils = require "astrolsp.utils" - local config = vim.tbl_get(require "astrolsp", "config", "file_operations") or {} ---@class AstroLSPFileOperationsRename @@ -56,8 +54,7 @@ function M.didCreateFiles(fnames) local filters = did_create.filters or {} local filtered = vim.tbl_filter(function(fname) return match_filters(filters, fname) end, fnames) if next(filtered) then - utils.notify( - client, + client:notify( "workspace/didCreateFiles", { files = vim.tbl_map(function(fname) return { uri = vim.uri_from_fname(fname) } end, filtered) } ) @@ -77,8 +74,7 @@ function M.didDeleteFiles(fnames) local filters = did_delete.filters or {} local filtered = vim.tbl_filter(function(fname) return match_filters(filters, fname) end, fnames) if next(filtered) then - utils.notify( - client, + client:notify( "workspace/didDeleteFiles", { files = vim.tbl_map(function(fname) return { uri = vim.uri_from_fname(fname) } end, filtered) } ) @@ -101,7 +97,7 @@ function M.didRenameFiles(renames) renames ) if next(filtered) then - utils.notify(client, "workspace/didRenameFiles", { + client:notify("workspace/didRenameFiles", { files = vim.tbl_map( function(rename) return { oldUri = vim.uri_from_fname(rename.from), newUri = vim.uri_from_fname(rename.to) } end, filtered @@ -116,7 +112,7 @@ end ---@param req string ---@param params table local function getWorkspaceEdit(client, req, params) - local resp = utils.request_sync(client, req, params, config.timeout) + local resp = client:request_sync(req, params, config.timeout) if resp and resp.result then return resp.result end end diff --git a/lua/astrolsp/init.lua b/lua/astrolsp/init.lua index 1cc8c22..a890a32 100644 --- a/lua/astrolsp/init.lua +++ b/lua/astrolsp/init.lua @@ -9,22 +9,6 @@ ---@class astrolsp local M = {} --- TODO: Roadmap to AstroLSP v4 --- ============================ --- - Move `on_attach` to an autocommand on `LspAttach` event --- - Leave `on_attach` option to allow users to override the default `on_attach` easily --- - Remove `mason_lspconfig` option as this is improving with mason-lspconfig v2 --- - Rely only on `vim.lsp.config` --- - Remove `native_lsp_config` option --- - Make `capabilities` and `flags` AstroLSP opt call `vim.lsp.config("*", { capabilities = {}, flags = {} })` --- - Move logic for enabling servers into the `setup` function --- - Maintain `handlers` for setting up, allow users to decide not to call `vim.lsp.enable(server_name)` --- - Remove `config` from AstroLSP opts, users should use `lsp/` --- - Remove `lsp_opts(server_name)`, users should use `vim.lsp.config[server_name]` --- - Remove `lsp_config(server_name)`, users should use `vim.lsp.config[server_name]` - -local utils = require "astrolsp.utils" - local tbl_contains = vim.tbl_contains local tbl_isempty = vim.tbl_isempty @@ -43,7 +27,7 @@ local function lsp_event(name) vim.api.nvim_exec_autocmds("User", { pattern = "A local function check_cond(cond, client, bufnr) local cond_type = type(cond) if cond_type == "function" then return cond(client, bufnr) end - if cond_type == "string" then return utils.supports_method(client, cond, bufnr) end + if cond_type == "string" then return client:supports_method(cond, bufnr) end if cond_type == "boolean" then return cond end return true end @@ -66,46 +50,39 @@ end --- Helper function to set up a given server with the Neovim LSP client ---@param server string The name of the server to be setup function M.lsp_setup(server) - local opts, default_handler - if M.config.native_lsp_config then - opts = M.lsp_opts(server) - default_handler = function(server_name) vim.lsp.enable(server_name) end - else - -- if server doesn't exist, set it up from user server definition - local lspconfig_avail, lspconfig = pcall(require, "lspconfig") - if lspconfig_avail then - local config_avail, config = pcall(require, "lspconfig.configs." .. server) - if not config_avail or not config.default_config then - local server_definition = M.config.config[server] - if server_definition and server_definition.cmd then - require("lspconfig.configs")[server] = { default_config = server_definition } - end - end - end - opts = M.lsp_opts(server) - default_handler = function(server_name, _opts) - if lspconfig_avail then - lspconfig[server_name].setup(_opts) - else - vim.notify(("No handler defined for `%s`"):format(server_name), vim.log.levels.WARN) - end - end - end - local handler = vim.F.if_nil(M.config.handlers[server], M.config.handlers[1], default_handler) - if handler then handler(server, opts) end + local handler = vim.F.if_nil(M.config.handlers[server], M.config.handlers["*"], vim.lsp.enable) + if handler then handler(server) end +end + +--- Set up a given `on_attach` function to run when language servers are attached +---@param on_attach fun(client:vim.lsp.Client, bufnr:integer) the `on_attach` function to run +---@param opts? { client_name: string?, autocmd: vim.api.keyset.create_autocmd? } options for configuring the `on_attach` +---@return integer autocmd_id The id for the created LspAttach autocommand +function M.add_on_attach(on_attach, opts) + if not opts then opts = {} end + local client_name, autocmd_opts = opts.client_name, opts.autocmd or {} + return vim.api.nvim_create_autocmd( + "LspAttach", + vim.tbl_deep_extend("force", autocmd_opts, { + callback = function(args) + local client = vim.lsp.get_client_by_id(args.data.client_id) + if client and (not client_name or client_name == client.name) then return on_attach(client, args.buf) end + end, + }) + ) end --- The `on_attach` function used by AstroNvim ---@param client vim.lsp.Client The LSP client details when attaching ---@param bufnr integer The buffer that the LSP client is attaching to function M.on_attach(client, bufnr) - if utils.supports_method(client, "textDocument/codeLens", bufnr) and M.config.features.codelens then + if client:supports_method("textDocument/codeLens", bufnr) and M.config.features.codelens then vim.lsp.codelens.refresh { bufnr = bufnr } end local formatting_disabled = vim.tbl_get(M.config, "formatting", "disabled") if - utils.supports_method(client, "textDocument/formatting", bufnr) + client:supports_method("textDocument/formatting", bufnr) and (formatting_disabled ~= true and not tbl_contains(formatting_disabled, client.name)) then local autoformat = assert(M.config.formatting.format_on_save) @@ -117,7 +94,7 @@ function M.on_attach(client, bufnr) end end - if utils.supports_method(client, "textDocument/semanticTokens/full", bufnr) and vim.lsp.semantic_tokens then + if client:supports_method("textDocument/semanticTokens/full", bufnr) and vim.lsp.semantic_tokens then if M.config.features.semantic_tokens then if vim.b[bufnr].semantic_tokens == nil then vim.b[bufnr].semantic_tokens = true end else @@ -202,51 +179,6 @@ function M.on_attach(client, bufnr) if not M.attached_clients[client.id] then M.attached_clients[client.id] = client end end --- A table of LSP clients that have been configured already -local is_configured = {} - ---- Configure the language server using `vim.lsp.config` ----@param server_name string The name of the server ----@return vim.lsp.Config # The resolved configuration -function M.lsp_config(server_name) - if not is_configured[server_name] then - local config = M.config.config[server_name] or {} - local existing_on_attach = (vim.lsp.config[server_name] or {}).on_attach - local user_on_attach = config.on_attach - config.on_attach = function(...) - if type(existing_on_attach) == "function" then existing_on_attach(...) end - M.on_attach(...) - if type(user_on_attach) == "function" then user_on_attach(...) end - end - vim.lsp.config(server_name, config) - is_configured[server_name] = true - end - return vim.lsp.config[server_name] -end - ---- Get the server configuration for a given language server to be provided to the server's `setup()` call ----@param server_name string The name of the server ----@return table # The table of LSP options used when setting up the given language server -function M.lsp_opts(server_name) - -- if native vim.lsp.config, then just return current configuration - if M.config.native_lsp_config then return M.lsp_config(server_name) or {} end - local opts = { capabilities = M.config.capabilities, flags = M.config.flags } - if M.config.config[server_name] then opts = vim.tbl_deep_extend("force", opts, M.config.config[server_name]) end - assert(opts) - - local lspconfig_avail, lspconfig = pcall(require, "lspconfig") - local old_on_attach = lspconfig_avail - and require("lspconfig.configs")[server_name] - and lspconfig[server_name].on_attach - local user_on_attach = opts.on_attach - opts.on_attach = function(client, bufnr) - if type(old_on_attach) == "function" then old_on_attach(client, bufnr) end - M.on_attach(client, bufnr) - if type(user_on_attach) == "function" then user_on_attach(client, bufnr) end - end - return opts -end - local key_cache = {} ---@type { [string]: string } ---@param mappings AstroLSPMappings? @@ -294,22 +226,28 @@ function M.setup(opts) end M.config = vim.tbl_deep_extend(extend_method, M.config, opts) - if not vim.lsp.config then -- disable native `vim.lsp.config` if not available - M.config.native_lsp_config = false - end - -- enable necessary capabilities for enabled LSP file operations local fileOperations = vim.tbl_get(M.config, "file_operations", "operations") if fileOperations and not vim.tbl_isempty(fileOperations) then - M.config.capabilities = vim.tbl_deep_extend("force", M.config.capabilities or {}, { - workspace = { fileOperations = fileOperations }, + M.config.config = vim.tbl_deep_extend("force", M.config.config or {}, { + ["*"] = { + capabilities = { workspace = { fileOperations = fileOperations } }, + }, }) end - if M.config.native_lsp_config then - vim.lsp.config("*", { capabilities = M.config.capabilities, flags = M.config.flags }) + for server, config in pairs(M.config.config) do + vim.lsp.config(server, config) end + -- Set up tracking of signature help trigger characters + M.add_on_attach(M.on_attach, { + autocmd = { + group = vim.api.nvim_create_augroup("astrolsp_on_attach", { clear = true }), + desc = "AstroLSP on_attach function", + }, + }) + local rename_augroup = vim.api.nvim_create_augroup("astrolsp_rename_operations", { clear = true }) vim.api.nvim_create_autocmd("User", { group = rename_augroup, @@ -348,22 +286,22 @@ function M.setup(opts) -- Set up tracking of signature help trigger characters local augroup = vim.api.nvim_create_augroup("track_signature_help_triggers", { clear = true }) - vim.api.nvim_create_autocmd("LspAttach", { - group = augroup, - desc = "Add signature help triggers as language servers attach", - callback = function(args) - local client = vim.lsp.get_client_by_id(args.data.client_id) - if client and utils.supports_method(client, "textDocument/signatureHelp", args.buf) then - for _, set in ipairs { "triggerCharacters", "retriggerCharacters" } do - local set_var = "signature_help_" .. set - local triggers = vim.b[args.buf][set_var] or {} - for _, trigger in ipairs(client.server_capabilities.signatureHelpProvider[set] or {}) do - triggers[trigger] = true - end - vim.b[args.buf][set_var] = triggers + M.add_on_attach(function(client, bufnr) + if client:supports_method("textDocument/signatureHelp", bufnr) then + for _, set in ipairs { "triggerCharacters", "retriggerCharacters" } do + local set_var = "signature_help_" .. set + local triggers = vim.b[bufnr][set_var] or {} + for _, trigger in ipairs(client.server_capabilities.signatureHelpProvider[set] or {}) do + triggers[trigger] = true end + vim.b[bufnr][set_var] = triggers end - end, + end + end, { + autocmd = { + group = augroup, + desc = "Add signature help triggers as language servers attach", + }, }) vim.api.nvim_create_autocmd("LspDetach", { group = augroup, @@ -372,9 +310,7 @@ function M.setup(opts) if not vim.api.nvim_buf_is_valid(args.buf) then return end local triggers, retriggers = {}, {} for _, client in pairs(vim.lsp.get_clients { bufnr = args.buf }) do - if - client.id ~= args.data.client_id and utils.supports_method(client, "textDocument/signatureHelp", args.buf) - then + if client.id ~= args.data.client_id and client:supports_method("textDocument/signatureHelp", args.buf) then for _, trigger in ipairs(client.server_capabilities.signatureHelpProvider.triggerCharacters or {}) do triggers[trigger] = true end @@ -427,21 +363,10 @@ function M.setup(opts) for method, default in pairs(M.config.defaults) do if default then - -- TODO: remove conditional after dropping support for Neovim v0.10 - if vim.fn.has "nvim-0.11" == 1 then - local original_method = vim.lsp.buf[method] - if type(original_method) == "function" then - vim.lsp.buf[method] = function(user_opts) - return original_method(vim.tbl_deep_extend("force", default, user_opts or {})) - end - end - else - local deprecated_handler = ({ - hover = "textDocument/hover", - signature_help = "textDocument/signatureHelp", - })[method] - if deprecated_handler and default then - vim.lsp.handlers[deprecated_handler] = vim.lsp.with(vim.lsp.handlers[deprecated_handler], default) + local original_method = vim.lsp.buf[method] + if type(original_method) == "function" then + vim.lsp.buf[method] = function(user_opts) + return original_method(vim.tbl_deep_extend("force", default, user_opts or {})) end end end @@ -450,6 +375,8 @@ function M.setup(opts) for method, handler in pairs(M.config.lsp_handlers or {}) do if handler then vim.lsp.handlers[method] = handler end end + + vim.tbl_map(M.lsp_setup, M.config.servers) end return M diff --git a/lua/astrolsp/mason-lspconfig.lua b/lua/astrolsp/mason-lspconfig.lua deleted file mode 100644 index 7f071b9..0000000 --- a/lua/astrolsp/mason-lspconfig.lua +++ /dev/null @@ -1,58 +0,0 @@ ----Utilities for working with mason-lspconfig.nvim ---- ----This module can be loaded with `local astrolsp_mason_lspconfig = require "astrolsp.mason-lspconfig"` ---- ----copyright 2025 ----license GNU General Public License v3.0 ----@class astrolsp.mason-lspconfig -local M = {} - -local function resolve_config() return require("astrolsp").config.mason_lspconfig or {} end - ---- Register a new language server with mason-lspconfig ----@param server string the server name in lspconfig ----@param spec AstroLSPMasonLspconfigServer the details for registering the server -function M.register_server(server, spec) - local filetype_mappings_avail, filetype_mappings = pcall(require, "mason-lspconfig.mappings.filetype") - local server_mappings_avail, server_mappings = pcall(require, "mason-lspconfig.mappings.server") - - if not (filetype_mappings_avail and server_mappings_avail) then - vim.notify("Unable to properly load required `mason-lspconfig` modules", vim.log.levels.ERROR) - end - - -- register server in the filetype maps - local filetypes = spec.filetypes - if type(filetypes) ~= "table" then filetypes = { filetypes } end - for _, filetype in ipairs(filetypes) do - if not filetype_mappings[filetype] then filetype_mappings[filetype] = {} end - table.insert(filetype_mappings, server) - end - -- register the mappings between lspconfig server name and mason package name - server_mappings.lspconfig_to_package[server] = spec.package - server_mappings.package_to_lspconfig[spec.package] = server - -- if a config is provided, set up a mason-lspconfig server configuration module - if spec.config then - local module = spec.config - if type(module) == "table" then - local orig_function = module - module = function() return orig_function end - end - local module_name = "mason-lspconfig.server_configurations." .. server - if package.loaded[module_name] == nil then - package.preload[module_name] = function() return module end - else - package.loaded[module_name] = module - end - end -end - ---- Register multiple new language servers with mason-lspconfig ----@param server_specs? AstroLSPMasonLspconfigServers -function M.register_servers(server_specs) - if not server_specs then server_specs = resolve_config().servers or {} end - for server, spec in pairs(server_specs) do - M.register_server(server, spec) - end -end - -return M diff --git a/lua/astrolsp/toggles.lua b/lua/astrolsp/toggles.lua index 9816e5c..3cc0546 100644 --- a/lua/astrolsp/toggles.lua +++ b/lua/astrolsp/toggles.lua @@ -9,8 +9,6 @@ ---@class astrolsp.toggles local M = {} -local utils = require "astrolsp.utils" - local config = require("astrolsp").config local features = config.features --[[@as AstroLSPFeatureOpts]] local format_on_save = config.formatting.format_on_save --[[@as AstroLSPFormatOnSaveOpts]] @@ -67,10 +65,8 @@ function M.buffer_semantic_tokens(bufnr, silent) vim.b[bufnr].semantic_tokens = not vim.b[bufnr].semantic_tokens local toggled = false for _, client in ipairs(vim.lsp.get_clients { bufnr = bufnr }) do - if utils.supports_method(client, "textDocument/semanticTokens/full", bufnr) then - -- HACK: `semantic_tokens.start/stop` don't support 0 for current buffer - local real_bufnr = bufnr == 0 and vim.api.nvim_get_current_buf() or bufnr - vim.lsp.semantic_tokens[vim.b[bufnr].semantic_tokens and "start" or "stop"](real_bufnr, client.id) + if client:supports_method("textDocument/semanticTokens/full", bufnr) then + vim.lsp.semantic_tokens[vim.b[bufnr].semantic_tokens and "start" or "stop"](bufnr, client.id) vim.lsp.semantic_tokens.force_refresh(bufnr) toggled = true end diff --git a/lua/astrolsp/utils.lua b/lua/astrolsp/utils.lua deleted file mode 100644 index 0475eaa..0000000 --- a/lua/astrolsp/utils.lua +++ /dev/null @@ -1,53 +0,0 @@ ----Utilities for interacting with the Neovim LSP integration ---- ----This module can be loaded with `local astrolsp_utils = require "astrolsp.utils"` ---- ----copyright 2025 ----license GNU General Public License v3.0 ----@class astrolsp.utils -local M = {} - --- TODO: remove helper functions when dropping support for Neovim v0.10 - ---- Helper function to support deprecated supports_method usage ----@param client vim.lsp.Client ----@param method string ----@param bufnr? integer -function M.supports_method(client, method, bufnr) - if vim.fn.has "nvim-0.11" == 1 then - return client:supports_method(method, bufnr) - else - ---@diagnostic disable-next-line: param-type-mismatch - return client.supports_method(method, { bufnr = bufnr }) - end -end - ---- Helper function to support deprecated request_sync usage ----@param client vim.lsp.Client ----@param req string ----@param params table ----@param timeout? integer ----@param bufnr? integer -function M.request_sync(client, req, params, timeout, bufnr) - if vim.fn.has "nvim-0.11" == 1 then - return client:request_sync(req, params, timeout, bufnr) - else - ---@diagnostic disable-next-line: param-type-mismatch - return client.request_sync(req, params, timeout, bufnr) - end -end - ---- Helper function to support deprecated notify usage ----@param client vim.lsp.Client ----@param method string ----@param params? table -function M.notify(client, method, params) - if vim.fn.has "nvim-0.11" == 1 then - return client:notify(method, params) - else - ---@diagnostic disable-next-line: param-type-mismatch - return client.notify(method, params) - end -end - -return M