mirror of
https://github.com/LunarVim/LunarVim.git
synced 2025-09-01 08:15:20 +02:00
refactor: use api-autocmds for lsp functions (#2549)
* refactor: use api-autocmds for lsp functions * chore: use the existing client.supports_method api * fix: a callback in an autocmd doesn't pass nil * revert: keep changes minimal to which-key Co-authored-by: Abouzar Parvan <abzcoding@users.noreply.github.com>
This commit is contained in:
parent
10449b4e09
commit
cfa702e6fe
9 changed files with 168 additions and 125 deletions
|
@ -7,6 +7,8 @@ return {
|
|||
pattern = "*",
|
||||
---@usage timeout number timeout in ms for the format request (Default: 1000)
|
||||
timeout = 1000,
|
||||
---@usage filter func to select client
|
||||
filter = require("lvim.lsp.handlers").format_filter,
|
||||
},
|
||||
keys = {},
|
||||
|
||||
|
|
|
@ -70,79 +70,44 @@ local get_format_on_save_opts = function()
|
|||
}
|
||||
end
|
||||
|
||||
function M.enable_format_on_save(opts)
|
||||
local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout)
|
||||
M.define_augroups {
|
||||
format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } },
|
||||
}
|
||||
function M.enable_format_on_save()
|
||||
local opts = get_format_on_save_opts()
|
||||
vim.api.nvim_create_augroup("lsp_format_on_save", {})
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
group = "lsp_format_on_save",
|
||||
pattern = opts.pattern,
|
||||
callback = function()
|
||||
vim.lsp.buf.format { timeout_ms = opts.timeout, filter = opts.filter }
|
||||
end,
|
||||
})
|
||||
Log:debug "enabled format-on-save"
|
||||
end
|
||||
|
||||
function M.disable_format_on_save()
|
||||
M.disable_augroup "format_on_save"
|
||||
pcall(vim.api.nvim_del_augroup_by_name, "lsp_format_on_save")
|
||||
Log:debug "disabled format-on-save"
|
||||
end
|
||||
|
||||
function M.configure_format_on_save()
|
||||
if lvim.format_on_save then
|
||||
local opts = get_format_on_save_opts()
|
||||
M.enable_format_on_save(opts)
|
||||
M.enable_format_on_save()
|
||||
else
|
||||
M.disable_format_on_save()
|
||||
end
|
||||
end
|
||||
|
||||
function M.toggle_format_on_save()
|
||||
if vim.fn.exists "#format_on_save#BufWritePre" == 0 then
|
||||
local opts = get_format_on_save_opts()
|
||||
M.enable_format_on_save(opts)
|
||||
local status, _ = pcall(vim.api.nvim_get_autocmds, {
|
||||
group = "lsp_format_on_save",
|
||||
event = "BufWritePre",
|
||||
})
|
||||
if not status then
|
||||
M.enable_format_on_save()
|
||||
else
|
||||
M.disable_format_on_save()
|
||||
end
|
||||
end
|
||||
|
||||
function M.enable_lsp_document_highlight(client_id)
|
||||
M.define_augroups({
|
||||
lsp_document_highlight = {
|
||||
{
|
||||
"CursorHold",
|
||||
"<buffer>",
|
||||
string.format("lua require('lvim.lsp.utils').conditional_document_highlight(%d)", client_id),
|
||||
},
|
||||
{
|
||||
"CursorMoved",
|
||||
"<buffer>",
|
||||
"lua vim.lsp.buf.clear_references()",
|
||||
},
|
||||
},
|
||||
}, true)
|
||||
end
|
||||
|
||||
function M.disable_lsp_document_highlight()
|
||||
M.disable_augroup "lsp_document_highlight"
|
||||
end
|
||||
|
||||
function M.enable_code_lens_refresh()
|
||||
M.define_augroups({
|
||||
lsp_code_lens_refresh = {
|
||||
{
|
||||
"InsertLeave ",
|
||||
"<buffer>",
|
||||
"lua vim.lsp.codelens.refresh()",
|
||||
},
|
||||
{
|
||||
"InsertLeave ",
|
||||
"<buffer>",
|
||||
"lua vim.lsp.codelens.display()",
|
||||
},
|
||||
},
|
||||
}, true)
|
||||
end
|
||||
|
||||
function M.disable_code_lens_refresh()
|
||||
M.disable_augroup "lsp_code_lens_refresh"
|
||||
end
|
||||
|
||||
function M.enable_transparent_mode()
|
||||
vim.cmd "au ColorScheme * hi Normal ctermbg=none guibg=none"
|
||||
vim.cmd "au ColorScheme * hi SignColumn ctermbg=none guibg=none"
|
||||
|
@ -170,7 +135,6 @@ end
|
|||
|
||||
--- Create autocommand groups based on the passed definitions
|
||||
---@param definitions table contains trigger, pattern and text. The key will be used as a group name
|
||||
---@param buffer boolean indicate if the augroup should be local to the buffer
|
||||
function M.define_augroups(definitions, buffer)
|
||||
for group_name, definition in pairs(definitions) do
|
||||
vim.cmd("augroup " .. group_name)
|
||||
|
|
|
@ -153,32 +153,31 @@ M.config = function()
|
|||
"Git Diff",
|
||||
},
|
||||
},
|
||||
|
||||
l = {
|
||||
name = "LSP",
|
||||
a = { "<cmd>lua vim.lsp.buf.code_action()<cr>", "Code Action" },
|
||||
d = { "<cmd>Telescope diagnostics bufnr=0 theme=get_ivy<cr>", "Buffer Diagnostics" },
|
||||
w = { "<cmd>Telescope diagnostics<cr>", "Diagnostics" },
|
||||
f = { "<cmd>lua vim.lsp.buf.formatting()<cr>", "Format" },
|
||||
f = { require("lvim.lsp.utils").format, "Format" },
|
||||
i = { "<cmd>LspInfo<cr>", "Info" },
|
||||
I = { "<cmd>LspInstallInfo<cr>", "Installer Info" },
|
||||
j = {
|
||||
"<cmd>lua vim.diagnostic.goto_next()<cr>",
|
||||
vim.diagnostic.goto_next,
|
||||
"Next Diagnostic",
|
||||
},
|
||||
k = {
|
||||
"<cmd>lua vim.diagnostic.goto_prev()<cr>",
|
||||
vim.diagnostic.goto_prev,
|
||||
"Prev Diagnostic",
|
||||
},
|
||||
l = { "<cmd>lua vim.lsp.codelens.run()<cr>", "CodeLens Action" },
|
||||
l = { vim.lsp.codelens.run, "CodeLens Action" },
|
||||
p = {
|
||||
name = "Peek",
|
||||
d = { "<cmd>lua require('lvim.lsp.peek').Peek('definition')<cr>", "Definition" },
|
||||
t = { "<cmd>lua require('lvim.lsp.peek').Peek('typeDefinition')<cr>", "Type Definition" },
|
||||
i = { "<cmd>lua require('lvim.lsp.peek').Peek('implementation')<cr>", "Implementation" },
|
||||
},
|
||||
q = { "<cmd>lua vim.diagnostic.setloclist()<cr>", "Quickfix" },
|
||||
r = { "<cmd>lua vim.lsp.buf.rename()<cr>", "Rename" },
|
||||
q = { vim.diagnostic.setloclist, "Quickfix" },
|
||||
r = { vim.lsp.buf.rename, "Rename" },
|
||||
s = { "<cmd>Telescope lsp_document_symbols<cr>", "Document Symbols" },
|
||||
S = {
|
||||
"<cmd>Telescope lsp_dynamic_workspace_symbols<cr>",
|
||||
|
|
|
@ -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",
|
||||
},
|
||||
},
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -143,7 +143,9 @@ function plugin_loader.sync_core_plugins()
|
|||
vim.api.nvim_create_autocmd("User", {
|
||||
pattern = "PackerComplete",
|
||||
once = true,
|
||||
callback = require("lvim.plugin-loader").load_snapshot,
|
||||
callback = function()
|
||||
require("lvim.plugin-loader").load_snapshot(default_snapshot)
|
||||
end,
|
||||
})
|
||||
pcall_packer_command "sync"
|
||||
end
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
"commit": "4781fcf"
|
||||
},
|
||||
"bufferline.nvim": {
|
||||
"commit": "f02e19b"
|
||||
"commit": "2d5266d"
|
||||
},
|
||||
"cmp-buffer": {
|
||||
"commit": "d66c4c2"
|
||||
|
@ -33,13 +33,13 @@
|
|||
"commit": "6e0881a"
|
||||
},
|
||||
"gitsigns.nvim": {
|
||||
"commit": "b800663"
|
||||
"commit": "61c8398"
|
||||
},
|
||||
"lua-dev.nvim": {
|
||||
"commit": "54149d1"
|
||||
},
|
||||
"lualine.nvim": {
|
||||
"commit": "030eb62"
|
||||
"commit": "45d07fc"
|
||||
},
|
||||
"nlsp-settings.nvim": {
|
||||
"commit": "fc3007e"
|
||||
|
@ -57,7 +57,7 @@
|
|||
"commit": "d6d8317"
|
||||
},
|
||||
"nvim-lsp-installer": {
|
||||
"commit": "3068a47"
|
||||
"commit": "193f171"
|
||||
},
|
||||
"nvim-lspconfig": {
|
||||
"commit": "21102d5"
|
||||
|
@ -102,7 +102,7 @@
|
|||
"commit": "281b07a"
|
||||
},
|
||||
"telescope.nvim": {
|
||||
"commit": "544c5ee"
|
||||
"commit": "23e28d0"
|
||||
},
|
||||
"toggleterm.nvim": {
|
||||
"commit": "6c7f5db"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue