feat(vim.pos)!: require buf param on vim.pos, vim.range #38665

Problem: `buf` is optional even though its needed to perform conversions
and the ordering of `(buf, row, col)` is not consistent.

Solution: make `buf` mandatory on `vim.range` and `vim.pos` and enforce
the `buf, row, col` ordering

(cherry picked from commit 01be30f638)
This commit is contained in:
Luis Calle
2026-04-06 10:51:36 -05:00
committed by github-actions[bot]
parent a5f1b373fa
commit 0a3add979a
8 changed files with 115 additions and 175 deletions

View File

@@ -4265,41 +4265,35 @@ by |vim.Pos| objects.
Fields: ~
• {row} (`integer`) 0-based byte index.
• {col} (`integer`) 0-based byte index.
• {buf}? (`integer`) Optional buffer handle.
When specified, it indicates that this position belongs
to a specific buffer. This field is required when
performing position conversions.
• {buf} (`integer`) buffer handle.
• {to_lsp} (`fun(pos: vim.Pos, position_encoding: lsp.PositionEncodingKind)`)
See |Pos:to_lsp()|.
• {lsp} (`fun(buf: integer, pos: lsp.Position, position_encoding: lsp.PositionEncodingKind)`)
See |Pos:lsp()|.
• {to_cursor} (`fun(pos: vim.Pos): integer, integer`) See
|Pos:to_cursor()|.
• {cursor} (`fun(pos: [integer, integer], opts: vim.Pos.Optional?)`)
See |Pos:cursor()|.
• {cursor} (`fun(buf: integer, pos: [integer, integer])`) See
|Pos:cursor()|.
• {to_extmark} (`fun(pos: vim.Pos): integer, integer`) See
|Pos:to_extmark()|.
• {extmark} (`fun(row: integer, col: integer, opts: vim.Pos.Optional?)`)
See |Pos:extmark()|.
• {extmark} (`fun(buf: integer, row: integer, col: integer)`) See
|Pos:extmark()|.
Pos:cursor({pos}, {opts}) *Pos:cursor()*
Pos:cursor({buf}, {pos}) *Pos:cursor()*
Creates a new |vim.Pos| from cursor position (see |api-indexing|).
Parameters: ~
• {pos} (`[integer, integer]`)
• {opts} (`table?`) A table with the following fields:
• {buf}? (`integer`)
• {buf} (`integer`)
• {pos} (`[integer, integer]`)
Pos:extmark({row}, {col}, {opts}) *Pos:extmark()*
Pos:extmark({buf}, {row}, {col}) *Pos:extmark()*
Creates a new |vim.Pos| from extmark position (see |api-indexing|).
Parameters: ~
• {row} (`integer`)
• {col} (`integer`)
• {opts} (`table?`) A table with the following fields:
• {buf}? (`integer`)
• {buf} (`integer`)
• {row} (`integer`)
• {col} (`integer`)
Pos:lsp({buf}, {pos}, {position_encoding}) *Pos:lsp()*
Creates a new |vim.Pos| from `lsp.Position`.
@@ -4311,7 +4305,6 @@ Pos:lsp({buf}, {pos}, {position_encoding}) *Pos:lsp()*
character = 5
}
-- `buf` is mandatory, as LSP positions are always associated with a buffer.
local pos = vim.pos.lsp(buf, lsp_pos, 'utf-16')
<
@@ -4344,9 +4337,8 @@ Pos:to_lsp({pos}, {position_encoding}) *Pos:to_lsp()*
Converts |vim.Pos| to `lsp.Position`.
Example: >lua
-- `buf` is required for conversion to LSP position.
local buf = vim.api.nvim_get_current_buf()
local pos = vim.pos(3, 5, { buf = buf })
local pos = vim.pos(buf, 3, 5)
-- Convert to LSP position, you can call it in a method style.
local lsp_pos = pos:lsp('utf-16')
@@ -4401,11 +4393,7 @@ Provides operations to compare, calculate, and convert ranges represented by
• {start_col} (`integer`) 0-based byte index.
• {end_row} (`integer`) 0-based byte index.
• {end_col} (`integer`) 0-based byte index.
• {buf}? (`integer`) Optional buffer handle.
When specified, it indicates that this range belongs to
a specific buffer. This field is required when
performing range conversions.
• {buf} (`integer`) Optional buffer handle.
• {is_empty} (`fun(self: vim.Range): boolean`) See
|Range:is_empty()|.
• {has} (`fun(outer: vim.Range, inner: vim.Range|vim.Pos): boolean`)
@@ -4417,14 +4405,14 @@ Provides operations to compare, calculate, and convert ranges represented by
• {lsp} (`fun(buf: integer, range: lsp.Range, position_encoding: lsp.PositionEncodingKind)`)
See |Range:lsp()|.
• {to_extmark} (`fun(range: vim.Range)`) See |Range:to_extmark()|.
• {extmark} (`fun(start_row: integer, start_col: integer, end_row: integer, end_col: integer, opts: vim.Pos.Optional?)`)
• {extmark} (`fun(buf: integer, start_row: integer, start_col: integer, end_row: integer, end_col: integer)`)
See |Range:extmark()|.
• {to_cursor} (`fun(range: vim.Range)`) See |Range:to_cursor()|.
• {cursor} (`fun(buf: integer, start_pos: [integer, integer], end_pos: [integer, integer], opts: vim.Pos.Optional?)`)
• {cursor} (`fun(buf: integer, start_pos: [integer, integer], end_pos: [integer, integer])`)
See |Range:cursor()|.
Range:cursor({buf}, {start_pos}, {end_pos}, {opts}) *Range:cursor()*
Range:cursor({buf}, {start_pos}, {end_pos}) *Range:cursor()*
Creates a new |vim.Range| from mark-like range (see |api-indexing|).
Example: >lua
@@ -4433,33 +4421,30 @@ Range:cursor({buf}, {start_pos}, {end_pos}, {opts}) *Range:cursor()*
-- move the cursor
local end_ = vim.api.nvim_win_get_cursor(0)
local range = vim.range.cursor(start, end_, { buf = buf })
local range = vim.range.cursor(buf, start, end_)
<
Parameters: ~
• {buf} (`integer`)
• {start_pos} (`[integer, integer]`)
• {end_pos} (`[integer, integer]`)
• {opts} (`table?`) A table with the following fields:
• {buf}? (`integer`)
*Range:extmark()*
Range:extmark({start_row}, {start_col}, {end_row}, {end_col}, {opts})
Range:extmark({buf}, {start_row}, {start_col}, {end_row}, {end_col})
Creates a new |vim.Range| from extmark range (see |api-indexing|).
Example: >lua
local buf = vim.api.nvim_get_current_buf()
local range = vim.range.extmark(3, 5, 4, 0, { buf = buf })
local range = vim.range.extmark(buf, 3, 5, 4, 0)
<
Parameters: ~
• {buf} (`integer`)
• {start_row} (`integer`)
• {start_col} (`integer`)
• {end_row} (`integer`)
• {end_col} (`integer`)
• {opts} (`table?`) A table with the following fields:
• {buf}? (`integer`)
Range:has({outer}, {inner}) *Range:has()*
Checks whether {outer} range contains {inner} range or position.
@@ -4499,7 +4484,6 @@ Range:lsp({buf}, {range}, {position_encoding}) *Range:lsp()*
['end'] = { line = 4, character = 0 }
}
-- `buf` is mandatory, as LSP ranges are always associated with a buffer.
local range = vim.range.lsp(buf, lsp_range, 'utf-16')
<
@@ -4512,9 +4496,8 @@ Range:to_cursor({range}) *Range:to_cursor()*
Converts |vim.Range| to mark-like range (see |api-indexing|).
Example: >lua
-- `buf` is required for conversion to extmark range.
local buf = vim.api.nvim_get_current_buf()
local range = vim.range(3, 5, 4, 0, { buf = buf })
local range = vim.range(buf, 3, 5, 4, 0)
-- Convert to cursor range, you can call it in a method style.
local cursor_range = range:to_cursor()
@@ -4527,9 +4510,8 @@ Range:to_extmark({range}) *Range:to_extmark()*
Converts |vim.Range| to extmark range (see |api-indexing|).
Example: >lua
-- `buf` is required for conversion to extmark range.
local buf = vim.api.nvim_get_current_buf()
local range = vim.range(3, 5, 4, 0, { buf = buf })
local range = vim.range(buf, 3, 5, 4, 0)
-- Convert to extmark range, you can call it in a method style.
local extmark_range = range:to_extmark()
@@ -4542,9 +4524,8 @@ Range:to_lsp({range}, {position_encoding}) *Range:to_lsp()*
Converts |vim.Range| to `lsp.Range`.
Example: >lua
-- `buf` is required for conversion to LSP range.
local buf = vim.api.nvim_get_current_buf()
local range = vim.range(3, 5, 4, 0, { buf = buf })
local range = vim.range(buf, 3, 5, 4, 0)
-- Convert to LSP range, you can call it in a method style.
local lsp_range = range:to_lsp('utf-16')

