mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-22 17:03:39 +02:00
refactor(lsp): refactored again :)
This commit is contained in:
parent
0006fe7a3c
commit
3db94e44a0
2 changed files with 36 additions and 34 deletions
|
@ -121,7 +121,7 @@ return {
|
||||||
require("lazyvim.plugins.lsp.keymaps").on_attach(client, buffer)
|
require("lazyvim.plugins.lsp.keymaps").on_attach(client, buffer)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
LazyVim.lsp.setup_dynamic_capability()
|
LazyVim.lsp.setup()
|
||||||
LazyVim.lsp.on_dynamic_capability(require("lazyvim.plugins.lsp.keymaps").on_attach)
|
LazyVim.lsp.on_dynamic_capability(require("lazyvim.plugins.lsp.keymaps").on_attach)
|
||||||
|
|
||||||
LazyVim.lsp.words.setup(opts.document_highlight)
|
LazyVim.lsp.words.setup(opts.document_highlight)
|
||||||
|
|
|
@ -34,7 +34,10 @@ function M.on_attach(on_attach)
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup_dynamic_capability()
|
---@type table<string, table<vim.lsp.Client, table<number, boolean>>>
|
||||||
|
M._supports_method = {}
|
||||||
|
|
||||||
|
function M.setup()
|
||||||
local register_capability = vim.lsp.handlers["client/registerCapability"]
|
local register_capability = vim.lsp.handlers["client/registerCapability"]
|
||||||
vim.lsp.handlers["client/registerCapability"] = function(err, res, ctx)
|
vim.lsp.handlers["client/registerCapability"] = function(err, res, ctx)
|
||||||
---@diagnostic disable-next-line: no-unknown
|
---@diagnostic disable-next-line: no-unknown
|
||||||
|
@ -49,6 +52,24 @@ function M.setup_dynamic_capability()
|
||||||
end
|
end
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
M.on_attach(M._check_methods)
|
||||||
|
M.on_dynamic_capability(M._check_methods)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param client vim.lsp.Client
|
||||||
|
function M._check_methods(client, buffer)
|
||||||
|
for method, clients in pairs(M._supports_method) do
|
||||||
|
clients[client] = clients[client] or {}
|
||||||
|
if not clients[client][buffer] then
|
||||||
|
if client.supports_method(method, { bufnr = buffer }) then
|
||||||
|
clients[client][buffer] = true
|
||||||
|
vim.api.nvim_exec_autocmds("User", {
|
||||||
|
pattern = "LspSupportsMethod",
|
||||||
|
data = { client_id = client.id, buffer = buffer, method = method },
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param fn fun(client:vim.lsp.Client, buffer):boolean?
|
---@param fn fun(client:vim.lsp.Client, buffer):boolean?
|
||||||
|
@ -67,34 +88,21 @@ function M.on_dynamic_capability(fn, opts)
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
M._on_supports_method_id = 0
|
|
||||||
|
|
||||||
---@param method string
|
---@param method string
|
||||||
---@param fn fun(client:vim.lsp.Client, buffer)
|
---@param fn fun(client:vim.lsp.Client, buffer)
|
||||||
function M.on_supports_method(method, fn)
|
function M.on_supports_method(method, fn)
|
||||||
M.on_attach(function(client, buffer)
|
M._supports_method[method] = M._supports_method[method] or setmetatable({}, { __mode = "k" })
|
||||||
if client.supports_method(method, { bufnr = buffer }) then
|
return vim.api.nvim_create_autocmd("User", {
|
||||||
fn(client, buffer)
|
pattern = "LspSupportsMethod",
|
||||||
else
|
callback = function(args)
|
||||||
M._on_supports_method_id = M._on_supports_method_id + 1
|
local client = vim.lsp.get_client_by_id(args.data.client_id)
|
||||||
local id = M._on_supports_method_id
|
local buffer = args.data.buffer ---@type number
|
||||||
local group = vim.api.nvim_create_augroup("on_supports_method_" .. id, { clear = true })
|
if client and method == args.data.method then
|
||||||
M.on_dynamic_capability(function(c, b)
|
return fn(client, buffer)
|
||||||
if c == client and b == buffer and client.supports_method(method, { bufnr = buffer }) then
|
|
||||||
fn(client, buffer)
|
|
||||||
pcall(vim.api.nvim_del_augroup_by_id, group)
|
|
||||||
end
|
end
|
||||||
end, { group = group })
|
|
||||||
vim.api.nvim_create_autocmd({ "LspDetach", "BufDelete" }, {
|
|
||||||
group = group,
|
|
||||||
buffer = buffer,
|
|
||||||
callback = function()
|
|
||||||
pcall(vim.api.nvim_del_augroup_by_id, group)
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param from string
|
---@param from string
|
||||||
---@param to string
|
---@param to string
|
||||||
|
@ -207,10 +215,9 @@ function M.words.setup(opts)
|
||||||
return handler(err, result, ctx, config)
|
return handler(err, result, ctx, config)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.on_supports_method("textDocument/documentHighlight", function(client, buf)
|
M.on_supports_method("textDocument/documentHighlight", function(_, buf)
|
||||||
local group = vim.api.nvim_create_augroup("lsp_word_" .. buf, { clear = true })
|
|
||||||
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI", "CursorMoved", "CursorMovedI" }, {
|
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI", "CursorMoved", "CursorMovedI" }, {
|
||||||
group = group,
|
group = vim.api.nvim_create_augroup("lsp_word_" .. buf, { clear = true }),
|
||||||
buffer = buf,
|
buffer = buf,
|
||||||
callback = function(ev)
|
callback = function(ev)
|
||||||
if not M.words.at() then
|
if not M.words.at() then
|
||||||
|
@ -222,11 +229,6 @@ function M.words.setup(opts)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
vim.api.nvim_create_autocmd("LspDetach", {
|
|
||||||
callback = function()
|
|
||||||
vim.api.nvim_create_augroup("lsp_word_" .. buf, { clear = true })
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue