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()

View File

@@ -92,6 +92,48 @@ describe('vim.range', function()
eq({ 1, 0, 1, 0 }, mark_range)
end)
it("converts between vim.Range 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_range = {
exec_lua(function()
local range = vim.range(buf, 0, 0, 1, 0)
return range:to_extmark()
end),
}
eq({ 0, 0, 0, 9 }, extmark_range)
local range = exec_lua(function()
return vim.range.extmark(
buf,
extmark_range[1],
extmark_range[2],
extmark_range[3],
extmark_range[4]
)
end)
eq({ 0, 0, 0, 9, buf }, range)
local extmark_range2 = {
exec_lua(function()
local range2 = vim.range(buf, 0, 0, 0, 9)
return range2:to_extmark()
end),
}
eq({ 0, 0, 0, 9 }, extmark_range2)
local range2 = exec_lua(function()
return vim.range.extmark(
buf,
extmark_range2[1],
extmark_range2[2],
extmark_range2[3],
extmark_range2[4]
)
end)
eq({ 0, 0, 0, 9, buf }, range2)
end)
it('checks whether a range contains a position', function()
eq(
true,