refactor: add vim._resolve_bufnr

This commit is contained in:
Lewis Russell 2024-12-07 13:05:05 +00:00 committed by Lewis Russell
parent b52ffd0a59
commit 668d2569b4
13 changed files with 61 additions and 121 deletions

View File

@ -53,7 +53,7 @@ function vim.inspect_pos(bufnr, row, col, filter)
local cursor = vim.api.nvim_win_get_cursor(win) local cursor = vim.api.nvim_win_get_cursor(win)
row, col = cursor[1] - 1, cursor[2] row, col = cursor[1] - 1, cursor[2]
end end
bufnr = bufnr == 0 and vim.api.nvim_get_current_buf() or bufnr bufnr = vim._resolve_bufnr(bufnr)
local results = { local results = {
treesitter = {}, --- @type table[] treesitter = {}, --- @type table[]

View File

@ -573,13 +573,6 @@ local underline_highlight_map = make_highlight_map('Underline')
local floating_highlight_map = make_highlight_map('Floating') local floating_highlight_map = make_highlight_map('Floating')
local sign_highlight_map = make_highlight_map('Sign') local sign_highlight_map = make_highlight_map('Sign')
local function get_bufnr(bufnr)
if not bufnr or bufnr == 0 then
return api.nvim_get_current_buf()
end
return bufnr
end
--- @param diagnostics vim.Diagnostic[] --- @param diagnostics vim.Diagnostic[]
--- @return table<integer,vim.Diagnostic[]> --- @return table<integer,vim.Diagnostic[]>
local function diagnostic_lines(diagnostics) local function diagnostic_lines(diagnostics)
@ -642,7 +635,7 @@ end
--- @param namespace integer --- @param namespace integer
--- @param bufnr? integer --- @param bufnr? integer
local function save_extmarks(namespace, bufnr) local function save_extmarks(namespace, bufnr)
bufnr = get_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
if not diagnostic_attached_buffers[bufnr] then if not diagnostic_attached_buffers[bufnr] then
api.nvim_buf_attach(bufnr, false, { api.nvim_buf_attach(bufnr, false, {
on_lines = function(_, _, _, _, _, last) on_lines = function(_, _, _, _, _, last)
@ -814,7 +807,7 @@ local function get_diagnostics(bufnr, opts, clamp)
end end
end end
elseif namespace == nil then elseif namespace == nil then
bufnr = get_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
for iter_namespace in pairs(diagnostic_cache[bufnr]) do for iter_namespace in pairs(diagnostic_cache[bufnr]) do
add_all_diags(bufnr, diagnostic_cache[bufnr][iter_namespace]) add_all_diags(bufnr, diagnostic_cache[bufnr][iter_namespace])
end end
@ -825,7 +818,7 @@ local function get_diagnostics(bufnr, opts, clamp)
end end
end end
else else
bufnr = get_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
for _, iter_namespace in ipairs(namespace) do for _, iter_namespace in ipairs(namespace) do
add_all_diags(bufnr, diagnostic_cache[bufnr][iter_namespace] or {}) add_all_diags(bufnr, diagnostic_cache[bufnr][iter_namespace] or {})
end end
@ -1100,7 +1093,7 @@ function M.set(namespace, bufnr, diagnostics, opts)
vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics')
vim.validate('opts', opts, 'table', true) vim.validate('opts', opts, 'table', true)
bufnr = get_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
if vim.tbl_isempty(diagnostics) then if vim.tbl_isempty(diagnostics) then
diagnostic_cache[bufnr][namespace] = nil diagnostic_cache[bufnr][namespace] = nil
@ -1380,7 +1373,7 @@ M.handlers.signs = {
vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics')
vim.validate('opts', opts, 'table', true) vim.validate('opts', opts, 'table', true)
bufnr = get_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
opts = opts or {} opts = opts or {}
if not api.nvim_buf_is_loaded(bufnr) then if not api.nvim_buf_is_loaded(bufnr) then
@ -1486,7 +1479,7 @@ M.handlers.underline = {
vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics')
vim.validate('opts', opts, 'table', true) vim.validate('opts', opts, 'table', true)
bufnr = get_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
opts = opts or {} opts = opts or {}
if not vim.api.nvim_buf_is_loaded(bufnr) then if not vim.api.nvim_buf_is_loaded(bufnr) then
@ -1550,7 +1543,7 @@ M.handlers.virtual_text = {
vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics') vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics')
vim.validate('opts', opts, 'table', true) vim.validate('opts', opts, 'table', true)
bufnr = get_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
opts = opts or {} opts = opts or {}
if not vim.api.nvim_buf_is_loaded(bufnr) then if not vim.api.nvim_buf_is_loaded(bufnr) then
@ -1675,7 +1668,7 @@ function M.hide(namespace, bufnr)
vim.validate('namespace', namespace, 'number', true) vim.validate('namespace', namespace, 'number', true)
vim.validate('bufnr', bufnr, 'number', true) vim.validate('bufnr', bufnr, 'number', true)
local buffers = bufnr and { get_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache) local buffers = bufnr and { vim._resolve_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache)
for _, iter_bufnr in ipairs(buffers) do for _, iter_bufnr in ipairs(buffers) do
local namespaces = namespace and { namespace } or vim.tbl_keys(diagnostic_cache[iter_bufnr]) local namespaces = namespace and { namespace } or vim.tbl_keys(diagnostic_cache[iter_bufnr])
for _, iter_namespace in ipairs(namespaces) do for _, iter_namespace in ipairs(namespaces) do
@ -1702,7 +1695,7 @@ function M.is_enabled(filter)
return vim.tbl_isempty(diagnostic_disabled) and not diagnostic_disabled[1] return vim.tbl_isempty(diagnostic_disabled) and not diagnostic_disabled[1]
end end
local bufnr = get_bufnr(filter.bufnr) local bufnr = vim._resolve_bufnr(filter.bufnr)
if type(diagnostic_disabled[bufnr]) == 'table' then if type(diagnostic_disabled[bufnr]) == 'table' then
return not diagnostic_disabled[bufnr][filter.ns_id] return not diagnostic_disabled[bufnr][filter.ns_id]
end end
@ -1743,7 +1736,7 @@ function M.show(namespace, bufnr, diagnostics, opts)
end end
else else
-- namespace is nil -- namespace is nil
bufnr = get_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
for iter_namespace in pairs(diagnostic_cache[bufnr]) do for iter_namespace in pairs(diagnostic_cache[bufnr]) do
M.show(iter_namespace, bufnr, nil, opts) M.show(iter_namespace, bufnr, nil, opts)
end end
@ -1810,7 +1803,7 @@ function M.open_float(opts, ...)
end end
opts = opts or {} opts = opts or {}
bufnr = get_bufnr(bufnr or opts.bufnr) bufnr = vim._resolve_bufnr(bufnr or opts.bufnr)
do do
-- Resolve options with user settings from vim.diagnostic.config -- Resolve options with user settings from vim.diagnostic.config
@ -2013,7 +2006,7 @@ function M.reset(namespace, bufnr)
vim.validate('namespace', namespace, 'number', true) vim.validate('namespace', namespace, 'number', true)
vim.validate('bufnr', bufnr, 'number', true) vim.validate('bufnr', bufnr, 'number', true)
local buffers = bufnr and { get_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache) local buffers = bufnr and { vim._resolve_bufnr(bufnr) } or vim.tbl_keys(diagnostic_cache)
for _, iter_bufnr in ipairs(buffers) do for _, iter_bufnr in ipairs(buffers) do
local namespaces = namespace and { namespace } or vim.tbl_keys(diagnostic_cache[iter_bufnr]) local namespaces = namespace and { namespace } or vim.tbl_keys(diagnostic_cache[iter_bufnr])
for _, iter_namespace in ipairs(namespaces) do for _, iter_namespace in ipairs(namespaces) do
@ -2151,7 +2144,7 @@ function M.enable(enable, filter)
ns.disabled = not enable ns.disabled = not enable
end end
else else
bufnr = get_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
if not ns_id then if not ns_id then
diagnostic_disabled[bufnr] = (not enable) and true or nil diagnostic_disabled[bufnr] = (not enable) and true or nil
else else

View File

@ -89,18 +89,6 @@ lsp._request_name_to_capability = {
-- TODO improve handling of scratch buffers with LSP attached. -- TODO improve handling of scratch buffers with LSP attached.
--- Returns the buffer number for the given {bufnr}.
---
---@param bufnr (integer|nil) Buffer number to resolve. Defaults to current buffer
---@return integer bufnr
local function resolve_bufnr(bufnr)
validate('bufnr', bufnr, 'number', true)
if bufnr == nil or bufnr == 0 then
return api.nvim_get_current_buf()
end
return bufnr
end
---@private ---@private
--- Called by the client when trying to call a method that's not --- Called by the client when trying to call a method that's not
--- supported in any of the servers registered for the current buffer. --- supported in any of the servers registered for the current buffer.
@ -389,7 +377,7 @@ end
function lsp.start(config, opts) function lsp.start(config, opts)
opts = opts or {} opts = opts or {}
local reuse_client = opts.reuse_client or reuse_client_default local reuse_client = opts.reuse_client or reuse_client_default
local bufnr = resolve_bufnr(opts.bufnr) local bufnr = vim._resolve_bufnr(opts.bufnr)
for _, client in pairs(all_clients) do for _, client in pairs(all_clients) do
if reuse_client(client, config) then if reuse_client(client, config) then
@ -528,7 +516,7 @@ end
---Buffer lifecycle handler for textDocument/didSave ---Buffer lifecycle handler for textDocument/didSave
--- @param bufnr integer --- @param bufnr integer
local function text_document_did_save_handler(bufnr) local function text_document_did_save_handler(bufnr)
bufnr = resolve_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
local uri = vim.uri_from_bufnr(bufnr) local uri = vim.uri_from_bufnr(bufnr)
local text = once(lsp._buf_get_full_text) local text = once(lsp._buf_get_full_text)
for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do
@ -689,7 +677,7 @@ end
function lsp.buf_attach_client(bufnr, client_id) function lsp.buf_attach_client(bufnr, client_id)
validate('bufnr', bufnr, 'number', true) validate('bufnr', bufnr, 'number', true)
validate('client_id', client_id, 'number') validate('client_id', client_id, 'number')
bufnr = resolve_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
if not api.nvim_buf_is_loaded(bufnr) then if not api.nvim_buf_is_loaded(bufnr) then
log.warn(string.format('buf_attach_client called on unloaded buffer (id: %d): ', bufnr)) log.warn(string.format('buf_attach_client called on unloaded buffer (id: %d): ', bufnr))
return false return false
@ -726,7 +714,7 @@ end
function lsp.buf_detach_client(bufnr, client_id) function lsp.buf_detach_client(bufnr, client_id)
validate('bufnr', bufnr, 'number', true) validate('bufnr', bufnr, 'number', true)
validate('client_id', client_id, 'number') validate('client_id', client_id, 'number')
bufnr = resolve_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
local client = all_clients[client_id] local client = all_clients[client_id]
if not client or not client.attached_buffers[bufnr] then if not client or not client.attached_buffers[bufnr] then
@ -832,7 +820,7 @@ function lsp.get_clients(filter)
local clients = {} --- @type vim.lsp.Client[] local clients = {} --- @type vim.lsp.Client[]
local bufnr = filter.bufnr and resolve_bufnr(filter.bufnr) local bufnr = filter.bufnr and vim._resolve_bufnr(filter.bufnr)
for _, client in pairs(all_clients) do for _, client in pairs(all_clients) do
if if
@ -928,7 +916,7 @@ function lsp.buf_request(bufnr, method, params, handler, on_unsupported)
validate('handler', handler, 'function', true) validate('handler', handler, 'function', true)
validate('on_unsupported', on_unsupported, 'function', true) validate('on_unsupported', on_unsupported, 'function', true)
bufnr = resolve_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
local method_supported = false local method_supported = false
local clients = lsp.get_clients({ bufnr = bufnr }) local clients = lsp.get_clients({ bufnr = bufnr })
local client_request_ids = {} --- @type table<integer,integer> local client_request_ids = {} --- @type table<integer,integer>
@ -1208,7 +1196,7 @@ end
function lsp.buf_get_clients(bufnr) function lsp.buf_get_clients(bufnr)
vim.deprecate('vim.lsp.buf_get_clients()', 'vim.lsp.get_clients()', '0.12') vim.deprecate('vim.lsp.buf_get_clients()', 'vim.lsp.get_clients()', '0.12')
local result = {} --- @type table<integer,vim.lsp.Client> local result = {} --- @type table<integer,vim.lsp.Client>
for _, client in ipairs(lsp.get_clients({ bufnr = resolve_bufnr(bufnr) })) do for _, client in ipairs(lsp.get_clients({ bufnr = vim._resolve_bufnr(bufnr) })) do
result[client.id] = client result[client.id] = client
end end
return result return result
@ -1262,7 +1250,7 @@ function lsp.for_each_buffer_client(bufnr, fn)
'lsp.get_clients({ bufnr = bufnr }) with regular loop', 'lsp.get_clients({ bufnr = bufnr }) with regular loop',
'0.12' '0.12'
) )
bufnr = resolve_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do
fn(client, client.id, bufnr) fn(client, client.id, bufnr)

View File

@ -519,7 +519,7 @@ end
--- @param opts? vim.lsp.buf.format.Opts --- @param opts? vim.lsp.buf.format.Opts
function M.format(opts) function M.format(opts)
opts = opts or {} opts = opts or {}
local bufnr = opts.bufnr or api.nvim_get_current_buf() local bufnr = vim._resolve_bufnr(opts.bufnr)
local mode = api.nvim_get_mode().mode local mode = api.nvim_get_mode().mode
local range = opts.range local range = opts.range
-- Try to use visual selection if no range is given -- Try to use visual selection if no range is given
@ -617,7 +617,7 @@ end
---@param opts? vim.lsp.buf.rename.Opts Additional options: ---@param opts? vim.lsp.buf.rename.Opts Additional options:
function M.rename(new_name, opts) function M.rename(new_name, opts)
opts = opts or {} opts = opts or {}
local bufnr = opts.bufnr or api.nvim_get_current_buf() local bufnr = vim._resolve_bufnr(opts.bufnr)
local clients = lsp.get_clients({ local clients = lsp.get_clients({
bufnr = bufnr, bufnr = bufnr,
name = opts.name, name = opts.name,

View File

@ -604,18 +604,6 @@ function Client:_resolve_handler(method)
return self.handlers[method] or lsp.handlers[method] return self.handlers[method] or lsp.handlers[method]
end end
--- Returns the buffer number for the given {bufnr}.
---
--- @param bufnr integer? Buffer number to resolve. Defaults to current buffer
--- @return integer bufnr
local function resolve_bufnr(bufnr)
validate('bufnr', bufnr, 'number', true)
if bufnr == nil or bufnr == 0 then
return api.nvim_get_current_buf()
end
return bufnr
end
--- Sends a request to the server. --- Sends a request to the server.
--- ---
--- This is a thin wrapper around {client.rpc.request} with some additional --- This is a thin wrapper around {client.rpc.request} with some additional
@ -640,7 +628,7 @@ function Client:request(method, params, handler, bufnr)
end end
-- Ensure pending didChange notifications are sent so that the server doesn't operate on a stale state -- Ensure pending didChange notifications are sent so that the server doesn't operate on a stale state
changetracking.flush(self, bufnr) changetracking.flush(self, bufnr)
bufnr = resolve_bufnr(bufnr) bufnr = vim._resolve_bufnr(bufnr)
local version = lsp.util.buf_versions[bufnr] local version = lsp.util.buf_versions[bufnr]
log.debug(self._log_prefix, 'client.request', self.id, method, params, handler, bufnr) log.debug(self._log_prefix, 'client.request', self.id, method, params, handler, bufnr)
local success, request_id = self.rpc.request(method, params, function(err, result) local success, request_id = self.rpc.request(method, params, function(err, result)
@ -891,7 +879,7 @@ end
--- @param bufnr? integer --- @param bufnr? integer
--- @return lsp.Registration? --- @return lsp.Registration?
function Client:_get_registration(method, bufnr) function Client:_get_registration(method, bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf() bufnr = vim._resolve_bufnr(bufnr)
for _, reg in ipairs(self.registrations[method] or {}) do for _, reg in ipairs(self.registrations[method] or {}) do
if not reg.registerOptions or not reg.registerOptions.documentSelector then if not reg.registerOptions or not reg.registerOptions.documentSelector then
return reg return reg
@ -928,7 +916,7 @@ end
--- @param handler? lsp.Handler only called if a server command --- @param handler? lsp.Handler only called if a server command
function Client:exec_cmd(command, context, handler) function Client:exec_cmd(command, context, handler)
context = vim.deepcopy(context or {}, true) --[[@as lsp.HandlerContext]] context = vim.deepcopy(context or {}, true) --[[@as lsp.HandlerContext]]
context.bufnr = context.bufnr or api.nvim_get_current_buf() context.bufnr = vim._resolve_bufnr(context.bufnr)
context.client_id = self.id context.client_id = self.id
local cmdname = command.command local cmdname = command.command
local fn = self.commands[cmdname] or lsp.commands[cmdname] local fn = self.commands[cmdname] or lsp.commands[cmdname]

View File

@ -104,16 +104,12 @@ function M.run()
end end
end end
local function resolve_bufnr(bufnr)
return bufnr == 0 and api.nvim_get_current_buf() or bufnr
end
--- Clear the lenses --- Clear the lenses
--- ---
---@param client_id integer|nil filter by client_id. All clients if nil ---@param client_id integer|nil filter by client_id. All clients if nil
---@param bufnr integer|nil filter by buffer. All buffers if nil, 0 for current buffer ---@param bufnr integer|nil filter by buffer. All buffers if nil, 0 for current buffer
function M.clear(client_id, bufnr) function M.clear(client_id, bufnr)
bufnr = bufnr and resolve_bufnr(bufnr) bufnr = bufnr and vim._resolve_bufnr(bufnr)
local buffers = bufnr and { bufnr } local buffers = bufnr and { bufnr }
or vim.tbl_filter(api.nvim_buf_is_loaded, api.nvim_list_bufs()) or vim.tbl_filter(api.nvim_buf_is_loaded, api.nvim_list_bufs())
for _, iter_bufnr in pairs(buffers) do for _, iter_bufnr in pairs(buffers) do
@ -296,7 +292,7 @@ end
--- @param opts? vim.lsp.codelens.refresh.Opts Optional fields --- @param opts? vim.lsp.codelens.refresh.Opts Optional fields
function M.refresh(opts) function M.refresh(opts)
opts = opts or {} opts = opts or {}
local bufnr = opts.bufnr and resolve_bufnr(opts.bufnr) local bufnr = opts.bufnr and vim._resolve_bufnr(opts.bufnr)
local buffers = bufnr and { bufnr } local buffers = bufnr and { bufnr }
or vim.tbl_filter(api.nvim_buf_is_loaded, api.nvim_list_bufs()) or vim.tbl_filter(api.nvim_buf_is_loaded, api.nvim_list_bufs())

View File

@ -716,7 +716,7 @@ end
--- @param bufnr integer Buffer handle, or 0 for the current buffer --- @param bufnr integer Buffer handle, or 0 for the current buffer
--- @param opts? vim.lsp.completion.BufferOpts --- @param opts? vim.lsp.completion.BufferOpts
function M.enable(enable, client_id, bufnr, opts) function M.enable(enable, client_id, bufnr, opts)
bufnr = (bufnr == 0 and api.nvim_get_current_buf()) or bufnr bufnr = vim._resolve_bufnr(bufnr)
if enable then if enable then
enable_completions(client_id, bufnr, opts or {}) enable_completions(client_id, bufnr, opts or {})

View File

@ -356,9 +356,7 @@ end
---@param bufnr (integer) Buffer handle, or 0 for current ---@param bufnr (integer) Buffer handle, or 0 for current
---@private ---@private
function M._enable(bufnr) function M._enable(bufnr)
if bufnr == nil or bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
if not bufstates[bufnr] then if not bufstates[bufnr] then
bufstates[bufnr] = { enabled = true } bufstates[bufnr] = { enabled = true }

View File

@ -149,8 +149,8 @@ function M.get(filter)
vim.list_extend(hints, M.get(vim.tbl_extend('keep', { bufnr = buf }, filter))) vim.list_extend(hints, M.get(vim.tbl_extend('keep', { bufnr = buf }, filter)))
end, vim.api.nvim_list_bufs()) end, vim.api.nvim_list_bufs())
return hints return hints
elseif bufnr == 0 then else
bufnr = api.nvim_get_current_buf() bufnr = vim._resolve_bufnr(bufnr)
end end
local bufstate = bufstates[bufnr] local bufstate = bufstates[bufnr]
@ -203,9 +203,7 @@ end
--- Clear inlay hints --- Clear inlay hints
---@param bufnr (integer) Buffer handle, or 0 for current ---@param bufnr (integer) Buffer handle, or 0 for current
local function clear(bufnr) local function clear(bufnr)
if bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
local bufstate = bufstates[bufnr] local bufstate = bufstates[bufnr]
local client_lens = (bufstate or {}).client_hints or {} local client_lens = (bufstate or {}).client_hints or {}
local client_ids = vim.tbl_keys(client_lens) --- @type integer[] local client_ids = vim.tbl_keys(client_lens) --- @type integer[]
@ -221,9 +219,7 @@ end
--- Disable inlay hints for a buffer --- Disable inlay hints for a buffer
---@param bufnr (integer) Buffer handle, or 0 for current ---@param bufnr (integer) Buffer handle, or 0 for current
local function _disable(bufnr) local function _disable(bufnr)
if bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
clear(bufnr) clear(bufnr)
bufstates[bufnr] = nil bufstates[bufnr] = nil
bufstates[bufnr].enabled = false bufstates[bufnr].enabled = false
@ -242,9 +238,7 @@ end
--- Enable inlay hints for a buffer --- Enable inlay hints for a buffer
---@param bufnr (integer) Buffer handle, or 0 for current ---@param bufnr (integer) Buffer handle, or 0 for current
local function _enable(bufnr) local function _enable(bufnr)
if bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
bufstates[bufnr] = nil bufstates[bufnr] = nil
bufstates[bufnr].enabled = true bufstates[bufnr].enabled = true
_refresh(bufnr) _refresh(bufnr)
@ -371,13 +365,10 @@ function M.is_enabled(filter)
filter = filter or {} filter = filter or {}
local bufnr = filter.bufnr local bufnr = filter.bufnr
vim.validate('bufnr', bufnr, 'number', true)
if bufnr == nil then if bufnr == nil then
return globalstate.enabled return globalstate.enabled
elseif bufnr == 0 then
bufnr = api.nvim_get_current_buf()
end end
return bufstates[bufnr].enabled return bufstates[vim._resolve_bufnr(bufnr)].enabled
end end
--- Optional filters |kwargs|, or `nil` for all. --- Optional filters |kwargs|, or `nil` for all.

View File

@ -600,9 +600,7 @@ function M.start(bufnr, client_id, opts)
vim.validate('bufnr', bufnr, 'number') vim.validate('bufnr', bufnr, 'number')
vim.validate('client_id', client_id, 'number') vim.validate('client_id', client_id, 'number')
if bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
opts = opts or {} opts = opts or {}
assert( assert(
@ -655,9 +653,7 @@ function M.stop(bufnr, client_id)
vim.validate('bufnr', bufnr, 'number') vim.validate('bufnr', bufnr, 'number')
vim.validate('client_id', client_id, 'number') vim.validate('client_id', client_id, 'number')
if bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
local highlighter = STHighlighter.active[bufnr] local highlighter = STHighlighter.active[bufnr]
if not highlighter then if not highlighter then
@ -691,9 +687,7 @@ end
--- - modifiers (table) token modifiers as a set. E.g., { static = true, readonly = true } --- - modifiers (table) token modifiers as a set. E.g., { static = true, readonly = true }
--- - client_id (integer) --- - client_id (integer)
function M.get_at_pos(bufnr, row, col) function M.get_at_pos(bufnr, row, col)
if bufnr == nil or bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
local highlighter = STHighlighter.active[bufnr] local highlighter = STHighlighter.active[bufnr]
if not highlighter then if not highlighter then
@ -739,8 +733,7 @@ function M.force_refresh(bufnr)
vim.validate('bufnr', bufnr, 'number', true) vim.validate('bufnr', bufnr, 'number', true)
local buffers = bufnr == nil and vim.tbl_keys(STHighlighter.active) local buffers = bufnr == nil and vim.tbl_keys(STHighlighter.active)
or bufnr == 0 and { api.nvim_get_current_buf() } or { vim._resolve_bufnr(bufnr) }
or { bufnr }
for _, buffer in ipairs(buffers) do for _, buffer in ipairs(buffers) do
local highlighter = STHighlighter.active[buffer] local highlighter = STHighlighter.active[buffer]
@ -770,9 +763,7 @@ end
---@param hl_group (string) Highlight group name ---@param hl_group (string) Highlight group name
---@param opts? vim.lsp.semantic_tokens.highlight_token.Opts Optional parameters: ---@param opts? vim.lsp.semantic_tokens.highlight_token.Opts Optional parameters:
function M.highlight_token(token, bufnr, client_id, hl_group, opts) function M.highlight_token(token, bufnr, client_id, hl_group, opts)
if bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
local highlighter = STHighlighter.active[bufnr] local highlighter = STHighlighter.active[bufnr]
if not highlighter then if not highlighter then
return return

View File

@ -192,9 +192,7 @@ local function get_lines(bufnr, rows)
rows = type(rows) == 'table' and rows or { rows } rows = type(rows) == 'table' and rows or { rows }
-- This is needed for bufload and bufloaded -- This is needed for bufload and bufloaded
if bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
local function buf_lines() local function buf_lines()
local lines = {} --- @type table<integer,string> local lines = {} --- @type table<integer,string>
@ -1976,7 +1974,7 @@ function M.make_given_range_params(start_pos, end_pos, bufnr, position_encoding)
validate('start_pos', start_pos, 'table', true) validate('start_pos', start_pos, 'table', true)
validate('end_pos', end_pos, 'table', true) validate('end_pos', end_pos, 'table', true)
validate('position_encoding', position_encoding, 'string', true) validate('position_encoding', position_encoding, 'string', true)
bufnr = bufnr or api.nvim_get_current_buf() bufnr = vim._resolve_bufnr(bufnr)
if position_encoding == nil then if position_encoding == nil then
vim.notify_once( vim.notify_once(
'position_encoding param is required in vim.lsp.util.make_given_range_params. Defaulting to position encoding of the first client.', 'position_encoding param is required in vim.lsp.util.make_given_range_params. Defaulting to position encoding of the first client.',
@ -2143,10 +2141,7 @@ end
---@param opts? vim.lsp.util._refresh.Opts Options table ---@param opts? vim.lsp.util._refresh.Opts Options table
function M._refresh(method, opts) function M._refresh(method, opts)
opts = opts or {} opts = opts or {}
local bufnr = opts.bufnr local bufnr = vim._resolve_bufnr(opts.bufnr)
if bufnr == nil or bufnr == 0 then
bufnr = api.nvim_get_current_buf()
end
local clients = vim.lsp.get_clients({ bufnr = bufnr, method = method, id = opts.client_id }) local clients = vim.lsp.get_clients({ bufnr = bufnr, method = method, id = opts.client_id })

View File

@ -1399,4 +1399,14 @@ function vim._with(context, f)
return vim._with_c(context, callback) return vim._with_c(context, callback)
end end
--- @param bufnr? integer
--- @return integer
function vim._resolve_bufnr(bufnr)
if bufnr == nil or bufnr == 0 then
return vim.api.nvim_get_current_buf()
end
vim.validate('bufnr', bufnr, 'number')
return bufnr
end
return vim return vim

View File

@ -32,9 +32,7 @@ M.minimum_language_version = vim._ts_get_minimum_language_version()
--- ---
---@return vim.treesitter.LanguageTree object to use for parsing ---@return vim.treesitter.LanguageTree object to use for parsing
function M._create_parser(bufnr, lang, opts) function M._create_parser(bufnr, lang, opts)
if bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = vim.api.nvim_get_current_buf()
end
vim.fn.bufload(bufnr) vim.fn.bufload(bufnr)
@ -90,9 +88,7 @@ function M.get_parser(bufnr, lang, opts)
opts = opts or {} opts = opts or {}
local should_error = opts.error == nil or opts.error local should_error = opts.error == nil or opts.error
if bufnr == nil or bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
if not valid_lang(lang) then if not valid_lang(lang) then
lang = M.language.get_lang(vim.bo[bufnr].filetype) lang = M.language.get_lang(vim.bo[bufnr].filetype)
@ -258,9 +254,7 @@ end
--- ---
---@return {capture: string, lang: string, metadata: vim.treesitter.query.TSMetadata}[] ---@return {capture: string, lang: string, metadata: vim.treesitter.query.TSMetadata}[]
function M.get_captures_at_pos(bufnr, row, col) function M.get_captures_at_pos(bufnr, row, col)
if bufnr == 0 then bufnr = vim._resolve_bufnr(bufnr)
bufnr = api.nvim_get_current_buf()
end
local buf_highlighter = M.highlighter.active[bufnr] local buf_highlighter = M.highlighter.active[bufnr]
if not buf_highlighter then if not buf_highlighter then
@ -361,11 +355,7 @@ end
function M.get_node(opts) function M.get_node(opts)
opts = opts or {} opts = opts or {}
local bufnr = opts.bufnr local bufnr = vim._resolve_bufnr(opts.bufnr)
if not bufnr or bufnr == 0 then
bufnr = api.nvim_get_current_buf()
end
local row, col --- @type integer, integer local row, col --- @type integer, integer
if opts.pos then if opts.pos then
@ -417,7 +407,7 @@ end
---@param bufnr (integer|nil) Buffer to be highlighted (default: current buffer) ---@param bufnr (integer|nil) Buffer to be highlighted (default: current buffer)
---@param lang (string|nil) Language of the parser (default: from buffer filetype) ---@param lang (string|nil) Language of the parser (default: from buffer filetype)
function M.start(bufnr, lang) function M.start(bufnr, lang)
bufnr = bufnr or api.nvim_get_current_buf() bufnr = vim._resolve_bufnr(bufnr)
local parser = assert(M.get_parser(bufnr, lang, { error = false })) local parser = assert(M.get_parser(bufnr, lang, { error = false }))
M.highlighter.new(parser) M.highlighter.new(parser)
end end
@ -426,7 +416,7 @@ end
--- ---
---@param bufnr (integer|nil) Buffer to stop highlighting (default: current buffer) ---@param bufnr (integer|nil) Buffer to stop highlighting (default: current buffer)
function M.stop(bufnr) function M.stop(bufnr)
bufnr = (bufnr and bufnr ~= 0) and bufnr or api.nvim_get_current_buf() bufnr = vim._resolve_bufnr(bufnr)
if M.highlighter.active[bufnr] then if M.highlighter.active[bufnr] then
M.highlighter.active[bufnr]:destroy() M.highlighter.active[bufnr]:destroy()