summaryrefslogtreecommitdiff
path: root/lua/lvim/lsp
diff options
context:
space:
mode:
Diffstat (limited to 'lua/lvim/lsp')
-rw-r--r--lua/lvim/lsp/config.lua25
-rw-r--r--lua/lvim/lsp/handlers.lua6
-rw-r--r--lua/lvim/lsp/init.lua55
-rw-r--r--lua/lvim/lsp/manager.lua63
-rw-r--r--lua/lvim/lsp/null-ls/init.lua2
-rw-r--r--lua/lvim/lsp/templates.lua2
-rw-r--r--lua/lvim/lsp/utils.lua114
7 files changed, 174 insertions, 93 deletions
diff --git a/lua/lvim/lsp/config.lua b/lua/lvim/lsp/config.lua
index eada4ce7..a0e22107 100644
--- a/lua/lvim/lsp/config.lua
+++ b/lua/lvim/lsp/config.lua
@@ -94,15 +94,24 @@ return {
},
buffer_mappings = {
normal_mode = {
- ["K"] = { "<cmd>lua vim.lsp.buf.hover()<CR>", "Show hover" },
- ["gd"] = { "<cmd>lua vim.lsp.buf.definition()<CR>", "Goto Definition" },
- ["gD"] = { "<cmd>lua vim.lsp.buf.declaration()<CR>", "Goto declaration" },
- ["gr"] = { "<cmd>lua vim.lsp.buf.references()<CR>", "Goto references" },
- ["gI"] = { "<cmd>lua vim.lsp.buf.implementation()<CR>", "Goto Implementation" },
- ["gs"] = { "<cmd>lua vim.lsp.buf.signature_help()<CR>", "show signature help" },
- ["gp"] = { "<cmd>lua require'lvim.lsp.peek'.Peek('definition')<CR>", "Peek definition" },
+ ["K"] = { vim.lsp.buf.hover, "Show hover" },
+ ["gd"] = { vim.lsp.buf.definition, "Goto Definition" },
+ ["gD"] = { vim.lsp.buf.declaration, "Goto declaration" },
+ ["gr"] = { vim.lsp.buf.references, "Goto references" },
+ ["gI"] = { vim.lsp.buf.implementation, "Goto Implementation" },
+ ["gs"] = { vim.lsp.buf.signature_help, "show signature help" },
+ ["gp"] = {
+ function()
+ require("lvim.lsp.peek").Peek "definition"
+ end,
+ "Peek definition",
+ },
["gl"] = {
- "<cmd>lua require'lvim.lsp.handlers'.show_line_diagnostics()<CR>",
+ function()
+ local config = lvim.lsp.diagnostics.float
+ config.scope = "line"
+ vim.diagnostic.open_float(0, config)
+ end,
"Show line diagnostics",
},
},
diff --git a/lua/lvim/lsp/handlers.lua b/lua/lvim/lsp/handlers.lua
index 5da0b21e..84f2ba5f 100644
--- a/lua/lvim/lsp/handlers.lua
+++ b/lua/lvim/lsp/handlers.lua
@@ -16,10 +16,4 @@ function M.setup()
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, lvim.lsp.float)
end
-function M.show_line_diagnostics()
- local config = lvim.lsp.diagnostics.float
- config.scope = "line"
- return vim.diagnostic.open_float(0, config)
-end
-
return M
diff --git a/lua/lvim/lsp/init.lua b/lua/lvim/lsp/init.lua
index a02ca426..13fafae0 100644
--- a/lua/lvim/lsp/init.lua
+++ b/lua/lvim/lsp/init.lua
@@ -3,23 +3,6 @@ local Log = require "lvim.core.log"
local utils = require "lvim.utils"
local autocmds = require "lvim.core.autocmds"
-local function lsp_highlight_document(client)
- if lvim.lsp.document_highlight == false then
- return -- we don't need further
- end
- autocmds.enable_lsp_document_highlight(client.id)
-end
-
-local function lsp_code_lens_refresh(client)
- if lvim.lsp.code_lens_refresh == false then
- return
- end
-
- if client.resolved_capabilities.code_lens then
- autocmds.enable_code_lens_refresh()
- end
-end
-
local function add_lsp_buffer_keybindings(bufnr)
local mappings = {
normal_mode = "n",
@@ -65,28 +48,12 @@ function M.common_capabilities()
return capabilities
end
-local function select_default_formater(client)
- if client.name == "null-ls" or not client.resolved_capabilities.document_formatting then
- return
- end
- Log:debug("Checking for formatter overriding for " .. client.name)
- local formatters = require "lvim.lsp.null-ls.formatters"
- local client_filetypes = client.config.filetypes or {}
- for _, filetype in ipairs(client_filetypes) do
- if #vim.tbl_keys(formatters.list_registered(filetype)) > 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
- end
- end
-end
-
function M.common_on_exit(_, _)
if lvim.lsp.document_highlight then
- autocmds.disable_lsp_document_highlight()
+ pcall(vim.api.nvim_del_augroup_by_name, "lsp_document_highlight")
end
if lvim.lsp.code_lens_refresh then
- autocmds.disable_code_lens_refresh()
+ pcall(vim.api.nvim_del_augroup_by_name, "lsp_code_lens_refresh")
end
end
@@ -96,7 +63,6 @@ function M.common_on_init(client, bufnr)
Log:debug "Called lsp.on_init_callback"
return
end
- select_default_formater(client)
end
function M.common_on_attach(client, bufnr)
@@ -104,8 +70,13 @@ function M.common_on_attach(client, bufnr)
lvim.lsp.on_attach_callback(client, bufnr)
Log:debug "Called lsp.on_attach_callback"
end
- lsp_highlight_document(client)
- lsp_code_lens_refresh(client)
+ local lu = require "lvim.lsp.utils"
+ if lvim.lsp.document_highlight then
+ lu.setup_document_highlight(client, bufnr)
+ end
+ if lvim.lsp.code_lens_refresh == false then
+ lu.setup_codelens_refresh(client, bufnr)
+ end
add_lsp_buffer_keybindings(bufnr)
end
@@ -134,8 +105,10 @@ function M.setup()
return
end
- for _, sign in ipairs(lvim.lsp.diagnostics.signs.values) do
- vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = sign.name })
+ if lvim.use_icons then
+ for _, sign in ipairs(lvim.lsp.diagnostics.signs.values) do
+ vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = sign.name })
+ end
end
require("lvim.lsp.handlers").setup()
@@ -149,7 +122,7 @@ function M.setup()
append_default_schemas = true,
}
- require("nvim-lsp-installer").settings {
+ require("nvim-lsp-installer").setup {
-- use the default nvim_data_dir, since the server binaries are independent
install_root_dir = utils.join_paths(vim.call("stdpath", "data"), "lsp_servers"),
}
diff --git a/lua/lvim/lsp/manager.lua b/lua/lvim/lsp/manager.lua
index 09369d48..2f24298d 100644
--- a/lua/lvim/lsp/manager.lua
+++ b/lua/lvim/lsp/manager.lua
@@ -16,29 +16,27 @@ function M.init_defaults(languages)
end
end
----Resolve the configuration for a server based on both common and user configuration
----@param name string
----@param user_config table [optional]
+---Resolve the configuration for a server by merging with the default config
+---@param server_name string
+---@vararg any config table [optional]
---@return table
-local function resolve_config(name, user_config)
- local config = {
+local function resolve_config(server_name, ...)
+ local defaults = {
on_attach = require("lvim.lsp").common_on_attach,
on_init = require("lvim.lsp").common_on_init,
on_exit = require("lvim.lsp").common_on_exit,
capabilities = require("lvim.lsp").common_capabilities(),
}
- local has_custom_provider, custom_config = pcall(require, "lvim/lsp/providers/" .. name)
+ local has_custom_provider, custom_config = pcall(require, "lvim/lsp/providers/" .. server_name)
if has_custom_provider then
- Log:debug("Using custom configuration for requested server: " .. name)
- config = vim.tbl_deep_extend("force", config, custom_config)
+ Log:debug("Using custom configuration for requested server: " .. server_name)
+ defaults = vim.tbl_deep_extend("force", defaults, custom_config)
end
- if user_config then
- config = vim.tbl_deep_extend("force", config, user_config)
- end
+ defaults = vim.tbl_deep_extend("force", defaults, ...)
- return config
+ return defaults
end
-- manually start the server and don't wait for the usual filetype trigger from lspconfig
@@ -62,47 +60,52 @@ local function client_is_configured(server_name, ft)
return false
end
+local function launch_server(server_name, config)
+ pcall(function()
+ require("lspconfig")[server_name].setup(config)
+ buf_try_add(server_name)
+ end)
+end
+
---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
+---@param user_config table? when available it will take predence over any default configurations
function M.setup(server_name, user_config)
vim.validate { name = { server_name, "string" } }
+ user_config = user_config or {}
if lvim_lsp_utils.is_client_active(server_name) or client_is_configured(server_name) then
return
end
- local config = resolve_config(server_name, user_config)
-
local servers = require "nvim-lsp-installer.servers"
- local server_available, requested_server = servers.get_server(server_name)
+ local server_available, server = servers.get_server(server_name)
if not server_available then
- pcall(function()
- require("lspconfig")[server_name].setup(config)
- buf_try_add(server_name)
- end)
+ local config = resolve_config(server_name, user_config)
+ launch_server(server_name, config)
return
end
- local install_notification = false
+ local install_in_progress = false
- if not requested_server:is_installed() then
+ if not server:is_installed() then
if lvim.lsp.automatic_servers_installation then
Log:debug "Automatic server installation detected"
- requested_server:install()
- install_notification = true
+ server:install()
+ install_in_progress = true
else
- Log:debug(requested_server.name .. " is not managed by the automatic installer")
+ Log:debug(server.name .. " is not managed by the automatic installer")
end
end
- requested_server:on_ready(function()
- if install_notification then
- vim.notify(string.format("Installation complete for [%s] server", requested_server.name), vim.log.levels.INFO)
+ server:on_ready(function()
+ if install_in_progress then
+ vim.notify(string.format("Installation complete for [%s] server", server.name), vim.log.levels.INFO)
end
- install_notification = false
- requested_server:setup(config)
+ install_in_progress = false
+ local config = resolve_config(server_name, server:get_default_options(), user_config)
+ launch_server(server_name, config)
end)
end
diff --git a/lua/lvim/lsp/null-ls/init.lua b/lua/lvim/lsp/null-ls/init.lua
index 68e20c98..51a200f4 100644
--- a/lua/lvim/lsp/null-ls/init.lua
+++ b/lua/lvim/lsp/null-ls/init.lua
@@ -2,7 +2,7 @@ local M = {}
local Log = require "lvim.core.log"
-function M:setup()
+function M.setup()
local status_ok, null_ls = pcall(require, "null-ls")
if not status_ok then
Log:error "Missing null-ls dependency"
diff --git a/lua/lvim/lsp/templates.lua b/lua/lvim/lsp/templates.lua
index 38e68fb6..09f82430 100644
--- a/lua/lvim/lsp/templates.lua
+++ b/lua/lvim/lsp/templates.lua
@@ -46,7 +46,7 @@ end
---Generates ftplugin files based on a list of server_names
---The files are generated to a runtimepath: "$LUNARVIM_RUNTIME_DIR/site/after/ftplugin/template.lua"
----@param servers_names table list of servers to be enabled. Will add all by default
+---@param servers_names? table list of servers to be enabled. Will add all by default
function M.generate_templates(servers_names)
servers_names = servers_names or {}
diff --git a/lua/lvim/lsp/utils.lua b/lua/lvim/lsp/utils.lua
index ebc682e5..c976ff72 100644
--- a/lua/lvim/lsp/utils.lua
+++ b/lua/lvim/lsp/utils.lua
@@ -40,7 +40,7 @@ function M.get_client_capabilities(client_id)
end
local enabled_caps = {}
- for capability, status in pairs(client.resolved_capabilities) do
+ for capability, status in pairs(client.server_capabilities or client.resolved_capabilities) do
if status == true then
table.insert(enabled_caps, capability)
end
@@ -84,14 +84,116 @@ function M.get_all_supported_filetypes()
return vim.tbl_keys(lsp_installer_filetypes or {})
end
-function M.conditional_document_highlight(id)
- local client_ok, method_supported = pcall(function()
- return vim.lsp.get_client_by_id(id).resolved_capabilities.document_highlight
+function M.setup_document_highlight(client, bufnr)
+ local status_ok, highlight_supported = pcall(function()
+ return client.supports_method "textDocument/documentHighlight"
end)
- if not client_ok or not method_supported then
+ if not status_ok or not highlight_supported then
return
end
- vim.lsp.buf.document_highlight()
+ local augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, {
+ group = "lsp_document_highlight",
+ })
+ if not augroup_exist then
+ vim.api.nvim_create_augroup("lsp_document_highlight", {})
+ end
+ vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
+ group = "lsp_document_highlight",
+ buffer = bufnr,
+ callback = vim.lsp.buf.document_highlight,
+ })
+ vim.api.nvim_create_autocmd("CursorMoved", {
+ group = "lsp_document_highlight",
+ buffer = bufnr,
+ callback = vim.lsp.buf.clear_references,
+ })
+end
+
+function M.setup_codelens_refresh(client, bufnr)
+ local status_ok, codelens_supported = pcall(function()
+ return client.supports_method "textDocument/codeLens"
+ end)
+ if not status_ok or not codelens_supported then
+ return
+ end
+ local augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, {
+ group = "lsp_code_lens_refresh",
+ })
+ if not augroup_exist then
+ vim.api.nvim_create_augroup("lsp_code_lens_refresh", {})
+ end
+ vim.api.nvim_create_autocmd("InsertLeave", {
+ group = "lsp_code_lens_refresh",
+ buffer = bufnr,
+ callback = vim.lsp.codelens.refresh,
+ })
+ vim.api.nvim_create_autocmd("InsertLeave", {
+ group = "lsp_code_lens_refresh",
+ buffer = bufnr,
+ callback = vim.lsp.codelens.display,
+ })
+end
+
+---filter passed to vim.lsp.buf.format
+---gives higher priority to null-ls
+---@param clients table clients attached to a buffer
+---@return table chosen clients
+function M.format_filter(clients)
+ return vim.tbl_filter(function(client)
+ local status_ok, formatting_supported = pcall(function()
+ return client.supports_method "textDocument/formatting"
+ end)
+ -- give higher prio to null-ls
+ if status_ok and formatting_supported and client.name == "null-ls" then
+ return "null-ls"
+ else
+ return status_ok and formatting_supported and client.name
+ end
+ end, clients)
+end
+
+---Provide vim.lsp.buf.format for nvim <0.8
+---@param opts table
+function M.format(opts)
+ opts = opts or { filter = M.format_filter }
+
+ if vim.lsp.buf.format then
+ vim.lsp.buf.format(opts)
+ end
+
+ local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
+ local clients = vim.lsp.buf_get_clients(bufnr)
+
+ if opts.filter then
+ clients = opts.filter(clients)
+ elseif opts.id then
+ clients = vim.tbl_filter(function(client)
+ return client.id == opts.id
+ end, clients)
+ elseif opts.name then
+ clients = vim.tbl_filter(function(client)
+ return client.name == opts.name
+ end, clients)
+ end
+
+ clients = vim.tbl_filter(function(client)
+ return client.supports_method "textDocument/formatting"
+ end, clients)
+
+ if #clients == 0 then
+ vim.notify "[LSP] Format request failed, no matching language servers."
+ end
+
+ local timeout_ms = opts.timeout_ms or 1000
+ for _, client in pairs(clients) do
+ local params = vim.lsp.util.make_formatting_params(opts.formatting_options)
+ local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
+ if result and result.result then
+ vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding)
+ elseif err then
+ vim.notify(string.format("[LSP][%s] %s", client.name, err), vim.log.levels.WARN)
+ end
+ end
end
return M