From df66b461596d351367f90d41a0fabffb873de4be Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Mon, 11 Oct 2021 22:03:16 +0200 Subject: Merge updates from lunarvim --- .config/nvim/lua/lsp/config.lua | 1 + .config/nvim/lua/lsp/init.lua | 23 +++++++- .config/nvim/lua/lsp/manager.lua | 87 ++++++++++++++--------------- .config/nvim/lua/lsp/null-ls/formatters.lua | 19 ++++--- .config/nvim/lua/lsp/null-ls/init.lua | 16 ++++-- .config/nvim/lua/lsp/null-ls/linters.lua | 19 ++++--- .config/nvim/lua/lsp/null-ls/services.lua | 4 +- .config/nvim/lua/lsp/providers/jsonls.lua | 12 ++-- .config/nvim/lua/lsp/templates.lua | 2 +- .config/nvim/lua/lsp/utils.lua | 26 +++------ 10 files changed, 116 insertions(+), 93 deletions(-) (limited to '.config/nvim/lua/lsp') diff --git a/.config/nvim/lua/lsp/config.lua b/.config/nvim/lua/lsp/config.lua index 32185b5..f13d965 100644 --- a/.config/nvim/lua/lsp/config.lua +++ b/.config/nvim/lua/lsp/config.lua @@ -17,6 +17,7 @@ return { }, override = {}, document_highlight = true, + code_lens_refresh = true, popup_border = "single", on_attach_callback = nil, on_init_callback = nil, diff --git a/.config/nvim/lua/lsp/init.lua b/.config/nvim/lua/lsp/init.lua index e3967df..f421d99 100644 --- a/.config/nvim/lua/lsp/init.lua +++ b/.config/nvim/lua/lsp/init.lua @@ -21,6 +21,25 @@ local function lsp_highlight_document(client) end end +local function lsp_code_lens_refresh(client) + if options.lsp.code_lens_refresh == false then + return + end + + if client.resolved_capabilities.code_lens then + vim.api.nvim_exec( + [[ + augroup lsp_code_lens_refresh + autocmd! * + autocmd InsertLeave lua vim.lsp.codelens.refresh() + autocmd InsertLeave lua vim.lsp.codelens.display() + augroup END + ]], + false + ) + end +end + local function add_lsp_buffer_keybindings(bufnr) local mappings = { normal_mode = "n", @@ -75,11 +94,10 @@ local function select_default_formater(client) Log:debug("Checking for formatter overriding for " .. client.name) local client_filetypes = client.config.filetypes or {} for _, filetype in ipairs(client_filetypes) do - if not vim.tbl_isempty(options.lang[filetype].formatters) then + if options.lang[filetype] and #vim.tbl_keys(options.lang[filetype].formatters) > 0 then Log:debug("Formatter overriding detected. Disabling formatting capabilities for " .. client.name) client.resolved_capabilities.document_formatting = false client.resolved_capabilities.document_range_formatting = false - return end end end @@ -99,6 +117,7 @@ function M.common_on_attach(client, bufnr) Log:debug "Called lsp.on_attach_callback" end lsp_highlight_document(client) + lsp_code_lens_refresh(client) add_lsp_buffer_keybindings(bufnr) end diff --git a/.config/nvim/lua/lsp/manager.lua b/.config/nvim/lua/lsp/manager.lua index 49771b2..a7d6892 100644 --- a/.config/nvim/lua/lsp/manager.lua +++ b/.config/nvim/lua/lsp/manager.lua @@ -24,32 +24,12 @@ local function is_overridden(server) end end -function M.setup_server(server_name) - vim.validate { - name = { server_name, "string" }, - } - - if lsp_utils.is_client_active(server_name) or is_overridden(server_name) then - return - end - - local lsp_installer_servers = require "nvim-lsp-installer.servers" - local server_available, requested_server = lsp_installer_servers.get_server( - server_name - ) - if server_available then - if not requested_server:is_installed() then - Log:debug(string.format("[%s] is not installed", server_name)) - if options.lsp.automatic_servers_installation then - Log:debug(string.format("Installing [%s]", server_name)) - requested_server:install() - else - return - end - end - end - - local default_config = { +---Resolve the configuration for a server based on both common and user configuration +---@param name string +---@param user_config table [optional] +---@return table +local function resolve_config(name, user_config) + local config = { on_attach = require("lsp").common_on_attach, on_init = require("lsp").common_on_init, capabilities = require("lsp").common_capabilities(), @@ -57,35 +37,52 @@ function M.setup_server(server_name) local status_ok, custom_config = pcall( require, - "lsp/providers/" .. requested_server.name + "lsp/providers/" .. name ) if status_ok then - local new_config = vim.tbl_deep_extend("force", default_config, custom_config) - Log:debug( - "Using custom configuration for requested server: " .. requested_server.name - ) - requested_server:setup(new_config) - else - Log:debug( - "Using the default configuration for requested server: " .. requested_server.name - ) - requested_server:setup(default_config) + Log:debug("Using custom configuration for requested server: " .. name) + config = vim.tbl_deep_extend("force", config, custom_config) end + + if user_config then + config = vim.tbl_deep_extend("force", config, user_config) + end + + return config end -function M.setup(servers) - local status_ok, _ = pcall(require, "nvim-lsp-installer") - if not status_ok then +---Setup a language server by providing a name +---@param server_name string name of the language server +---@param user_config table [optional] when available it will take predence over any default configurations +function M.setup(server_name, user_config) + vim.validate { name = { server_name, "string" } } + + if lsp_utils.is_client_active(server_name) or is_overridden(server_name) then return end - --- allow using a single value - if type(servers) == "string" then - servers = { servers } + local config = resolve_config(server_name, user_config) + local server_available, requested_server = require("nvim-lsp-installer.servers").get_server(server_name) + + local function ensure_installed(server) + if server:is_installed() then + return true + end + if not lvim.lsp.automatic_servers_installation then + Log:debug(server.name .. " is not managed by the automatic installer") + return false + end + Log:debug(string.format("Installing [%s]", server.name)) + server:install() + vim.schedule(function() + vim.cmd [[LspStart]] + end) end - for _, server in ipairs(servers) do - M.setup_server(server) + if server_available and ensure_installed(requested_server) then + requested_server:setup(config) + else + require("lspconfig")[server_name].setup(config) end end diff --git a/.config/nvim/lua/lsp/null-ls/formatters.lua b/.config/nvim/lua/lsp/null-ls/formatters.lua index 0d3505f..577a0d9 100644 --- a/.config/nvim/lua/lsp/null-ls/formatters.lua +++ b/.config/nvim/lua/lsp/null-ls/formatters.lua @@ -13,9 +13,13 @@ end function M.list_available(filetype) local formatters = {} + local tbl = require "utils.table" for _, provider in pairs(null_ls.builtins.formatting) do - -- TODO: Add support for wildcard filetypes - if vim.tbl_contains(provider.filetypes or {}, filetype) then + if + tbl.contains(provider.filetypes or {}, function(ft) + return ft == "*" or ft == filetype + end) + then table.insert(formatters, provider.name) end end @@ -27,7 +31,8 @@ function M.list_configured(formatter_configs) local formatters, errors = {}, {} for _, fmt_config in ipairs(formatter_configs) do - local formatter = null_ls.builtins.formatting[fmt_config.exe] + local formatter_name = fmt_config.exe:gsub("-", "_") + local formatter = null_ls.builtins.formatting[formatter_name] if not formatter then Log:error("Not a valid formatter: " .. fmt_config.exe) @@ -42,6 +47,7 @@ function M.list_configured(formatter_configs) formatters[fmt_config.exe] = formatter.with { command = formatter_cmd, extra_args = fmt_config.args, + filetypes = fmt_config.filetypes, } end end @@ -50,14 +56,13 @@ function M.list_configured(formatter_configs) return { supported = formatters, unsupported = errors } end -function M.setup(formatter_configs, filetype) +function M.setup(formatter_configs) if vim.tbl_isempty(formatter_configs) then return end - local formatters_by_ft = {} - formatters_by_ft[filetype] = M.list_configured(formatter_configs) - null_ls.register { sources = formatters_by_ft[filetype].supported } + local formatters_by_ft = M.list_configured(formatter_configs) + null_ls.register { sources = formatters_by_ft.supported } end return M diff --git a/.config/nvim/lua/lsp/null-ls/init.lua b/.config/nvim/lua/lsp/null-ls/init.lua index 571fb6b..fde9fed 100644 --- a/.config/nvim/lua/lsp/null-ls/init.lua +++ b/.config/nvim/lua/lsp/null-ls/init.lua @@ -13,12 +13,18 @@ function M:setup() null_ls.config() require("lspconfig")["null-ls"].setup(options.lsp.null_ls.setup) - for _, filetype in pairs(options.lang) do - if filetype.formatters then - formatters.setup(filetype.formatters, filetype) + for filetype, config in pairs(options.lang) do + if not vim.tbl_isempty(config.formatters) then + vim.tbl_map(function(c) + c.filetypes = { filetype } + end, config.formatters) + formatters.setup(config.formatters) end - if filetype.linters then - linters.setup(filetype.linters, filetype) + if not vim.tbl_isempty(config.linters) then + vim.tbl_map(function(c) + c.filetypes = { filetype } + end, config.formatters) + linters.setup(config.linters) end end end diff --git a/.config/nvim/lua/lsp/null-ls/linters.lua b/.config/nvim/lua/lsp/null-ls/linters.lua index e9e92e9..3f834ba 100644 --- a/.config/nvim/lua/lsp/null-ls/linters.lua +++ b/.config/nvim/lua/lsp/null-ls/linters.lua @@ -13,9 +13,13 @@ end function M.list_available(filetype) local linters = {} + local tbl = require "utils.table" for _, provider in pairs(null_ls.builtins.diagnostics) do - -- TODO: Add support for wildcard filetypes - if vim.tbl_contains(provider.filetypes or {}, filetype) then + if + tbl.contains(provider.filetypes or {}, function(ft) + return ft == "*" or ft == filetype + end) + then table.insert(linters, provider.name) end end @@ -27,7 +31,8 @@ function M.list_configured(linter_configs) local linters, errors = {}, {} for _, lnt_config in pairs(linter_configs) do - local linter = null_ls.builtins.diagnostics[lnt_config.exe] + local linter_name = lnt_config.exe:gsub("-", "_") + local linter = null_ls.builtins.diagnostics[linter_name] if not linter then Log:error("Not a valid linter: " .. lnt_config.exe) @@ -42,6 +47,7 @@ function M.list_configured(linter_configs) linters[lnt_config.exe] = linter.with { command = linter_cmd, extra_args = lnt_config.args, + filetypes = lnt_config.filetypes, } end end @@ -50,14 +56,13 @@ function M.list_configured(linter_configs) return { supported = linters, unsupported = errors } end -function M.setup(linter_configs, filetype) +function M.setup(linter_configs) if vim.tbl_isempty(linter_configs) then return end - local linters_by_ft = {} - linters_by_ft[filetype] = M.list_configured(linter_configs) - null_ls.register { sources = linters_by_ft[filetype].supported } + local linters = M.list_configured(linter_configs) + null_ls.register { sources = linters.supported } end return M diff --git a/.config/nvim/lua/lsp/null-ls/services.lua b/.config/nvim/lua/lsp/null-ls/services.lua index c62fc70..ef9e7d2 100644 --- a/.config/nvim/lua/lsp/null-ls/services.lua +++ b/.config/nvim/lua/lsp/null-ls/services.lua @@ -4,8 +4,8 @@ local function find_root_dir() local util = require "lspconfig/util" local lsp_utils = require "lsp.utils" - local status_ok, ts_client = lsp_utils.is_client_active "typescript" - if status_ok then + local ts_client = lsp_utils.is_client_active "typescript" + if ts_client then return ts_client.config.root_dir end local dirname = vim.fn.expand "%:p:h" diff --git a/.config/nvim/lua/lsp/providers/jsonls.lua b/.config/nvim/lua/lsp/providers/jsonls.lua index 70ad7e1..e81b2c3 100644 --- a/.config/nvim/lua/lsp/providers/jsonls.lua +++ b/.config/nvim/lua/lsp/providers/jsonls.lua @@ -184,11 +184,13 @@ local opts = { schemas = extended_schemas, }, }, - commands = { - Format = { - function() - vim.lsp.buf.range_formatting({}, { 0, 0 }, { vim.fn.line "$", 0 }) - end, + setup = { + commands = { + Format = { + function() + vim.lsp.buf.range_formatting({}, { 0, 0 }, { vim.fn.line "$", 0 }) + end, + }, }, }, } diff --git a/.config/nvim/lua/lsp/templates.lua b/.config/nvim/lua/lsp/templates.lua index 004187e..2cce567 100644 --- a/.config/nvim/lua/lsp/templates.lua +++ b/.config/nvim/lua/lsp/templates.lua @@ -23,7 +23,7 @@ function M.is_ignored(server_name, filetypes) filetypes = filetypes or get_supported_filetypes(server_name) if vim.tbl_contains(filetypes, "javascript") then - if server_name == "tsserver" or server_name == "tailwindcss" then + if server_name == "tsserver" then return false else return true diff --git a/.config/nvim/lua/lsp/utils.lua b/.config/nvim/lua/lsp/utils.lua index e0046db..5900340 100644 --- a/.config/nvim/lua/lsp/utils.lua +++ b/.config/nvim/lua/lsp/utils.lua @@ -1,27 +1,15 @@ local M = {} +local tbl = require "utils.table" + function M.is_client_active(name) local clients = vim.lsp.get_active_clients() - for _, client in pairs(clients) do - if client.name == name then - return true, client - end - end - return false -end - -function M.disable_formatting_capability(client) - -- FIXME: figure out a reasonable way to do this - client.resolved_capabilities.document_formatting = false - require("core.log"):debug( - string.format( - "Turning off formatting capability for language server [%s] ", - client.name - ) - ) + return tbl.find_first(clients, function(client) + return client.name == name + end) end -function M.get_active_client_by_ft(filetype) +function M.get_active_clients_by_ft(filetype) local matches = {} local clients = vim.lsp.get_active_clients() for _, client in pairs(clients) do @@ -33,7 +21,7 @@ function M.get_active_client_by_ft(filetype) return matches end -function M.get_ls_capabilities(client_id) +function M.get_client_capabilities(client_id) if not client_id then local buf_clients = vim.lsp.buf_get_clients() for _, buf_client in ipairs(buf_clients) do -- cgit v1.2.3-70-g09d2