refactor: integer functions, optimize asserts #34112

refactor(lua): add integer coercion helpers

Add vim._tointeger() and vim._ensure_integer(), including optional base
support, and switch integer-only tonumber()/assert call sites in the Lua
runtime to use them.

This also cleans up related integer parsing in LSP, health, loader, URI,
tohtml, and Treesitter code.

supported by AI
This commit is contained in:
Lewis Russell
2026-03-12 15:04:05 +00:00
committed by GitHub
parent 2fe07cc965
commit ce1154048b
30 changed files with 144 additions and 95 deletions

View File

@@ -32,8 +32,8 @@ local M = {}
--- @field lines string[] snapshot of buffer lines from last didChange
--- @field lines_tmp string[]
--- @field pending_changes table[] List of debounced changes in incremental sync mode
--- @field timer uv.uv_timer_t? uv_timer
--- @field last_flush nil|number uv.hrtime of the last flush/didChange-notification
--- @field timer? uv.uv_timer_t uv_timer
--- @field last_flush? number uv.hrtime of the last flush/didChange-notification
--- @field needs_flush boolean true if buffer updates haven't been sent to clients/servers yet
--- @field refs integer how many clients are using this group
---
@@ -68,7 +68,7 @@ local function get_group(client)
local change_capability = vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'change')
local sync_kind = change_capability or protocol.TextDocumentSyncKind.None
if not allow_inc_sync and change_capability == protocol.TextDocumentSyncKind.Incremental then
sync_kind = protocol.TextDocumentSyncKind.Full --[[@as integer]]
sync_kind = protocol.TextDocumentSyncKind.Full
end
return {
sync_kind = sync_kind,

View File

@@ -174,9 +174,7 @@ local G = P({
--- @param input string
--- @return vim.snippet.Node<vim.snippet.SnippetData>
function M.parse(input)
local snippet = G:match(input)
assert(snippet, 'snippet parsing failed')
return snippet --- @type vim.snippet.Node<vim.snippet.SnippetData>
return assert(G:match(input), 'snippet parsing failed')
end
return M

View File

@@ -713,10 +713,8 @@ end
--- @see |vim.lsp.buf_request_all()|
function Client:request(method, params, handler, bufnr)
if not handler then
handler = assert(
self:_resolve_handler(method),
string.format('not found: %q request handler for client %q.', method, self.name)
)
handler = self:_resolve_handler(method)
or error(('not found: %q request handler for client %q.'):format(method, self.name))
end
-- Ensure pending didChange notifications are sent so that the server doesn't operate on a stale state
changetracking.flush(self, bufnr)

View File

@@ -324,7 +324,13 @@ local function generate_kind(item)
-- extract hex from RGB format
local r, g, b = doc:match('rgb%((%d+)%s*,?%s*(%d+)%s*,?%s*(%d+)%)')
local hex = r and string.format('%02x%02x%02x', tonumber(r), tonumber(g), tonumber(b))
local hex = r
and string.format(
'%02x%02x%02x',
vim._ensure_integer(r),
vim._ensure_integer(g),
vim._ensure_integer(b)
)
or doc:match('#?([%da-fA-F]+)')
if not hex then
@@ -696,7 +702,7 @@ function CompletionResolver:is_valid()
return vim.api.nvim_buf_is_valid(self.bufnr)
and vim.api.nvim_get_current_buf() == self.bufnr
and vim.startswith(vim.api.nvim_get_mode().mode, 'i')
and tonumber(vim.fn.pumvisible()) == 1
and vim.fn.pumvisible() ~= 0
and (vim.tbl_get(cmp_info, 'completed', 'word') or '') == self.word,
cmp_info
end
@@ -916,7 +922,7 @@ local function trigger(bufnr, clients, ctx)
reset_timer()
Context:cancel_pending()
if tonumber(vim.fn.pumvisible()) == 1 and not Context.isIncomplete then
if vim.fn.pumvisible() ~= 0 and not Context.isIncomplete then
return
end
@@ -1018,7 +1024,7 @@ end
--- @param handle vim.lsp.completion.BufHandle
local function on_insert_char_pre(handle)
if tonumber(vim.fn.pumvisible()) == 1 then
if vim.fn.pumvisible() ~= 0 then
if Context.isIncomplete then
reset_timer()
@@ -1143,8 +1149,7 @@ local function enable_completions(client_id, bufnr, opts)
end
if not buf_handle.clients[client_id] then
local client = lsp.get_client_by_id(client_id)
assert(client, 'invalid client ID')
local client = assert(lsp.get_client_by_id(client_id), 'invalid client ID')
-- Add the new client to the buffer's clients.
buf_handle.clients[client_id] = client
@@ -1245,7 +1250,6 @@ end
--- - findstart=1: list of matches (actually just calls |complete()|)
function M._omnifunc(findstart, base)
lsp.log.debug('omnifunc.findstart', { findstart = findstart, base = base })
assert(base) -- silence luals
local bufnr = api.nvim_get_current_buf()
local clients = lsp.get_clients({ bufnr = bufnr, method = 'textDocument/completion' })
local remaining = #clients

View File

@@ -47,10 +47,9 @@ local function get_contrast_color(color)
if not (r_s and g_s and b_s) then
error('Invalid color format: ' .. color)
end
local r, g, b = tonumber(r_s, 16), tonumber(g_s, 16), tonumber(b_s, 16)
if not (r and g and b) then
error('Invalid color format: ' .. color)
end
local r = vim._ensure_integer(r_s, 16)
local g = vim._ensure_integer(g_s, 16)
local b = vim._ensure_integer(b_s, 16)
-- Source: https://www.w3.org/TR/WCAG21/#dfn-relative-luminance
-- Using power 2.2 is a close approximation to full piecewise transform

View File

@@ -137,8 +137,7 @@ local function check_watcher()
return
end
local watchfunc = vim.lsp._watchfiles._watchfunc
assert(watchfunc)
local watchfunc = assert(vim.lsp._watchfiles._watchfunc)
local watchfunc_name --- @type string
if watchfunc == vim._watch.watch then
watchfunc_name = 'libuv-watch'

View File

@@ -56,7 +56,10 @@ local function get_content_length(header)
elseif state == 'value' then
if c == 13 and header:byte(i + 1) == 10 then -- must end with \r\n
local value = buf:get()
return assert(digit and tonumber(value), 'value of Content-Length is not number: ' .. value)
if digit then
return vim._ensure_integer(value)
end
error('value of Content-Length is not number: ' .. value)
else
buf:put(string.char(c))
end
@@ -429,7 +432,7 @@ function Client:handle_body(body)
)
then
-- We sent a number, so we expect a number.
local result_id = assert(tonumber(decoded.id), 'response id must be a number') --[[@as integer]]
local result_id = vim._ensure_integer(decoded.id)
-- Notify the user that a response was received for the request
local notify_reply_callback = self.notify_reply_callbacks[result_id]

View File

@@ -636,8 +636,7 @@ function M.rename(old_fname, new_fname, opts)
local newdir = vim.fs.dirname(new_fname)
vim.fn.mkdir(newdir, 'p')
local ok, err = os.rename(old_fname_full, new_fname)
assert(ok, err)
assert(os.rename(old_fname_full, new_fname))
local old_undofile = vim.fn.undofile(old_fname_full)
if uv.fs_stat(old_undofile) ~= nil then
@@ -1745,10 +1744,10 @@ function M.open_floating_preview(contents, syntax, opts)
api.nvim_create_autocmd('WinClosed', {
group = api.nvim_create_augroup('nvim.closing_floating_preview', { clear = true }),
callback = function(args)
local winid = tonumber(args.match)
local ok, preview_bufnr = pcall(api.nvim_win_get_var, winid, 'lsp_floating_bufnr')
local winid = vim._tointeger(args.match)
local preview_bufnr = vim.w[winid].lsp_floating_bufnr
if
ok
preview_bufnr
and api.nvim_buf_is_valid(preview_bufnr)
and winid == vim.b[preview_bufnr].lsp_floating_preview
then