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

@@ -51,7 +51,7 @@ local function ctx_is_valid(ctx)
return false
end
local cur_pos = vim.pos.cursor(bufnr, api.nvim_win_get_cursor(0))
local cur_pos = vim.pos.cursor(0)
local pos = vim.pos.lsp(bufnr, p, enc)
return cur_pos == pos
end
@@ -222,7 +222,7 @@ local function get_locations(method, context, opts)
)
end
local pos = opts.pos or vim.pos.cursor(0, api.nvim_win_get_cursor(0))
local pos = opts.pos or vim.pos.cursor(0)
local buf = pos.buf
local clients = lsp.get_clients({ method = method, bufnr = buf })

View File

@@ -490,7 +490,7 @@ function M.run(opts)
local winid = api.nvim_get_current_win()
local bufnr = api.nvim_win_get_buf(winid)
local pos = vim.pos.cursor(bufnr, api.nvim_win_get_cursor(winid))
local pos = vim.pos.cursor(winid)
local params = {
textDocument = vim.lsp.util.make_text_document_params(bufnr),
}

View File

@@ -1810,10 +1810,9 @@ end
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentPositionParams
function M.make_position_params(win, position_encoding)
win = win or 0
local buf = api.nvim_win_get_buf(win)
return {
textDocument = M.make_text_document_params(buf),
position = vim.pos.cursor(buf, api.nvim_win_get_cursor(win)):to_lsp(position_encoding),
textDocument = M.make_text_document_params(api.nvim_win_get_buf(win)),
position = vim.pos.cursor(win):to_lsp(position_encoding),
}
end

View File

@@ -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')
validate('pos', pos, 'table', true)
if buf == 0 then
buf = api.nvim_get_current_buf()
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]))