refactor: rename _ensure_integer => _assert_integer

This commit is contained in:
Justin M. Keyes
2026-03-12 17:10:00 +01:00
parent 682c77805c
commit 017d8aa298
14 changed files with 24 additions and 24 deletions

View File

@@ -57,7 +57,7 @@ function M.apply_marks()
vim.b.tutor_extmarks = {}
for expct, _ in pairs(vim.b.tutor_metadata.expect) do
---@diagnostic disable-next-line: assign-type-mismatch
local lnum = vim._ensure_integer(expct)
local lnum = vim._assert_integer(expct)
vim.api.nvim_buf_set_extmark(0, tutor_hl_ns, lnum - 1, 0, {
line_hl_group = 'tutorExpect',
invalidate = true,

View File

@@ -228,7 +228,7 @@ local function cterm_to_hex(colorstr)
if colorstr:sub(1, 1) == '#' then
return colorstr
end
local color = vim._ensure_integer(colorstr)
local color = vim._assert_integer(colorstr)
assert(0 <= color and color <= 255)
if cterm_color_cache[color] then
return cterm_color_cache[color]
@@ -238,7 +238,7 @@ local function cterm_to_hex(colorstr)
cterm_color_cache[color] = hex
else
notify("Couldn't get terminal colors, using fallback")
local t_Co = vim._ensure_integer(vim.api.nvim_eval('&t_Co'))
local t_Co = vim._assert_integer(vim.api.nvim_eval('&t_Co'))
if t_Co <= 8 then
cterm_color_cache = cterm_8_to_hex
elseif t_Co == 88 then
@@ -782,7 +782,7 @@ local function styletable_statuscolumn(state)
end)
minwidth = minwidth + math.min(maxfold, max)
else
minwidth = minwidth + vim._ensure_integer(foldcolumn)
minwidth = minwidth + vim._assert_integer(foldcolumn)
end
end

View File

@@ -799,7 +799,7 @@ do
return nil
end
local max = vim._ensure_integer(string.rep('f', #c), 16)
local max = vim._assert_integer(string.rep('f', #c), 16)
return val / max
end

View File

@@ -478,7 +478,7 @@ function vim.region(bufnr, pos1, pos2, regtype, inclusive)
local c2 --- @type number
if regtype:byte() == 22 then -- block selection: take width from regtype
c1 = pos1[2]
c2 = c1 + vim._ensure_integer(regtype:sub(2))
c2 = c1 + vim._assert_integer(regtype:sub(2))
-- and adjust for non-ASCII characters
local bufline = vim.api.nvim_buf_get_lines(bufnr, l, l + 1, true)[1]
local utflen = vim.str_utfindex(bufline, 'utf-32', #bufline)

View File

@@ -1645,7 +1645,7 @@ end
--- @param x any Value to convert.
--- @param base? integer Numeric base passed to `tonumber()`.
--- @return integer integer Converted integer value.
function vim._ensure_integer(x, base)
function vim._assert_integer(x, base)
return vim._tointeger(x, base) or error(('Cannot convert %s to integer'):format(x))
end

View File

@@ -14,7 +14,7 @@ function M.check()
local version, backtraces, alternative = v[1], v[2], v[3]
local major, minor = version:match('(%d+)%.(%d+)')
major, minor = vim._ensure_integer(major), vim._ensure_integer(minor)
major, minor = vim._assert_integer(major), vim._assert_integer(minor)
local removal_version = string.format('nvim-%d.%d', major, minor)
local will_be_removed = vim.fn.has(removal_version) == 1 and 'was removed' or 'will be removed'

View File

@@ -2886,7 +2886,7 @@ function M.match(str, pat, groups, severity_map, defaults)
if field == 'severity' then
diagnostic[field] = severity_map[match]
elseif field == 'lnum' or field == 'end_lnum' or field == 'col' or field == 'end_col' then
diagnostic[field] = vim._ensure_integer(match) - 1
diagnostic[field] = vim._assert_integer(match) - 1
elseif field then
diagnostic[field] = match
end

View File

@@ -29,7 +29,7 @@ local function resolve_hash(hash)
else
local c = hash:match('^concat%-(%d+)')
if c then
hash = concat_hash(vim._ensure_integer(c))
hash = concat_hash(vim._assert_integer(c))
else
error('invalid value for hash: ' .. hash)
end

View File

@@ -327,9 +327,9 @@ local function generate_kind(item)
local hex = r
and string.format(
'%02x%02x%02x',
vim._ensure_integer(r),
vim._ensure_integer(g),
vim._ensure_integer(b)
vim._assert_integer(r),
vim._assert_integer(g),
vim._assert_integer(b)
)
or doc:match('#?([%da-fA-F]+)')

View File

@@ -47,9 +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 = vim._ensure_integer(r_s, 16)
local g = vim._ensure_integer(g_s, 16)
local b = vim._ensure_integer(b_s, 16)
local r = vim._assert_integer(r_s, 16)
local g = vim._assert_integer(g_s, 16)
local b = vim._assert_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

@@ -57,7 +57,7 @@ local function get_content_length(header)
if c == 13 and header:byte(i + 1) == 10 then -- must end with \r\n
local value = buf:get()
if digit then
return vim._ensure_integer(value)
return vim._assert_integer(value)
end
error('value of Content-Length is not number: ' .. value)
else
@@ -432,7 +432,7 @@ function Client:handle_body(body)
)
then
-- We sent a number, so we expect a number.
local result_id = vim._ensure_integer(decoded.id)
local result_id = vim._assert_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

@@ -86,8 +86,8 @@ local function get_error_entry(err, node)
local start_line, start_col = node:range()
local line_offset, col_offset, msg = err:gmatch('.-:%d+: Query error at (%d+):(%d+)%. ([^:]+)')() ---@type string, string, string
start_line, start_col =
start_line + vim._ensure_integer(line_offset) - 1,
start_col + vim._ensure_integer(col_offset) - 1
start_line + vim._assert_integer(line_offset) - 1,
start_col + vim._assert_integer(col_offset) - 1
local end_line, end_col = start_line, start_col
if msg:match('^Invalid syntax') or msg:match('^Impossible') then
-- Use the length of the underlined node

View File

@@ -25,7 +25,7 @@ local PATTERNS = {
---@param hex string
---@return string
local function hex_to_char(hex)
return schar(vim._ensure_integer(hex, 16))
return schar(vim._assert_integer(hex, 16))
end
---@param char string

View File

@@ -201,9 +201,9 @@ function M._version(version, strict) -- Adapted from https://github.com/folke/la
or (major and minor and patch and major ~= '' and minor ~= '' and patch ~= '')
then
return setmetatable({
major = vim._ensure_integer(major),
minor = minor == '' and 0 or vim._ensure_integer(minor),
patch = patch == '' and 0 or vim._ensure_integer(patch),
major = vim._assert_integer(major),
minor = minor == '' and 0 or vim._assert_integer(minor),
patch = patch == '' and 0 or vim._assert_integer(patch),
prerelease = prerel ~= '' and prerel or nil,
build = build ~= '' and build or nil,
}, Version)