Skip to content

Commit 62b45b8

Browse files
committed
feat(lua): conversion between extmark positions
1 parent 7499c9f commit 62b45b8

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

runtime/doc/lua.txt

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,20 +3986,23 @@ comparisons and conversions between various types of positions.
39863986
as format conversions.
39873987

39883988
Fields: ~
3989-
{row} (`integer`) 0-based byte index.
3990-
{col} (`integer`) 0-based byte index.
3991-
{buf}? (`integer`) Optional buffer handle.
3992-
3993-
When specified, it indicates that this position belongs
3994-
to a specific buffer. This field is required when
3995-
performing position conversions.
3996-
• {to_lsp} (`fun(pos: vim.Pos, position_encoding: lsp.PositionEncodingKind)`)
3997-
See |Pos:to_lsp()|.
3998-
{lsp} (`fun(buf: integer, pos: lsp.Position, position_encoding: lsp.PositionEncodingKind)`)
3999-
See |Pos:lsp()|.
4000-
• {to_cursor} (`fun(pos: vim.Pos): [integer, integer]`) See
4001-
|Pos:to_cursor()|.
4002-
{cursor} (`fun(pos: [integer, integer])`) See |Pos:cursor()|.
3989+
{row} (`integer`) 0-based byte index.
3990+
{col} (`integer`) 0-based byte index.
3991+
{buf}? (`integer`) Optional buffer handle.
3992+
3993+
When specified, it indicates that this position belongs
3994+
to a specific buffer. This field is required when
3995+
performing position conversions.
3996+
• {to_lsp} (`fun(pos: vim.Pos, position_encoding: lsp.PositionEncodingKind)`)
3997+
See |Pos:to_lsp()|.
3998+
{lsp} (`fun(buf: integer, pos: lsp.Position, position_encoding: lsp.PositionEncodingKind)`)
3999+
See |Pos:lsp()|.
4000+
• {to_cursor} (`fun(pos: vim.Pos): [integer, integer]`) See
4001+
|Pos:to_cursor()|.
4002+
{cursor} (`fun(pos: [integer, integer])`) See |Pos:cursor()|.
4003+
• {to_extmark} (`fun(pos: vim.Pos): [integer, integer]`) See
4004+
|Pos:to_extmark()|.
4005+
{extmark} (`fun(pos: [integer, integer])`) See |Pos:extmark()|.
40034006

40044007

40054008
Pos:cursor({pos}) *Pos:cursor()*
@@ -4008,6 +4011,12 @@ Pos:cursor({pos}) *Pos:cursor()*
40084011
Parameters: ~
40094012
{pos} (`[integer, integer]`)
40104013

4014+
Pos:extmark({pos}) *Pos:extmark()*
4015+
Creates a new |vim.Pos| from extmark position.
4016+
4017+
Parameters: ~
4018+
{pos} (`[integer, integer]`)
4019+
40114020
Pos:lsp({buf}, {pos}, {position_encoding}) *Pos:lsp()*
40124021
Creates a new |vim.Pos| from `lsp.Position`.
40134022

@@ -4036,6 +4045,15 @@ Pos:to_cursor({pos}) *Pos:to_cursor()*
40364045
Return: ~
40374046
(`[integer, integer]`)
40384047

4048+
Pos:to_extmark({pos}) *Pos:to_extmark()*
4049+
Converts |vim.Pos| to extmark position.
4050+
4051+
Parameters: ~
4052+
{pos} (`vim.Pos`) See |vim.Pos|.
4053+
4054+
Return: ~
4055+
(`[integer, integer]`)
4056+
40394057
Pos:to_lsp({pos}, {position_encoding}) *Pos:to_lsp()*
40404058
Converts |vim.Pos| to `lsp.Position`.
40414059

runtime/lua/vim/pos.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,20 @@ function Pos.cursor(pos)
191191
return Pos.new(pos[1] - 1, pos[2])
192192
end
193193

194+
--- Converts |vim.Pos| to extmark position.
195+
---@param pos vim.Pos
196+
---@return [integer, integer]
197+
function Pos.to_extmark(pos)
198+
return { pos.row, pos.col }
199+
end
200+
201+
--- Creates a new |vim.Pos| from extmark position.
202+
---@param pos [integer, integer]
203+
function Pos.extmark(pos)
204+
local row, col = unpack(pos)
205+
return Pos.new(row, col)
206+
end
207+
194208
-- Overload `Range.new` to allow calling this module as a function.
195209
setmetatable(Pos, {
196210
__call = function(_, ...)

0 commit comments

Comments
 (0)