View File

@@ -463,7 +463,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(api.nvim_win_get_cursor(winid))
local pos = vim.pos.cursor(bufnr, api.nvim_win_get_cursor(winid))
local params = {
textDocument = vim.lsp.util.make_text_document_params(bufnr),
}

View File

@@ -303,7 +303,8 @@ api.nvim_set_decoration_provider(document_color_ns, {
local function get_hl_info_under_cursor(provider)
local cursor_row, cursor_col = unpack(api.nvim_win_get_cursor(0)) --- @type integer, integer
cursor_row = cursor_row - 1 -- Convert to 0-based index
local cursor_pos = vim.pos(cursor_row, cursor_col)
local buf = api.nvim_get_current_buf()
local cursor_pos = vim.pos(buf, cursor_row, cursor_col)
for client_id, state in pairs(provider.client_state) do
for _, hl in ipairs(state.hl_info) do

View File

@@ -223,7 +223,8 @@ function Completor:show(hint)
if current.range then
row, col = current.range:to_extmark()
else
row, col = vim.pos.cursor(api.nvim_win_get_cursor(vim.fn.bufwinid(self.bufnr))):to_extmark()
row, col =
vim.pos.cursor(self.bufnr, api.nvim_win_get_cursor(vim.fn.bufwinid(self.bufnr))):to_extmark()
end
-- To ensure that virtual text remains visible continuously (without flickering)
@@ -245,7 +246,7 @@ function Completor:show(hint)
-- At least, characters before the cursor should be skipped.
if api.nvim_win_get_buf(winid) == self.bufnr then
local cursor_row, cursor_col =
vim.pos.cursor(api.nvim_win_get_cursor(winid), { buf = self.bufnr }):to_extmark()
vim.pos.cursor(self.bufnr, api.nvim_win_get_cursor(winid)):to_extmark()
if row == cursor_row then
skip = math.max(skip, cursor_col - col + 1)
end

View File

@@ -35,12 +35,7 @@ local validate = vim.validate
---@class vim.Pos
---@field row integer 0-based byte index.
---@field col integer 0-based byte index.
---
--- Optional buffer handle.
---
--- When specified, it indicates that this position belongs to a specific buffer.
--- This field is required when performing position conversions.
---@field buf? integer
---@field buf integer buffer handle.
---@field private [1] integer underlying representation of row
---@field private [2] integer underlying representation of col
---@field private [3] integer underlying representation of buf
@@ -61,26 +56,20 @@ function Pos.__index(pos, key)
return Pos[key]
end
---@class vim.Pos.Optional
---@inlinedoc
---@field buf? integer
---@package
---@param buf integer
---@param row integer
---@param col integer
---@param opts? vim.Pos.Optional
function Pos.new(row, col, opts)
function Pos.new(buf, row, col)
validate('buf', buf, 'number')
validate('row', row, 'number')
validate('col', col, 'number')
validate('opts', opts, 'table', true)
opts = opts or {}
---@type vim.Pos
local self = setmetatable({
row,
col,
opts.buf,
buf,
}, Pos)
return self
@@ -134,9 +123,8 @@ end
---
--- Example:
--- ```lua
--- -- `buf` is required for conversion to LSP position.
--- local buf = vim.api.nvim_get_current_buf()
--- local pos = vim.pos(3, 5, { buf = buf })
--- local pos = vim.pos(buf, 3, 5)
---
--- -- Convert to LSP position, you can call it in a method style.
--- local lsp_pos = pos:lsp('utf-16')
@@ -147,8 +135,7 @@ function Pos.to_lsp(pos, position_encoding)
validate('pos', pos, 'table')
validate('position_encoding', position_encoding, 'string')
local buf = assert(pos.buf, 'position is not a buffer position')
local row, col = pos.row, pos.col
local buf, row, col = pos.buf, pos.row, pos.col
-- When on the first character,
-- we can ignore the difference between byte and character.
if col > 0 then
@@ -169,7 +156,6 @@ end
--- character = 5
--- }
---
--- -- `buf` is mandatory, as LSP positions are always associated with a buffer.
--- local pos = vim.pos.lsp(buf, lsp_pos, 'utf-16')
--- ```
---@param buf integer
@@ -189,7 +175,7 @@ function Pos.lsp(buf, pos, position_encoding)
col = vim.str_byteindex(get_line(buf, row), position_encoding, col, false)
end
return Pos.new(row, col, { buf = buf })
return Pos.new(buf, row, col)
end
--- Converts |vim.Pos| to cursor position (see |api-indexing|).
@@ -200,10 +186,10 @@ function Pos.to_cursor(pos)
end
--- Creates a new |vim.Pos| from cursor position (see |api-indexing|).
---@param buf integer
---@param pos [integer, integer]
---@param opts vim.Pos.Optional|nil
function Pos.cursor(pos, opts)
return Pos.new(pos[1] - 1, pos[2], opts)
function Pos.cursor(buf, pos)
return Pos.new(buf, pos[1] - 1, pos[2])
end
--- Converts |vim.Pos| to extmark position (see |api-indexing|).
@@ -223,11 +209,11 @@ function Pos.to_extmark(pos)
end
--- Creates a new |vim.Pos| from extmark position (see |api-indexing|).
---@param buf integer
---@param row integer
---@param col integer
---@param opts vim.Pos.Optional|nil
function Pos.extmark(row, col, opts)
return Pos.new(row, col, opts)
function Pos.extmark(buf, row, col)
return Pos.new(buf, row, col)
end
-- Overload `Range.new` to allow calling this module as a function.
@@ -236,6 +222,6 @@ setmetatable(Pos, {
return Pos.new(...)
end,
})
---@cast Pos +fun(row: integer, col: integer, opts: vim.Pos.Optional?): vim.Pos
---@cast Pos +fun(buf: integer, row: integer, col: integer): vim.Pos
return Pos

View File

@@ -41,16 +41,12 @@ local api = vim.api
---@field start_col integer 0-based byte index.
---@field end_row integer 0-based byte index.
---@field end_col integer 0-based byte index.
---
--- Optional buffer handle.
---
--- When specified, it indicates that this range belongs to a specific buffer.
--- This field is required when performing range conversions.
---@field buf? integer
---@field buf integer Optional buffer handle.
---@field private [1] integer underlying representation of start_row
---@field private [2] integer underlying representation of start_col
---@field private [3] integer underlying representation of end_row
---@field private [4] integer underlying representation of end_col
---@field private [5] integer underlying representation of buf
local Range = {}
---@private
@@ -74,7 +70,7 @@ end
---@package
---@overload fun(self: vim.Range, start: vim.Pos, end_: vim.Pos): vim.Range
---@overload fun(self: vim.Range, start_row: integer, start_col: integer, end_row: integer, end_col: integer, opts?: vim.Pos.Optional): vim.Range
---@overload fun(self: vim.Range, buf: integer, start_row: integer, start_col: integer, end_row: integer, end_col: integer): vim.Range
function Range.new(...)
---@type integer, integer, integer, integer, integer|nil
local start_row, start_col, end_row, end_col, buf
@@ -91,11 +87,9 @@ function Range.new(...)
end
start_row, start_col, end_row, end_col, buf =
start.row, start.col, end_.row, end_.col, start.buf
elseif nargs == 4 or nargs == 5 then
local opts
---@type integer, integer, integer, integer, vim.Pos.Optional|nil
start_row, start_col, end_row, end_col, opts = ...
buf = opts and opts.buf
elseif nargs == 5 then
---@type integer, integer, integer, integer, integer
buf, start_row, start_col, end_row, end_col = ...
else
error('invalid parameters')
end
@@ -147,7 +141,7 @@ end
---@param col integer
---@param buf integer
---@return integer, integer
local function to_inclusive_pos(row, col, buf)
local function to_inclusive_pos(buf, row, col)
if col > 0 then
col = col - 1
elseif col == 0 and row > 0 then
@@ -163,7 +157,7 @@ end
---@param r2 vim.Range
function Range.__lt(r1, r2)
local r1_inclusive_end_row, r1_inclusive_end_col =
to_inclusive_pos(r1.end_row, r1.end_col, r1.buf)
to_inclusive_pos(r1.buf, r1.end_row, r1.end_col)
return cmp_pos(r1_inclusive_end_row, r1_inclusive_end_col, r2.start_row, r2.start_col) == -1
end
@@ -172,7 +166,7 @@ end
---@param r2 vim.Range
function Range.__le(r1, r2)
local r1_inclusive_end_row, r1_inclusive_end_col =
to_inclusive_pos(r1.end_row, r1.end_col, r1.buf)
to_inclusive_pos(r1.buf, r1.end_row, r1.end_col)
return cmp_pos(r1_inclusive_end_row, r1_inclusive_end_col, r2.start_row, r2.start_col) ~= 1
end
@@ -189,7 +183,7 @@ end
---@return boolean `true` if the given range is empty
function Range:is_empty()
local inclusive_end_row, inclusive_end_col =
to_inclusive_pos(self.end_row, self.end_col, self.buf)
to_inclusive_pos(self.buf, self.end_row, self.end_col)
return cmp_pos(self.start_row, self.start_col, inclusive_end_row, inclusive_end_col) ~= -1
end
@@ -208,9 +202,9 @@ function Range.has(outer, inner)
---@cast inner -vim.Pos
local outer_inclusive_end_row, outer_inclusive_end_col =
to_inclusive_pos(outer.end_row, outer.end_col, outer.buf)
to_inclusive_pos(outer.buf, outer.end_row, outer.end_col)
local inner_inclusive_end_row, inner_inclusive_end_col =
to_inclusive_pos(inner.end_row, inner.end_col, inner.buf)
to_inclusive_pos(inner.buf, inner.end_row, inner.end_col)
return cmp_pos(outer.start_row, outer.start_col, inner.start_row, inner.start_col) ~= 1
and cmp_pos(outer.end_row, outer.end_col, inner.end_row, inner.end_col) ~= -1
@@ -234,9 +228,9 @@ end
--- `nil` if such range does not exist.
function Range.intersect(r1, r2)
local r1_inclusive_end_row, r1_inclusive_end_col =
to_inclusive_pos(r1.end_row, r1.end_col, r1.buf)
to_inclusive_pos(r1.buf, r1.end_row, r1.end_col)
local r2_inclusive_end_row, r2_inclusive_end_col =
to_inclusive_pos(r2.end_row, r2.end_col, r2.buf)
to_inclusive_pos(r2.buf, r2.end_row, r2.end_col)
if
cmp_pos(r1_inclusive_end_row, r1_inclusive_end_col, r2.start_row, r2.start_col) ~= 1
@@ -254,9 +248,8 @@ end
---
--- Example:
--- ```lua
--- -- `buf` is required for conversion to LSP range.
--- local buf = vim.api.nvim_get_current_buf()
--- local range = vim.range(3, 5, 4, 0, { buf = buf })
--- local range = vim.range(buf, 3, 5, 4, 0)
---
--- -- Convert to LSP range, you can call it in a method style.
--- local lsp_range = range:to_lsp('utf-16')
@@ -270,10 +263,8 @@ function Range.to_lsp(range, position_encoding)
---@type lsp.Range
return {
['start'] = vim
.pos(range.start_row, range.start_col, { buf = range.buf })
:to_lsp(position_encoding),
['end'] = vim.pos(range.end_row, range.end_col, { buf = range.buf }):to_lsp(position_encoding),
['start'] = vim.pos(range.buf, range.start_row, range.start_col):to_lsp(position_encoding),
['end'] = vim.pos(range.buf, range.end_row, range.end_col):to_lsp(position_encoding),
}
end
@@ -287,7 +278,6 @@ end
--- ['end'] = { line = 4, character = 0 }
--- }
---
--- -- `buf` is mandatory, as LSP ranges are always associated with a buffer.
--- local range = vim.range.lsp(buf, lsp_range, 'utf-16')
--- ```
---@param buf integer
@@ -310,9 +300,8 @@ end
---
--- Example:
--- ```lua
--- -- `buf` is required for conversion to extmark range.
--- local buf = vim.api.nvim_get_current_buf()
--- local range = vim.range(3, 5, 4, 0, { buf = buf })
--- local range = vim.range(buf, 3, 5, 4, 0)
---
--- -- Convert to extmark range, you can call it in a method style.
--- local extmark_range = range:to_extmark()
@@ -321,8 +310,8 @@ end
function Range.to_extmark(range)
validate('range', range, 'table')
local srow, scol = vim.pos(range.start_row, range.start_col, { buf = range.buf }):to_extmark()
local erow, ecol = vim.pos(range.end_row, range.end_col, { buf = range.buf }):to_extmark()
local srow, scol = vim.pos(range.buf, range.start_row, range.start_col):to_extmark()
local erow, ecol = vim.pos(range.buf, range.end_row, range.end_col):to_extmark()
return srow, scol, erow, ecol
end
@@ -332,21 +321,22 @@ end
--- ```lua
--- local buf = vim.api.nvim_get_current_buf()
---
--- local range = vim.range.extmark(3, 5, 4, 0, { buf = buf })
--- local range = vim.range.extmark(buf, 3, 5, 4, 0)
--- ```
---@param buf integer
---@param start_row integer
---@param start_col integer
---@param end_row integer
---@param end_col integer
---@param opts vim.Pos.Optional|nil
function Range.extmark(start_row, start_col, end_row, end_col, opts)
validate('range', start_row, 'number')
validate('range', start_col, 'number')
validate('range', end_row, 'number')
validate('range', end_col, 'number')
function Range.extmark(buf, start_row, start_col, end_row, end_col)
validate('buf', buf, 'number')
validate('start_row', start_row, 'number')
validate('start_col', start_col, 'number')
validate('end_row', end_row, 'number')
validate('end_col', end_col, 'number')
local start = vim.pos.extmark(start_row, start_col, opts)
local end_ = vim.pos.extmark(end_row, end_col, opts)
local start = vim.pos.extmark(buf, start_row, start_col)
local end_ = vim.pos.extmark(buf, end_row, end_col)
return Range.new(start, end_)
end
@@ -355,9 +345,8 @@ end
---
--- Example:
--- ```lua
--- -- `buf` is required for conversion to extmark range.
--- local buf = vim.api.nvim_get_current_buf()
--- local range = vim.range(3, 5, 4, 0, { buf = buf })
--- local range = vim.range(buf, 3, 5, 4, 0)
---
--- -- Convert to cursor range, you can call it in a method style.
--- local cursor_range = range:to_cursor()
@@ -366,8 +355,8 @@ end
function Range.to_cursor(range)
validate('range', range, 'table')
local srow, scol = vim.pos(range.start_row, range.start_col, { buf = range.buf }):to_cursor()
local erow, ecol = vim.pos(range.end_row, range.end_col, { buf = range.buf }):to_cursor()
local srow, scol = vim.pos(range.buf, range.start_row, range.start_col):to_cursor()
local erow, ecol = vim.pos(range.buf, range.end_row, range.end_col):to_cursor()
return srow, scol, erow, ecol
end
@@ -380,19 +369,18 @@ end
--- -- move the cursor
--- local end_ = vim.api.nvim_win_get_cursor(0)
---
--- local range = vim.range.cursor(start, end_, { buf = buf })
--- local range = vim.range.cursor(buf, start, end_)
--- ```
---@param buf integer
---@param start_pos [integer, integer]
---@param end_pos [integer, integer]
---@param opts vim.Pos.Optional|nil
function Range.cursor(buf, start_pos, end_pos, opts)
function Range.cursor(buf, start_pos, end_pos)
validate('buf', buf, 'number')
validate('range', start_pos, 'table')
validate('range', end_pos, 'table')
local start = vim.pos.cursor(start_pos, opts)
local end_ = vim.pos.cursor(end_pos, opts)
local start = vim.pos.cursor(buf, start_pos)
local end_ = vim.pos.cursor(buf, end_pos)
return Range.new(start, end_)
end
@@ -404,6 +392,6 @@ setmetatable(Range, {
end,
})
---@cast Range +fun(start: vim.Pos, end_: vim.Pos): vim.Range
---@cast Range +fun(start_row: integer, start_col: integer, end_row: integer, end_col: integer, opts?: vim.Pos.Optional): vim.Range
---@cast Range +fun(buf: integer, start_row: integer, start_col: integer, end_row: integer, end_col: integer): vim.Range
return Range

View File

@@ -10,19 +10,10 @@ local insert = n.insert
describe('vim.pos', function()
before_each(clear)
it('creates a position with or without optional fields', function()
local pos = exec_lua(function()
return vim.pos(3, 5)
end)
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)
end)
pos = exec_lua(function()
return vim.pos(3, 5, { buf = buf })
it('creates a position', function()
local pos, buf = exec_lua(function()
local buf = vim.api.nvim_create_buf(false, true)
return vim.pos(buf, 3, 5), buf
end)
eq(3, pos[1])
eq(5, pos[2])
@@ -30,40 +21,43 @@ describe('vim.pos', function()
end)
it('comparisons by overloaded operators', function()
local buf = exec_lua(function()
return vim.api.nvim_create_buf(false, true)
end)
eq(
true,
exec_lua(function()
return vim.pos(3, 5) < vim.pos(4, 5)
return vim.pos(buf, 3, 5) < vim.pos(buf, 4, 5)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(3, 5) <= vim.pos(3, 6)
return vim.pos(buf, 3, 5) <= vim.pos(buf, 3, 6)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(3, 5) > vim.pos(2, 5)
return vim.pos(buf, 3, 5) > vim.pos(buf, 2, 5)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(3, 5) >= vim.pos(3, 5)
return vim.pos(buf, 3, 5) >= vim.pos(buf, 3, 5)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(3, 5) == vim.pos(3, 5)
return vim.pos(buf, 3, 5) == vim.pos(buf, 3, 5)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(3, 5) ~= vim.pos(3, 6)
return vim.pos(buf, 3, 5) ~= vim.pos(buf, 3, 6)
end)
)
end)
@@ -74,7 +68,7 @@ describe('vim.pos', function()
end)
insert('Neovim 是 Vim 的分支,专注于扩展性和可用性。')
local lsp_pos = exec_lua(function()
local pos = vim.pos(0, 36, { buf = buf })
local pos = vim.pos(buf, 0, 36)
return pos:to_lsp('utf-16')
end)
eq({ line = 0, character = 20 }, lsp_pos)
@@ -95,25 +89,25 @@ describe('vim.pos', function()
insert('Some text')
local extmark_pos = {
exec_lua(function()
local pos = vim.pos(1, 0, { buf = buf })
local pos = vim.pos(buf, 1, 0)
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 })
return vim.pos.extmark(buf, extmark_pos[1], extmark_pos[2])
end)
eq({ 0, 9, buf }, pos)
local extmark_pos2 = {
exec_lua(function()
local pos2 = vim.pos(0, 9, { buf = buf })
local pos2 = vim.pos(buf, 0, 9)
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 })
return vim.pos.extmark(buf, extmark_pos2[1], extmark_pos2[2])
end)
eq({ 0, 9, buf }, pos2)
end)

View File

@@ -10,40 +10,27 @@ local insert = n.insert
describe('vim.range', function()
before_each(clear)
it('creates a range with or without optional fields', function()
local range = exec_lua(function()
return vim.range(3, 5, 4, 6)
it('creates a range', function()
local range, buf = exec_lua(function()
local buf = vim.api.nvim_create_buf(false, true)
return vim.range(buf, 3, 5, 4, 6), buf
end)
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[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))
it('creates a range from two positions', function()
local range, buf1 = exec_lua(function()
local buf = vim.api.nvim_create_buf(false, true)
return vim.range(vim.pos(buf, 3, 5), vim.pos(buf, 4, 6)), buf
end)
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)
end)
range = exec_lua(function()
return vim.range(vim.pos(3, 5, { buf = buf1 }), vim.pos(4, 6, { buf = buf1 }))
end)
eq(buf1, range[5])
local buf2 = exec_lua(function()
@@ -51,7 +38,7 @@ describe('vim.range', function()
end)
local success = exec_lua(function()
return pcall(function()
return vim.range(vim.pos(3, 5, { buf = buf1 }), vim.pos(4, 6, { buf = buf2 }))
return vim.range(vim.pos(buf1, 3, 5), vim.pos(buf2, 4, 6))
end)
end)
eq(success, false)
@@ -63,7 +50,7 @@ describe('vim.range', function()
end)
insert('Neovim 是 Vim 的分支,专注于扩展性和可用性。')
local lsp_range = exec_lua(function()
local range = vim.range(0, 10, 0, 36, { buf = buf })
local range = vim.range(buf, 0, 10, 0, 36)
return range:to_lsp('utf-16')
end)
eq({
@@ -86,7 +73,8 @@ describe('vim.range', function()
eq(
true,
exec_lua(function()
return vim.range(0, 0, 1, 5):has(vim.pos(0, 1))
local buf = vim.api.nvim_create_buf(false, true)
return vim.range(buf, 0, 0, 1, 5):has(vim.pos(buf, 0, 1))
end)
)
end)
@@ -95,7 +83,8 @@ describe('vim.range', function()
eq(
false,
exec_lua(function()
return vim.range(0, 0, 0, 4):has(vim.range(0, 0, 0, 0))
local buf = vim.api.nvim_create_buf(false, true)
return vim.range(buf, 0, 0, 0, 4):has(vim.range(buf, 0, 0, 0, 0))
end)
)
end)