aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/pos.lua
diff options
context:
space:
mode:
authorYi Ming <ofseed@foxmail.com>2026-06-02 20:06:08 +0800
committerYi Ming <ofseed@foxmail.com>2026-06-08 19:52:51 +0800
commit2bd13177b83a158f2e8a00e7aeac5ffe793e0923 (patch)
treec40c4fdffbffff6da44aec0d2d6286d7046c1076 /runtime/lua/vim/pos.lua
parent3f37b230af914906084fd21d1c630ecf3fb30862 (diff)
downloadrneovim-2bd13177b83a158f2e8a00e7aeac5ffe793e0923.tar.gz
rneovim-2bd13177b83a158f2e8a00e7aeac5ffe793e0923.tar.bz2
rneovim-2bd13177b83a158f2e8a00e7aeac5ffe793e0923.zip
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.
Diffstat (limited to 'runtime/lua/vim/pos.lua')
-rw-r--r--runtime/lua/vim/pos.lua30
1 files changed, 24 insertions, 6 deletions
diff --git a/runtime/lua/vim/pos.lua b/runtime/lua/vim/pos.lua
index 599bd3ab33..0cf5cc9e7c 100644
--- a/runtime/lua/vim/pos.lua
+++ b/runtime/lua/vim/pos.lua
@@ -165,20 +165,38 @@ end
--- Creates a new |vim.Pos| from cursor position (see |api-indexing|).
---
+--- If {pos} is omitted, the first argument is treated as {win} instead of {buf},
+--- and the current cursor position of {win} is used.
+---
--- Example:
--- ```lua
+--- local buf = vim.api.nvim_win_get_buf(0)
--- local cursor_pos = vim.api.nvim_win_get_cursor(0)
---- local pos = vim.pos.cursor(0, cursor_pos)
+--- local pos = vim.pos.cursor(buf, cursor_pos)
+--- -- This is equivalent:
+--- local pos = vim.pos.cursor(0)
--- ```
---@param buf integer
---@param pos [integer, integer] (lnum, col) tuple
---@return vim.Pos
+---@overload fun(win: integer): vim.Pos
function M.cursor(buf, pos)
- validate('buf', buf, 'number')
- validate('pos', pos, 'table')
-
- if buf == 0 then
- buf = api.nvim_get_current_buf()
+ validate('pos', pos, 'table', true)
+
+ if pos then
+ validate('buf', buf, 'number')
+ if buf == 0 then
+ buf = api.nvim_get_current_buf()
+ end
+ else
+ local win = buf
+ validate('win', win, 'number')
+ if win == 0 then
+ win = api.nvim_get_current_win()
+ end
+
+ buf = api.nvim_win_get_buf(win)
+ pos = api.nvim_win_get_cursor(win)
end
return M.new(buf, util.from_mark(pos[1], pos[2]))