Merge #35109 vim.pos, vim.range

This commit is contained in:
Justin M. Keyes
2025-08-17 16:37:56 -04:00
committed by GitHub
9 changed files with 768 additions and 0 deletions

View File

@@ -3949,6 +3949,205 @@ vim.net.request({url}, {opts}, {on_response}) *vim.net.request()*
success.
==============================================================================
Lua module: vim.pos *vim.pos*
WARNING: This module is under experimental support. Its semantics are not yet
finalized, and the stability of this API is not guaranteed. Avoid using it
outside of Nvim. You may subscribe to or participate in the tracking issue
https://github.com/neovim/neovim/issues/25509 to stay updated or contribute to
its development.
Built on |vim.Pos| objects, this module offers operations that support
comparisons and conversions between various types of positions.
*vim.Pos*
Represents a well-defined position.
A |vim.Pos| object contains the {row} and {col} coordinates of a position.
To create a new |vim.Pos| object, call `vim.pos()`.
Example: >lua
local pos1 = vim.pos(3, 5)
local pos2 = vim.pos(4, 0)
-- Operators are overloaded for comparing two `vim.Pos` objects.
if pos1 < pos2 then
print("pos1 comes before pos2")
end
if pos1 ~= pos2 then
print("pos1 and pos2 are different positions")
end
<
It may include optional fields that enable additional capabilities, such
as format conversions.
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.
• {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()|.
Pos:lsp({buf}, {pos}, {position_encoding}) *Pos:lsp()*
Creates a new |vim.Pos| from `lsp.Position`.
Example: >lua
local buf = vim.api.nvim_get_current_buf()
local lsp_pos = {
line = 3,
character = 5
}
-- `buf` is mandatory, as LSP positions are always associated with a buffer.
local pos = vim.pos.lsp(buf, lsp_pos, 'utf-16')
<
Parameters: ~
• {buf} (`integer`)
• {pos} (`lsp.Position`)
• {position_encoding} (`lsp.PositionEncodingKind`)
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 })
-- Convert to LSP position, you can call it in a method style.
local lsp_pos = pos:lsp('utf-16')
<
Parameters: ~
• {pos} (`vim.Pos`) See |vim.Pos|.
• {position_encoding} (`lsp.PositionEncodingKind`)
==============================================================================
Lua module: vim.range *vim.range*
WARNING: This module is under experimental support. Its semantics are not yet
finalized, and the stability of this API is not guaranteed. Avoid using it
outside of Nvim. You may subscribe to or participate in the tracking issue
https://github.com/neovim/neovim/issues/25509 to stay updated or contribute to
its development.
Built on |vim.Range| objects, this module offers operations that support
comparisons as well as containment checks (for positions and for other
ranges). conversions between various types of ranges is also provided.
*vim.Range*
Represents a well-defined range.
A |vim.Range| object contains a {start} and a {end_} position(see
|vim.Pos|). Note that the {end_} position is exclusive. To create a new
|vim.Range| object, call `vim.range()`.
Example: >lua
local pos1 = vim.pos(3, 5)
local pos2 = vim.pos(4, 0)
-- Create a range from two positions.
local range1 = vim.range(pos1, pos2)
-- Or createa range from four integers representing start and end positions.
local range2 = vim.range(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.
-- Operators are overloaded for comparing two `vim.Pos` objects.
if range1 == range2 then
print("range1 and range2 are the same range")
end
<
It may include optional fields that enable additional capabilities, such
as format conversions. Note that the {start} and {end_} positions need to
have the same optional fields.
Fields: ~
• {start} (`vim.Pos`) Start position.
• {end_} (`vim.Pos`) End position, exclusive.
• {has} (`fun(outer: vim.Range, inner: vim.Range): boolean`) See
|Range:has()|.
• {intersect} (`fun(r1: vim.Range, r2: vim.Range): vim.Range?`) See
|Range:intersect()|.
• {to_lsp} (`fun(range: vim.Range, position_encoding: lsp.PositionEncodingKind)`)
See |Range:to_lsp()|.
• {lsp} (`fun(buf: integer, range: lsp.Range, position_encoding: lsp.PositionEncodingKind)`)
See |Range:lsp()|.
Range:has({outer}, {inner}) *Range:has()*
Checks whether {outer} range contains {inner} range.
Parameters: ~
• {outer} (`vim.Range`) See |vim.Range|.
• {inner} (`vim.Range`) See |vim.Range|.
Return: ~
(`boolean`) `true` if {outer} range fully contains {inner} range.
Range:intersect({r1}, {r2}) *Range:intersect()*
Computes the common range shared by the given ranges.
Parameters: ~
• {r1} (`vim.Range`) First range to intersect. See |vim.Range|.
• {r2} (`vim.Range`) Second range to intersect. See |vim.Range|.
Return: ~
(`vim.Range?`) range that is present inside both `r1` and `r2`. `nil`
if such range does not exist. See |vim.Range|.
Range:lsp({buf}, {range}, {position_encoding}) *Range:lsp()*
Creates a new |vim.Range| from `lsp.Range`.
Example: >lua
local buf = vim.api.nvim_get_current_buf()
local lsp_range = {
['start'] = { line = 3, character = 5 },
['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')
<
Parameters: ~
• {buf} (`integer`)
• {range} (`lsp.Range`)
• {position_encoding} (`lsp.PositionEncodingKind`)
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 })
-- Convert to LSP range, you can call it in a method style.
local lsp_range = range:to_lsp('utf-16')
<
Parameters: ~
• {range} (`vim.Range`) See |vim.Range|.
• {position_encoding} (`lsp.PositionEncodingKind`)
==============================================================================
Lua module: vim.re *vim.re*

View File

@@ -241,6 +241,7 @@ LUA
• Built-in plugin manager |vim.pack|
• |vim.list.unique()| to deduplicate lists.
• |vim.list.bisect()| for binary search.
• Experimental `vim.pos` and `vim.range` for Position/Range abstraction.
OPTIONS