feat(vim.pos): accept buf=0 for current buf #39414

This commit is contained in:
Luis Calle
2026-04-26 05:34:18 -05:00
committed by Justin M. Keyes
parent 828a35b14f
commit 49efe692f3
5 changed files with 74 additions and 44 deletions

View File

@@ -16,8 +16,8 @@ local validate = vim.validate
---
--- Example:
--- ```lua
--- local pos1 = vim.pos(vim.api.nvim_get_current_buf(), 3, 5)
--- local pos2 = vim.pos(vim.api.nvim_get_current_buf(), 4, 0)
--- local pos1 = vim.pos(0, 3, 5)
--- local pos2 = vim.pos(0, 4, 0)
---
--- -- Operators are overloaded for comparing two `vim.Pos` objects.
--- if pos1 < pos2 then
@@ -65,6 +65,10 @@ function M.new(buf, row, col)
validate('row', row, 'number')
validate('col', col, 'number')
if buf == 0 then
buf = api.nvim_get_current_buf()
end
---@type vim.Pos
local self = setmetatable({
row,
@@ -123,8 +127,7 @@ end
---
--- Example:
--- ```lua
--- local buf = vim.api.nvim_get_current_buf()
--- local pos = vim.pos(buf, 3, 5)
--- local pos = vim.pos(0, 3, 5)
---
--- -- Convert to LSP position, you can call it in a method style.
--- local lsp_pos = pos:to_lsp('utf-16')
@@ -150,13 +153,12 @@ end
---
--- Example:
--- ```lua
--- local buf = vim.api.nvim_get_current_buf()
--- local lsp_pos = {
--- line = 3,
--- character = 5
--- }
---
--- local pos = vim.pos.lsp(buf, lsp_pos, 'utf-16')
--- local pos = vim.pos.lsp(0, lsp_pos, 'utf-16')
--- ```
---@param buf integer
---@param pos lsp.Position
@@ -166,6 +168,10 @@ function M.lsp(buf, pos, position_encoding)
validate('pos', pos, 'table')
validate('position_encoding', position_encoding, 'string')
if buf == 0 then
buf = api.nvim_get_current_buf()
end
local row, col = pos.line, pos.character
-- When on the first character,
-- we can ignore the difference between byte and character.
@@ -213,6 +219,10 @@ end
---@param row integer
---@param col integer
function M.extmark(buf, row, col)
if buf == 0 then
buf = api.nvim_get_current_buf()
end
return M.new(buf, row, col)
end

View File

@@ -18,13 +18,13 @@ local api = vim.api
---
--- Example:
--- ```lua
--- local pos1 = vim.pos(vim.api.nvim_get_current_buf(), 3, 5)
--- local pos2 = vim.pos(vim.api.nvim_get_current_buf(), 4, 0)
--- local pos1 = vim.pos(0, 3, 5)
--- local pos2 = vim.pos(0, 4, 0)
---
--- -- Create a range from two positions.
--- local range1 = vim.range(pos1, pos2)
--- -- Or create a range from four integers representing start and end positions.
--- local range2 = vim.range(vim.api.nvim_get_current_buf(), 3, 5, 4, 0)
--- local range2 = vim.range(0, 3, 5, 4, 0)
---
--- -- Because `vim.Range` is end exclusive, `range1` and `range2` both represent
--- -- a range starting at the row 3, column 5 and ending at where the row 3 ends
@@ -94,6 +94,10 @@ function M.new(...)
error('invalid parameters')
end
if buf == 0 then
buf = api.nvim_get_current_buf()
end
---@type vim.Range
local self = setmetatable({
start_row,
@@ -253,8 +257,7 @@ end
---
--- Example:
--- ```lua
--- local buf = vim.api.nvim_get_current_buf()
--- local range = vim.range(buf, 3, 5, 4, 0)
--- local range = vim.range(0, 3, 5, 4, 0)
---
--- -- Convert to LSP range, you can call it in a method style.
--- local lsp_range = range:to_lsp('utf-16')
@@ -277,13 +280,12 @@ end
---
--- Example:
--- ```lua
--- local buf = vim.api.nvim_get_current_buf()
--- local lsp_range = {
--- ['start'] = { line = 3, character = 5 },
--- ['end'] = { line = 4, character = 0 }
--- }
---
--- local range = vim.range.lsp(buf, lsp_range, 'utf-16')
--- local range = vim.range.lsp(0, lsp_range, 'utf-16')
--- ```
---@param buf integer
---@param range lsp.Range
@@ -293,6 +295,10 @@ function M.lsp(buf, range, position_encoding)
validate('range', range, 'table')
validate('position_encoding', position_encoding, 'string')
if buf == 0 then
buf = api.nvim_get_current_buf()
end
-- TODO(ofseed): avoid using `Pos:lsp()` here,
-- as they need reading files separately if buffer is unloaded.
local start = vim.pos.lsp(buf, range['start'], position_encoding)
@@ -305,8 +311,7 @@ end
---
--- Example:
--- ```lua
--- local buf = vim.api.nvim_get_current_buf()
--- local range = vim.range(buf, 3, 5, 4, 0)
--- local range = vim.range(0, 3, 5, 4, 0)
---
--- -- Convert to extmark range, you can call it in a method style.
--- local extmark_range = range:to_extmark()
@@ -324,9 +329,7 @@ end
---
--- Example:
--- ```lua
--- local buf = vim.api.nvim_get_current_buf()
---
--- local range = vim.range.extmark(buf, 3, 5, 4, 0)
--- local range = vim.range.extmark(0, 3, 5, 4, 0)
--- ```
---@param buf integer
---@param start_row integer
@@ -340,6 +343,10 @@ function M.extmark(buf, start_row, start_col, end_row, end_col)
validate('end_row', end_row, 'number')
validate('end_col', end_col, 'number')
if buf == 0 then
buf = api.nvim_get_current_buf()
end
local start = vim.pos.extmark(buf, start_row, start_col)
local end_ = vim.pos.extmark(buf, end_row, end_col)
@@ -350,8 +357,7 @@ end
---
--- Example:
--- ```lua
--- local buf = vim.api.nvim_get_current_buf()
--- local range = vim.range(buf, 3, 5, 4, 0)
--- local range = vim.range(0, 3, 5, 4, 0)
---
--- -- Convert to cursor range, you can call it in a method style.
--- local cursor_range = range:to_cursor()
@@ -369,12 +375,11 @@ end
---
--- Example:
--- ```lua
--- local buf = vim.api.nvim_get_current_buf()
--- local start = vim.api.nvim_win_get_cursor(0)
--- -- move the cursor
--- local end_ = vim.api.nvim_win_get_cursor(0)
---
--- local range = vim.range.cursor(buf, start, end_)
--- local range = vim.range.cursor(0, start, end_)
--- ```
---@param buf integer
---@param start_pos [integer, integer]
@@ -384,6 +389,10 @@ function M.cursor(buf, start_pos, end_pos)
validate('range', start_pos, 'table')
validate('range', end_pos, 'table')
if buf == 0 then
buf = api.nvim_get_current_buf()
end
local start = vim.pos.cursor(buf, start_pos)
local end_ = vim.pos.cursor(buf, end_pos)