feat: extend vim.Pos, vim.Range #36397

Problem:
Using nested `vim.Pos` objects to represent each `vim.Range` object
requires 3 tables for each `vim.Range`, which may be undesirable in
performance critical code. Using key-value tables performs worse than
using array-like tables (lists).

Solution:
Use array-like indices for the internal fields of both `vim.Pos` and
`vim.Range` objects. Use a metatable to allow users to access them like
if they were key-value tables.

---

Problem:
The `vim.Pos` conversion interface for `extmark` indexing does not take
into account the difference in how a position on top of a newline is
represented in `vim.Pos` and `extmark`.
- `vim.Pos`: for a newline at the end of row `n`, `row` takes the value
  `n + 1` and `col` takes the value `0`.
- `extmark`: for a newline at the end of for `n`, `row` takes the value
  `n` and `col` takes the value `#row_text`.

Solution:
Handle this in the `extmark` interface.

---

Problem:
Not all `to_xxx` interfaces have wrapping objects like `to_lsp`.

Solution:
Return unwrapped values in `to_xxx` interfaces where it makes sense.
Accept unwrapped values in "from" interfaces where it makes sense.

---

Problem:
`start` and `end` positions have different semantics, so they can't be
compared. `vim.Range` relies on comparing the `end` and `start` of two
ranges to decide which one is greater, which doesn't work as expected
because this of the different semantics.

For example, for the ranges:

    local a = {
      start = { row = 0, col = 22, },
      end_ = { row = 0, col = 24, },
    }
    local b = {
      start = { row = 0, col = 17, },
      end_ = { row = 0, col = 22, },
    }

in this code:

    local foo, bar = "foo",  "bar"
    --               |---||-|
    --                 b  a

The range `b` is smaller than the range `a`, but the current
implementation compares `b._end` (`col = 22`) and `a.start` (`col = 22`)
and concludes that, since `b.col` is not smaller than `a.col`, `b`
should be greater than `a`.

Solution:
- Use a `to_inclusive_pos` to normalize end positions inside of
  `vim.Range` whenever a comparison between a start and an end position
  is necessary.
This commit is contained in:
Luis Calle
2026-03-29 10:22:40 -05:00
committed by GitHub
parent 527684c8dd
commit f3c2eb49ba
9 changed files with 494 additions and 135 deletions

View File

@@ -14,9 +14,9 @@ describe('vim.pos', function()
local pos = exec_lua(function()
return vim.pos(3, 5)
end)
eq(3, pos.row)
eq(5, pos.col)
eq(nil, pos.buf)
eq(3, pos[1])
eq(5, pos[2])
eq(nil, pos[3])
local buf = exec_lua(function()
return vim.api.nvim_create_buf(false, true)
@@ -24,9 +24,9 @@ describe('vim.pos', function()
pos = exec_lua(function()
return vim.pos(3, 5, { buf = buf })
end)
eq(3, pos.row)
eq(5, pos.col)
eq(buf, pos.buf)
eq(3, pos[1])
eq(5, pos[2])
eq(buf, pos[3])
end)
it('comparisons by overloaded operators', function()
@@ -82,9 +82,39 @@ describe('vim.pos', function()
return vim.pos.lsp(buf, lsp_pos, 'utf-16')
end)
eq({
buf = buf,
row = 0,
col = 36,
0,
36,
buf,
}, 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(1, 0, { buf = buf })
return pos:to_extmark()
end),
}
eq({ 0, 9 }, extmark_pos)
local pos = exec_lua(function()
return vim.pos.extmark(extmark_pos[1], extmark_pos[2], { buf = buf })
end)
eq({ 0, 9, buf }, pos)
local extmark_pos2 = {
exec_lua(function()
local pos2 = vim.pos(0, 9, { buf = buf })
return pos2:to_extmark()
end),
}
eq({ 0, 9 }, extmark_pos2)
local pos2 = exec_lua(function()
return vim.pos.extmark(extmark_pos2[1], extmark_pos2[2], { buf = buf })
end)
eq({ 0, 9, buf }, pos2)
end)
end)

View File

@@ -14,32 +14,29 @@ describe('vim.range', function()
local range = exec_lua(function()
return vim.range(3, 5, 4, 6)
end)
eq(3, range.start.row)
eq(5, range.start.col)
eq(4, range.end_.row)
eq(6, range.end_.col)
eq(nil, range.start.buf)
eq(nil, range.end_.buf)
eq(3, range[1])
eq(5, range[2])
eq(4, range[3])
eq(6, range[4])
eq(nil, range[5])
local buf = exec_lua(function()
return vim.api.nvim_create_buf(false, true)
end)
range = exec_lua(function()
return vim.range(3, 5, 4, 6, { buf = buf })
end)
eq(buf, range.start.buf)
eq(buf, range.end_.buf)
eq(buf, range[5])
end)
it('creates a range from two positions when optional fields are not matched', function()
local range = exec_lua(function()
return vim.range(vim.pos(3, 5), vim.pos(4, 6))
end)
eq(3, range.start.row)
eq(5, range.start.col)
eq(4, range.end_.row)
eq(6, range.end_.col)
eq(nil, range.start.buf)
eq(nil, range.end_.buf)
eq(3, range[1])
eq(5, range[2])
eq(4, range[3])
eq(6, range[4])
eq(nil, range[5])
local buf1 = exec_lua(function()
return vim.api.nvim_create_buf(false, true)
@@ -47,8 +44,7 @@ describe('vim.range', function()
range = exec_lua(function()
return vim.range(vim.pos(3, 5, { buf = buf1 }), vim.pos(4, 6, { buf = buf1 }))
end)
eq(buf1, range.start.buf)
eq(buf1, range.end_.buf)
eq(buf1, range[5])
local buf2 = exec_lua(function()
return vim.api.nvim_create_buf(false, true)
@@ -78,8 +74,11 @@ describe('vim.range', function()
return vim.range.lsp(buf, lsp_range, 'utf-16')
end)
eq({
start = { row = 0, col = 10, buf = buf },
end_ = { row = 0, col = 36, buf = buf },
0,
10,
0,
36,
buf,
}, range)
end)
@@ -91,4 +90,13 @@ describe('vim.range', function()
end)
)
end)
it('checks whether a range does not contain an empty range just outside it', function()
eq(
false,
exec_lua(function()
return vim.range(0, 0, 0, 4):has(vim.range(0, 0, 0, 0))
end)
)
end)
end)