Skip to content

Commit 7499c9f

Browse files
committed
feat(lua): conversion between cursor positions
1 parent a4a690e commit 7499c9f

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

runtime/doc/lua.txt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,19 +3986,28 @@ 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.
3989+
{row} (`integer`) 0-based byte index.
3990+
{col} (`integer`) 0-based byte index.
3991+
{buf}? (`integer`) Optional buffer handle.
39923992

3993-
When specified, it indicates that this position belongs to a
3994-
specific buffer. This field is required when performing
3995-
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()|.
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()|.
40004003

40014004

4005+
Pos:cursor({pos}) *Pos:cursor()*
4006+
Creates a new |vim.Pos| from cursor position.
4007+
4008+
Parameters: ~
4009+
{pos} (`[integer, integer]`)
4010+
40024011
Pos:lsp({buf}, {pos}, {position_encoding}) *Pos:lsp()*
40034012
Creates a new |vim.Pos| from `lsp.Position`.
40044013

@@ -4018,6 +4027,15 @@ Pos:lsp({buf}, {pos}, {position_encoding}) *Pos:lsp()*
40184027
{pos} (`lsp.Position`)
40194028
• {position_encoding} (`lsp.PositionEncodingKind`)
40204029

4030+
Pos:to_cursor({pos}) *Pos:to_cursor()*
4031+
Converts |vim.Pos| to cursor position.
4032+
4033+
Parameters: ~
4034+
{pos} (`vim.Pos`) See |vim.Pos|.
4035+
4036+
Return: ~
4037+
(`[integer, integer]`)
4038+
40214039
Pos:to_lsp({pos}, {position_encoding}) *Pos:to_lsp()*
40224040
Converts |vim.Pos| to `lsp.Position`.
40234041

runtime/lua/vim/pos.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ function Pos.lsp(buf, pos, position_encoding)
178178
return Pos.new(row, col, { buf = buf })
179179
end
180180

181+
--- Converts |vim.Pos| to cursor position.
182+
---@param pos vim.Pos
183+
---@return [integer, integer]
184+
function Pos.to_cursor(pos)
185+
return { pos.row + 1, pos.col }
186+
end
187+
188+
--- Creates a new |vim.Pos| from cursor position.
189+
---@param pos [integer, integer]
190+
function Pos.cursor(pos)
191+
return Pos.new(pos[1] - 1, pos[2])
192+
end
193+
181194
-- Overload `Range.new` to allow calling this module as a function.
182195
setmetatable(Pos, {
183196
__call = function(_, ...)

0 commit comments

Comments
 (0)