From c2ff6d6b78fa0368659e4880e550f99be94bf477 Mon Sep 17 00:00:00 2001 From: Yi Ming Date: Fri, 6 Dec 2024 02:42:42 +0800 Subject: [PATCH] feat(lsp): allow synchronous refresh of document highlights --- runtime/doc/lsp.txt | 2 ++ runtime/lua/vim/lsp/document_highlight.lua | 25 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt index e17b7a92ab..f41c3557f4 100644 --- a/runtime/doc/lsp.txt +++ b/runtime/doc/lsp.txt @@ -1611,6 +1611,8 @@ jump({opts}) *vim.lsp.document_highlight.jump()* find the nearest highlight. Default is the current cursor position. • {winid}? (`integer`, default: `0`) Window ID + • {refresh}? (`boolean`) Refresh documents highlights + immediately before jumping. *vim.lsp.document_highlight.on_document_highlight()* on_document_highlight({result}, {ctx}) diff --git a/runtime/lua/vim/lsp/document_highlight.lua b/runtime/lua/vim/lsp/document_highlight.lua index b410bb8149..58049cd325 100644 --- a/runtime/lua/vim/lsp/document_highlight.lua +++ b/runtime/lua/vim/lsp/document_highlight.lua @@ -141,7 +141,7 @@ function M.on_document_highlight(err, result, ctx) end ---@param bufnr integer ----@param opts? {client_id?: integer} +---@param opts? {client_id?: integer, sync?: boolean} local function refresh(bufnr, opts) local bufstate = assert(bufstates[bufnr]) local enabled = bufstate.enabled @@ -167,7 +167,21 @@ local function refresh(bufnr, opts) for _, client in ipairs(clients) do 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 @@ -436,6 +450,9 @@ end ---Window ID ---(default: `0`) ---@field winid? integer +--- +---Refresh documents highlights immediately before jumping. +---@field refresh? boolean ---Move to a document highlight ---@param opts vim.lsp.document_highlight.JumpOpts @@ -456,6 +473,10 @@ function M.jump(opts) return end + if opts.refresh then + refresh(bufnr, { sync = true }) + end + while count > 0 do pos = jump_next(bufnr, pos, bufstate.client_highlights) count = count - 1