refactor(lsp): drop vim.lsp.util._refresh() #33903

Problem:
- util._refresh() is only used by `inlay_hint.lua` and `document_color.lua`, and
  both have their own wrapper functions;
- util._refresh() provides unified parameters, but this layer of wrapping is
  almost meaningless because
  - document color does not need the range parameter;
  - inlay hint requires a range parameter, but it is not complicated

Therefore, it can be considered redundant.
ref https://github.com/neovim/neovim/pull/32887#discussion_r1996413602

Solution:
Remove it.
This commit is contained in:
Yi Ming
2025-07-13 13:00:10 +08:00
committed by GitHub
parent 89b946aa87
commit 73fbc693b5
3 changed files with 39 additions and 75 deletions

View File

@ -252,11 +252,17 @@ end
--- @param bufnr integer
--- @param client_id? integer
function M._buf_refresh(bufnr, client_id)
util._refresh(ms.textDocument_documentColor, {
bufnr = bufnr,
handler = on_document_color,
client_id = client_id,
})
for _, client in
ipairs(lsp.get_clients({
bufnr = bufnr,
id = client_id,
method = ms.textDocument_documentColor,
}))
do
---@type lsp.DocumentColorParams
local params = { textDocument = util.make_text_document_params(bufnr) }
client:request(ms.textDocument_documentColor, params, on_document_color)
end
end
--- Query whether document colors are enabled in the given buffer.

View File

@ -85,6 +85,29 @@ function M.on_inlayhint(err, result, ctx)
api.nvim__redraw({ buf = bufnr, valid = true, flush = false })
end
--- Refresh inlay hints, only if we have attached clients that support it
---@param bufnr (integer) Buffer handle, or 0 for current
---@param client_id? (integer) Client ID, or nil for all
local function refresh(bufnr, client_id)
for _, client in
ipairs(vim.lsp.get_clients({
bufnr = bufnr,
id = client_id,
method = ms.textDocument_inlayHint,
}))
do
client:request(ms.textDocument_inlayHint, {
textDocument = util.make_text_document_params(bufnr),
range = util._make_line_range_params(
bufnr,
0,
api.nvim_buf_line_count(bufnr) - 1,
client.offset_encoding
),
}, nil, bufnr)
end
end
--- |lsp-handler| for the method `workspace/inlayHint/refresh`
---@param ctx lsp.HandlerContext
---@private
@ -97,7 +120,7 @@ function M.on_refresh(err, _, ctx)
if api.nvim_win_get_buf(winid) == bufnr then
if bufstates[bufnr] and bufstates[bufnr].enabled then
bufstates[bufnr].applied = {}
util._refresh(ms.textDocument_inlayHint, { bufnr = bufnr })
refresh(bufnr)
end
end
end
@ -229,22 +252,13 @@ local function _disable(bufnr)
bufstates[bufnr].enabled = false
end
--- Refresh inlay hints, only if we have attached clients that support it
---@param bufnr (integer) Buffer handle, or 0 for current
---@param opts? vim.lsp.util._refresh.Opts Additional options to pass to util._refresh
local function _refresh(bufnr, opts)
opts = opts or {}
opts['bufnr'] = bufnr
util._refresh(ms.textDocument_inlayHint, opts)
end
--- Enable inlay hints for a buffer
---@param bufnr (integer) Buffer handle, or 0 for current
local function _enable(bufnr)
bufnr = vim._resolve_bufnr(bufnr)
bufstates[bufnr] = nil
bufstates[bufnr].enabled = true
_refresh(bufnr)
refresh(bufnr)
end
api.nvim_create_autocmd('LspNotify', {
@ -259,7 +273,7 @@ api.nvim_create_autocmd('LspNotify', {
return
end
if bufstates[bufnr].enabled then
_refresh(bufnr, { client_id = args.data.client_id })
refresh(bufnr, args.data.client_id)
end
end,
group = augroup,
@ -274,7 +288,7 @@ api.nvim_create_autocmd('LspAttach', {
clear(cb_bufnr)
if bufstates[cb_bufnr] and bufstates[cb_bufnr].enabled then
bufstates[cb_bufnr].applied = {}
_refresh(cb_bufnr)
refresh(cb_bufnr)
end
end,
on_detach = function(_, cb_bufnr)

View File

@ -2224,7 +2224,7 @@ end
---@param end_line integer
---@param position_encoding 'utf-8'|'utf-16'|'utf-32'
---@return lsp.Range
local function make_line_range_params(bufnr, start_line, end_line, position_encoding)
function M._make_line_range_params(bufnr, start_line, end_line, position_encoding)
local last_line = api.nvim_buf_line_count(bufnr) - 1
---@type lsp.Position
@ -2284,62 +2284,6 @@ function M._cancel_requests(filter)
end
end
---@class (private) vim.lsp.util._refresh.Opts
---@field bufnr integer? Buffer to refresh (default: 0)
---@field only_visible? boolean Whether to only refresh for the visible regions of the buffer (default: false)
---@field client_id? integer Client ID to refresh (default: all clients)
---@field handler? lsp.Handler
--- Request updated LSP information for a buffer.
---
---@param method vim.lsp.protocol.Method.ClientToServer.Request LSP method to call
---@param opts? vim.lsp.util._refresh.Opts Options table
function M._refresh(method, opts)
opts = opts or {}
local bufnr = vim._resolve_bufnr(opts.bufnr)
local clients = vim.lsp.get_clients({ bufnr = bufnr, method = method, id = opts.client_id })
if #clients == 0 then
return
end
local textDocument = M.make_text_document_params(bufnr)
if opts.only_visible then
for _, window in ipairs(api.nvim_list_wins()) do
if api.nvim_win_get_buf(window) == bufnr then
local first = vim.fn.line('w0', window)
local last = vim.fn.line('w$', window)
M._cancel_requests({
bufnr = bufnr,
clients = clients,
method = method,
type = 'pending',
})
for _, client in ipairs(clients) do
client:request(method, {
textDocument = textDocument,
range = make_line_range_params(bufnr, first - 1, last - 1, client.offset_encoding),
}, opts.handler, bufnr)
end
end
end
else
for _, client in ipairs(clients) do
client:request(method, {
textDocument = textDocument,
range = make_line_range_params(
bufnr,
0,
api.nvim_buf_line_count(bufnr) - 1,
client.offset_encoding
),
}, opts.handler, bufnr)
end
end
end
---@param feature string
---@param client_id? integer
local function make_enable_var(feature, client_id)