refactor(pos,range): extract vim.pos._util

Problem:
- To share logic, creating a `vim.Range` currently creates two `vim.Pos` values
  as intermediates, which causes unnecessary table allocations.
- `pos.lua` and `range.lua` contain some overlapping logic.

Solution:
Add `vim.pos._util`, a module for handling
positions represented directly by `row` and `col`.
This commit is contained in:
Yi Ming
2026-05-20 16:02:57 +08:00
parent 8d1233a144
commit b1c1f32089
7 changed files with 326 additions and 325 deletions

View File

@@ -92,36 +92,6 @@ describe('vim.pos', function()
}, pos)
end)
it("converts between vim.Pos and extmark on buffer's last line", function()
local buf = exec_lua(function()
return vim.api.nvim_get_current_buf()
end)
insert('Some text')
local extmark_pos = {
exec_lua(function()
local pos = vim.pos(buf, 1, 0)
return pos:to_extmark()
end),
}
eq({ 0, 9 }, extmark_pos)
local pos = exec_lua(function()
return vim.pos.extmark(buf, extmark_pos[1], extmark_pos[2])
end)
eq({ 0, 9, buf }, pos)
local extmark_pos2 = {
exec_lua(function()
local pos2 = vim.pos(buf, 0, 9)
return pos2:to_extmark()
end),
}
eq({ 0, 9 }, extmark_pos2)
local pos2 = exec_lua(function()
return vim.pos.extmark(buf, extmark_pos2[1], extmark_pos2[2])
end)
eq({ 0, 9, buf }, pos2)
end)
it('converts between vim.Pos and buffer offset', function()
local buf = exec_lua(function()
return vim.api.nvim_get_current_buf()