summaryrefslogtreecommitdiffstatshomepage
path: root/runtime/lua/vim/_core/editor.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2026-03-12 15:04:05 +0000
committerGitHub <noreply@github.com>2026-03-12 11:04:05 -0400
commitce1154048b11cdc402d0e5da352a3adca19afb56 (patch)
tree02516c918bd7b47e96b02eb83323b917ec4dd37d /runtime/lua/vim/_core/editor.lua
parent2fe07cc9658cfca6dcda1b9b37e5f80e38fd7b19 (diff)
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
Diffstat (limited to 'runtime/lua/vim/_core/editor.lua')
-rw-r--r--runtime/lua/vim/_core/editor.lua19
1 files changed, 9 insertions, 10 deletions
diff --git a/runtime/lua/vim/_core/editor.lua b/runtime/lua/vim/_core/editor.lua
index 680791c4d3..49adc6ff7e 100644
--- a/runtime/lua/vim/_core/editor.lua
+++ b/runtime/lua/vim/_core/editor.lua
@@ -1,3 +1,5 @@
+local tointeger = vim._tointeger
+
-- Nvim-Lua stdlib: the `vim` module (:help lua-stdlib)
--
@@ -74,7 +76,7 @@ function vim._os_proc_info(pid)
local ppid_string = assert(vim.system({ 'ps', '-p', pid, '-o', 'ppid=' }):wait().stdout)
-- Remove trailing whitespace.
name = vim.trim(name):gsub('^.*/', '')
- local ppid = tonumber(ppid_string) or -1
+ local ppid = tointeger(ppid_string) or -1
return {
name = name,
pid = pid,
@@ -95,12 +97,9 @@ function vim._os_proc_children(ppid)
elseif r.code ~= 0 then
error('command failed: ' .. vim.fn.string(cmd))
end
- local children = {}
+ local children = {} --- @type integer[]
for s in r.stdout:gmatch('%S+') do
- local i = tonumber(s)
- if i ~= nil then
- table.insert(children, i)
- end
+ children[#children + 1] = tointeger(s)
end
return children
end
@@ -476,17 +475,17 @@ 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 + tonumber(regtype:sub(2))
+ c2 = c1 + vim._ensure_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)
if c1 <= utflen then
- c1 = assert(tonumber(vim.str_byteindex(bufline, 'utf-32', c1)))
+ c1 = vim.str_byteindex(bufline, 'utf-32', c1)
else
c1 = #bufline + 1
end
if c2 <= utflen then
- c2 = assert(tonumber(vim.str_byteindex(bufline, 'utf-32', c2)))
+ c2 = vim.str_byteindex(bufline, 'utf-32', c2)
else
c2 = #bufline + 1
end
@@ -1264,7 +1263,7 @@ function vim.deprecate(name, alternative, version, plugin, backtrace)
-- Example: if removal `version` is 0.12 (soft-deprecated since 0.10-dev), show warnings
-- starting at 0.11, including 0.11-dev.
local major, minor = version:match('(%d+)%.(%d+)')
- major, minor = tonumber(major), tonumber(minor)
+ major, minor = tointeger(major), tointeger(minor)
local nvim_major = 0 --- Current Nvim major version.
-- We can't "subtract" from a major version, so: