feat(pos): create a cursor position by using the current of a window

Problem:
`vim.pos.cursor(vim.api.nvim_get_current_buf(win), vim.api.nvim_win_get_cursor(win))`
is too verbose to create a cursor position of a window,
but it is a common use case.

Solution:
Overload `vim.pos.cursor()`, so that it accepts `win` as an argument when `pos` is omitted.
This commit is contained in:
Yi Ming
2026-06-02 20:06:08 +08:00
parent 3f37b230af
commit 2bd13177b8
6 changed files with 47 additions and 12 deletions

View File

@@ -30,6 +30,15 @@ describe('vim.pos', function()
eq(buf, pos[3])
end)
it('creates a position from the window cursor', function()
local pos, buf = exec_lua(function()
vim.api.nvim_buf_set_lines(0, 0, -1, true, { 'first', 'second' })
vim.api.nvim_win_set_cursor(0, { 2, 3 })
return vim.pos.cursor(0), vim.api.nvim_get_current_buf()
end)
eq({ 1, 3, buf }, pos)
end)
it('comparisons by overloaded operators', function()
local buf = exec_lua(function()
return vim.api.nvim_create_buf(false, true)