From 017d8aa298254f887a2df1983039f3d678d3ed1a Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 12 Mar 2026 17:10:00 +0100 Subject: [PATCH] refactor: rename _ensure_integer => _assert_integer --- runtime/lua/nvim/tutor.lua | 2 +- runtime/lua/tohtml.lua | 6 +++--- runtime/lua/vim/_core/defaults.lua | 2 +- runtime/lua/vim/_core/editor.lua | 2 +- runtime/lua/vim/_core/shared.lua | 2 +- runtime/lua/vim/deprecated/health.lua | 2 +- runtime/lua/vim/diagnostic.lua | 2 +- runtime/lua/vim/func/_memoize.lua | 2 +- runtime/lua/vim/lsp/completion.lua | 6 +++--- runtime/lua/vim/lsp/document_color.lua | 6 +++--- runtime/lua/vim/lsp/rpc.lua | 4 ++-- runtime/lua/vim/treesitter/_query_linter.lua | 4 ++-- runtime/lua/vim/uri.lua | 2 +- runtime/lua/vim/version.lua | 6 +++--- 14 files changed, 24 insertions(+), 24 deletions(-) diff --git a/runtime/lua/nvim/tutor.lua b/runtime/lua/nvim/tutor.lua index 53a417f366..97bfbbb2d0 100644 --- a/runtime/lua/nvim/tutor.lua +++ b/runtime/lua/nvim/tutor.lua @@ -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, diff --git a/runtime/lua/tohtml.lua b/runtime/lua/tohtml.lua index 6d46a26a11..464ac1c6ad 100644 --- a/runtime/lua/tohtml.lua +++ b/runtime/lua/tohtml.lua @@ -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 diff --git a/runtime/lua/vim/_core/defaults.lua b/runtime/lua/vim/_core/defaults.lua index 10611be194..5ef11ee94a 100644 --- a/runtime/lua/vim/_core/defaults.lua +++ b/runtime/lua/vim/_core/defaults.lua @@ -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 diff --git a/runtime/lua/vim/_core/editor.lua b/runtime/lua/vim/_core/editor.lua index 50105c75e6..c967727336 100644 --- a/runtime/lua/vim/_core/editor.lua +++ b/runtime/lua/vim/_core/editor.lua @@ -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) diff --git a/runtime/lua/vim/_core/shared.lua b/runtime/lua/vim/_core/shared.lua index b898f0e45b..669e2a75fa 100644 --- a/runtime/lua/vim/_core/shared.lua +++ b/runtime/lua/vim/_core/shared.lua @@ -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 diff --git a/runtime/lua/vim/deprecated/health.lua b/runtime/lua/vim/deprecated/health.lua index 901a107d6f..297587b248 100644 --- a/runtime/lua/vim/deprecated/health.lua +++ b/runtime/lua/vim/deprecated/health.lua @@ -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' diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 999558eb9c..5b9dfaec7e 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -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 diff --git a/runtime/lua/vim/func/_memoize.lua b/runtime/lua/vim/func/_memoize.lua index c79ca596db..a68f76a0c4 100644 --- a/runtime/lua/vim/func/_memoize.lua +++ b/runtime/lua/vim/func/_memoize.lua @@ -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 diff --git a/runtime/lua/vim/lsp/completion.lua b/runtime/lua/vim/lsp/completion.lua index 66880ae8d1..a1925907e6 100644 --- a/runtime/lua/vim/lsp/completion.lua +++ b/runtime/lua/vim/lsp/completion.lua @@ -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]+)') diff --git a/runtime/lua/vim/lsp/document_color.lua b/runtime/lua/vim/lsp/document_color.lua index 1c6e3d40d0..4e210894cb 100644 --- a/runtime/lua/vim/lsp/document_color.lua +++ b/runtime/lua/vim/lsp/document_color.lua @@ -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 diff --git a/runtime/lua/vim/lsp/rpc.lua b/runtime/lua/vim/lsp/rpc.lua index d75b3ed18f..ed32fdabce 100644 --- a/runtime/lua/vim/lsp/rpc.lua +++ b/runtime/lua/vim/lsp/rpc.lua @@ -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] diff --git a/runtime/lua/vim/treesitter/_query_linter.lua b/runtime/lua/vim/treesitter/_query_linter.lua index 391d3487d1..ed82fda922 100644 --- a/runtime/lua/vim/treesitter/_query_linter.lua +++ b/runtime/lua/vim/treesitter/_query_linter.lua @@ -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 diff --git a/runtime/lua/vim/uri.lua b/runtime/lua/vim/uri.lua index 0e954bcb7a..95a5183d73 100644 --- a/runtime/lua/vim/uri.lua +++ b/runtime/lua/vim/uri.lua @@ -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 diff --git a/runtime/lua/vim/version.lua b/runtime/lua/vim/version.lua index 652d645325..e0e81c6861 100644 --- a/runtime/lua/vim/version.lua +++ b/runtime/lua/vim/version.lua @@ -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)