mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-22 00:49:03 +02:00
feat(lsp): document highlights now use native lsp. vim-illuminate
is available as an extra
This commit is contained in:
parent
960ec8079b
commit
cc99b219de
5 changed files with 125 additions and 41 deletions
|
@ -21,7 +21,7 @@ function M.get_clients(opts)
|
|||
return opts and opts.filter and vim.tbl_filter(opts.filter, ret) or ret
|
||||
end
|
||||
|
||||
---@param on_attach fun(client, buffer)
|
||||
---@param on_attach fun(client:lsp.Client, buffer)
|
||||
function M.on_attach(on_attach)
|
||||
vim.api.nvim_create_autocmd("LspAttach", {
|
||||
callback = function(args)
|
||||
|
@ -125,4 +125,76 @@ function M.format(opts)
|
|||
end
|
||||
end
|
||||
|
||||
---@alias LspWord {from:{[1]:number, [2]:number}, to:{[1]:number, [2]:number}, current?:boolean} 1-0 indexed
|
||||
M.words = {}
|
||||
M.words.ns = vim.api.nvim_create_namespace("vim_lsp_references")
|
||||
|
||||
---@param opts? {enabled?: boolean}
|
||||
function M.words.setup(opts)
|
||||
opts = opts or {}
|
||||
if not opts.enabled then
|
||||
return
|
||||
end
|
||||
M.on_attach(function(client, buf)
|
||||
if client.supports_method("textDocument/documentHighlight") then
|
||||
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI", "CursorMoved", "CursorMovedI" }, {
|
||||
group = vim.api.nvim_create_augroup("lsp_word_" .. buf, { clear = true }),
|
||||
buffer = buf,
|
||||
callback = function(ev)
|
||||
if not M.words.at() then
|
||||
if ev.event:find("CursorMoved") then
|
||||
vim.lsp.buf.clear_references()
|
||||
else
|
||||
vim.lsp.buf.document_highlight()
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
vim.keymap.set("n", "]]", function()
|
||||
M.words.jump(vim.v.count1)
|
||||
end, { buffer = buf })
|
||||
vim.keymap.set("n", "[[", function()
|
||||
M.words.jump(-vim.v.count1)
|
||||
end, { buffer = buf })
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@return LspWord[]
|
||||
function M.words.get()
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
return vim.tbl_map(function(extmark)
|
||||
local ret = {
|
||||
from = { extmark[2] + 1, extmark[3] },
|
||||
to = { extmark[4].end_row + 1, extmark[4].end_col },
|
||||
}
|
||||
if cursor[1] >= ret.from[1] and cursor[1] <= ret.to[1] and cursor[2] >= ret.from[2] and cursor[2] <= ret.to[2] then
|
||||
ret.current = true
|
||||
end
|
||||
return ret
|
||||
end, vim.api.nvim_buf_get_extmarks(0, M.words.ns, 0, -1, { details = true }))
|
||||
end
|
||||
|
||||
---@param words? LspWord[]
|
||||
---@return LspWord?, number?
|
||||
function M.words.at(words)
|
||||
for idx, word in ipairs(words or M.words.get()) do
|
||||
if word.current then
|
||||
return word, idx
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function M.words.jump(count)
|
||||
local words = M.words.get()
|
||||
local _, idx = M.words.at(words)
|
||||
if not idx then
|
||||
return
|
||||
end
|
||||
local target = words[idx + count]
|
||||
if target then
|
||||
vim.api.nvim_win_set_cursor(0, target.from)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue