feat(lsp): allow synchronous refresh of document highlights

This commit is contained in:
Yi Ming 2024-12-06 02:42:42 +08:00
parent 8bb15e4cf4
commit c2ff6d6b78
2 changed files with 25 additions and 2 deletions

View File

@ -1611,6 +1611,8 @@ jump({opts}) *vim.lsp.document_highlight.jump()*
find the nearest highlight. Default is the current cursor find the nearest highlight. Default is the current cursor
position. position.
• {winid}? (`integer`, default: `0`) Window ID • {winid}? (`integer`, default: `0`) Window ID
• {refresh}? (`boolean`) Refresh documents highlights
immediately before jumping.
*vim.lsp.document_highlight.on_document_highlight()* *vim.lsp.document_highlight.on_document_highlight()*
on_document_highlight({result}, {ctx}) on_document_highlight({result}, {ctx})

View File

@ -141,7 +141,7 @@ function M.on_document_highlight(err, result, ctx)
end end
---@param bufnr integer ---@param bufnr integer
---@param opts? {client_id?: integer} ---@param opts? {client_id?: integer, sync?: boolean}
local function refresh(bufnr, opts) local function refresh(bufnr, opts)
local bufstate = assert(bufstates[bufnr]) local bufstate = assert(bufstates[bufnr])
local enabled = bufstate.enabled local enabled = bufstate.enabled
@ -167,7 +167,21 @@ local function refresh(bufnr, opts)
for _, client in ipairs(clients) do for _, client in ipairs(clients) do
local params = util.make_position_params(0, client.offset_encoding) local params = util.make_position_params(0, client.offset_encoding)
client:request(ms.textDocument_documentHighlight, params, nil, bufnr)
if opts.sync then
local response = client:request_sync(ms.textDocument_documentHighlight, params, nil, bufnr)
if response == nil then
return
end
M.on_document_highlight(
response.err,
response.result,
{ bufnr = bufnr, client_id = client.id, method = ms.textDocument_documentHighlight }
)
else
client:request(ms.textDocument_documentHighlight, params, nil, bufnr)
end
end end
end end
@ -436,6 +450,9 @@ end
---Window ID ---Window ID
---(default: `0`) ---(default: `0`)
---@field winid? integer ---@field winid? integer
---
---Refresh documents highlights immediately before jumping.
---@field refresh? boolean
---Move to a document highlight ---Move to a document highlight
---@param opts vim.lsp.document_highlight.JumpOpts ---@param opts vim.lsp.document_highlight.JumpOpts
@ -456,6 +473,10 @@ function M.jump(opts)
return return
end end
if opts.refresh then
refresh(bufnr, { sync = true })
end
while count > 0 do while count > 0 do
pos = jump_next(bufnr, pos, bufstate.client_highlights) pos = jump_next(bufnr, pos, bufstate.client_highlights)
count = count - 1 count = count - 1