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.lua47
-rw-r--r--lua/lvim/lsp/utils.lua114
4 files changed, 134 insertions, 58 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 44923123..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
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