mirror of
https://github.com/neovim/neovim.git
synced 2025-12-19 12:55:32 +00:00
feat(lua): conversion between cursor positions
This commit is contained in:
@@ -3990,15 +3990,24 @@ comparisons and conversions between various types of positions.
|
||||
• {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.
|
||||
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()|.
|
||||
• {to_cursor} (`fun(pos: vim.Pos): [integer, integer]`) See
|
||||
|Pos:to_cursor()|.
|
||||
• {cursor} (`fun(pos: [integer, integer])`) See |Pos:cursor()|.
|
||||
|
||||
|
||||
Pos:cursor({pos}) *Pos:cursor()*
|
||||
Creates a new |vim.Pos| from cursor position.
|
||||
|
||||
Parameters: ~
|
||||
• {pos} (`[integer, integer]`)
|
||||
|
||||
Pos:lsp({buf}, {pos}, {position_encoding}) *Pos:lsp()*
|
||||
Creates a new |vim.Pos| from `lsp.Position`.
|
||||
|
||||
@@ -4018,6 +4027,15 @@ Pos:lsp({buf}, {pos}, {position_encoding}) *Pos:lsp()*
|
||||
• {pos} (`lsp.Position`)
|
||||
• {position_encoding} (`lsp.PositionEncodingKind`)
|
||||
|
||||
Pos:to_cursor({pos}) *Pos:to_cursor()*
|
||||
Converts |vim.Pos| to cursor position.
|
||||
|
||||
Parameters: ~
|
||||
• {pos} (`vim.Pos`) See |vim.Pos|.
|
||||
|
||||
Return: ~
|
||||
(`[integer, integer]`)
|
||||
|
||||
Pos:to_lsp({pos}, {position_encoding}) *Pos:to_lsp()*
|
||||
Converts |vim.Pos| to `lsp.Position`.
|
||||
|
||||
|
||||
@@ -178,6 +178,19 @@ function Pos.lsp(buf, pos, position_encoding)
|
||||
return Pos.new(row, col, { buf = buf })
|
||||
end
|
||||
|
||||
--- Converts |vim.Pos| to cursor position.
|
||||
---@param pos vim.Pos
|
||||
---@return [integer, integer]
|
||||
function Pos.to_cursor(pos)
|
||||
return { pos.row + 1, pos.col }
|
||||
end
|
||||
|
||||
--- Creates a new |vim.Pos| from cursor position.
|
||||
---@param pos [integer, integer]
|
||||
function Pos.cursor(pos)
|
||||
return Pos.new(pos[1] - 1, pos[2])
|
||||
end
|
||||
|
||||
-- Overload `Range.new` to allow calling this module as a function.
|
||||
setmetatable(Pos, {
|
||||
__call = function(_, ...)
|
||||
|
||||
Reference in New Issue
Block a user