robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1---@brief 2--- 3--- EXPERIMENTAL: This API is unstable, do not use it. Its semantics are not yet finalized. 4--- Subscribe to this issue to stay updated: https://github.com/neovim/neovim/issues/25509 5--- 6--- Provides operations to compare, calculate, and convert positions represented by |vim.Pos| 7--- objects. 8 9local api =vim . api 10local validate =vim . validate 11local util =require ( 'vim.pos._util' ) 12 13--- Represents a buffer position based on [api-indexing] (0-indexed, end-exclusive ranges). 14--- Call `vim.pos()` to create a new `vim.Pos` by passing the {buf}, {row}, and {col} of a position. 15--- 16--- Example: 17--- ```lua 18--- local pos1 = vim.pos(0, 3, 5) 19--- local pos2 = vim.pos(0, 4, 0) 20--- 21--- -- Operators are overloaded for comparing two `vim.Pos` objects. 22--- if pos1 < pos2 then 23--- print("pos1 comes before pos2") 24--- end 25--- 26--- if pos1 ~= pos2 then 27--- print("pos1 and pos2 are different positions") 28--- end 29--- ``` 30--- 31--- It may include optional fields that enable additional capabilities, 32--- such as format conversions. 33--- 34---@class vim.Pos 35---@field row integer 0-based byte index. 36---@field col integer 0-based byte index. 37---@field buf integer buffer handle. 38---@field private [1] integer underlying representation of row 39---@field private [2] integer underlying representation of col 40---@field private [3] integer underlying representation of buf 41local M ={} 42 43---@private 44---@param pos vim.Pos 45---@param key any 46function M . __index ( pos , key ) 47if key == 'row' then 48return pos [ 1 ] 49elseif key == 'col' then 50return pos [ 2 ] 51elseif key == 'buf' then 52return pos [ 3 ] 53end 54 55return M [ key ] 56end 57 58---@private 59---@param pos vim.Pos 60---@param key any 61---@param value any 62function M . __newindex ( pos , key , value ) 63if key == 'row' then 64validate ( 'row' , value , 'number' ) 65pos [ 1 ] =value 66elseif key == 'col' then 67validate ( 'col' , value , 'number' ) 68pos [ 2 ] =value 69elseif key == 'buf' then 70validate ( 'buf' , value , 'number' ) 71if value == 0 then 72value =api . nvim_get_current_buf () 73end 74pos [ 3 ] =value 75else 76rawset ( pos , key , value ) 77end 78end 79 80---@package 81---@param buf integer 82---@param row integer 83---@param col integer 84function M . new ( buf , row , col ) 85validate ( 'buf' , buf , 'number' ) 86validate ( 'row' , row , 'number' ) 87validate ( 'col' , col , 'number' ) 88 89if buf == 0 then 90buf =api . nvim_get_current_buf () 91end 92 93---@type vim.Pos 94local self =setmetatable ( { 95row , 96col , 97buf , 98} , M ) 99 100return self 101end 102 103---@private 104---@param p1 vim.Pos 105---@param p2 vim.Pos 106function M . __lt ( p1 , p2 ) 107return util . cmp_pos . lt ( p1 [ 1 ], p1 [ 2 ], p2 [ 1 ], p2 [ 2 ]) 108end 109 110---@private 111---@param p1 vim.Pos 112---@param p2 vim.Pos 113function M . __le ( p1 , p2 ) 114return util . cmp_pos . le ( p1 [ 1 ], p1 [ 2 ], p2 [ 1 ], p2 [ 2 ]) 115end 116 117---@private 118---@param p1 vim.Pos 119---@param p2 vim.Pos 120function M . __eq ( p1 , p2 ) 121return util . cmp_pos . eq ( p1 [ 1 ], p1 [ 2 ], p2 [ 1 ], p2 [ 2 ]) 122end 123 124--- Converts |vim.Pos| to `lsp.Position`. 125--- 126--- Example: 127--- ```lua 128--- local pos = vim.pos(0, 3, 5) 129--- 130--- -- Convert to LSP position, you can call it in a method style. 131--- local lsp_pos = pos:to_lsp('utf-16') 132--- ``` 133---@param pos vim.Pos 134---@param position_encoding lsp.PositionEncodingKind 135---@return lsp.Position 136function M . to_lsp ( pos , position_encoding ) 137validate ( 'pos' , pos , 'table' ) 138validate ( 'position_encoding' , position_encoding , 'string' ) 139 140return util . to_lsp ( pos . buf , pos [ 1 ], pos [ 2 ], position_encoding ) 141end 142 143--- Creates a new |vim.Pos| from `lsp.Position`. 144--- 145--- Example: 146--- ```lua 147--- local lsp_pos = { 148--- line = 3, 149--- character = 5 150--- } 151--- 152--- local pos = vim.pos.lsp(0, lsp_pos, 'utf-16') 153--- ``` 154---@param buf integer 155---@param pos lsp.Position 156---@param position_encoding lsp.PositionEncodingKind 157---@return vim.Pos 158function M . lsp ( buf , pos , position_encoding ) 159validate ( 'buf' , buf , 'number' ) 160validate ( 'pos' , pos , 'table' ) 161validate ( 'position_encoding' , position_encoding , 'string' ) 162 163local row , col =util . from_lsp ( buf , pos , position_encoding ) 164return M . new ( buf , row , col ) 165end 166 167--- Converts |vim.Pos| to cursor position (see |api-indexing|). 168--- 169--- Example: 170--- ```lua 171--- local pos = vim.pos(0, 3, 5) 172--- 173--- -- Convert to cursor position, you can call it in a method style. 174--- local cursor_pos = pos:to_cursor() 175--- vim.api.nvim_win_set_cursor(0, cursor_pos) 176--- ``` 177---@param pos vim.Pos 178---@return [integer, integer] (lnum, col) tuple 179function M . to_cursor ( pos ) 180validate ( 'pos' , pos , 'table' ) 181return { util . to_mark ( pos [ 1 ], pos [ 2 ]) } 182end 183 184--- Creates a new |vim.Pos| from cursor position (see |api-indexing|). 185--- 186--- If {pos} is omitted, the first argument is treated as {win} instead of {buf}, 187--- and the current cursor position of {win} is used. If {win} is also omitted, 188--- it defaults to the current window. 189--- 190--- Example: 191--- ```lua 192--- local buf = vim.api.nvim_win_get_buf(0) 193--- local cursor_pos = vim.api.nvim_win_get_cursor(0) 194--- local pos = vim.pos.cursor(buf, cursor_pos) 195--- -- This is equivalent: 196--- local pos = vim.pos.cursor(0) 197--- ``` 198---@param buf integer 199---@param pos [integer, integer] (lnum, col) tuple 200---@return vim.Pos 201---@overload fun(win?: integer): vim.Pos 202function M . cursor ( buf , pos ) 203validate ( 'pos' , pos , 'table' , true ) 204 205if pos then 206validate ( 'buf' , buf , 'number' ) 207else 208local win =buf 209validate ( 'win' , win , 'number' , true ) 210if win == 0 or win == nil then 211win =api . nvim_get_current_win () 212end 213 214buf =api . nvim_win_get_buf ( win ) 215pos =api . nvim_win_get_cursor ( win ) 216end 217 218return M . new ( buf , util . from_mark ( pos [ 1 ], pos [ 2 ])) 219end 220 221--- Converts |vim.Pos| to mark position (see |api-indexing|). 222--- 223--- Example: 224--- ```lua 225--- local pos = vim.pos(0, 3, 5) 226--- 227--- -- Convert to mark position, you can call it in a method style. 228--- local lnum, col = pos:to_mark() 229--- vim.api.nvim_buf_set_mark(0, 'M', lnum, col) 230--- ``` 231---@param pos vim.Pos 232---@return integer lnum, integer col 233function M . to_mark ( pos ) 234validate ( 'pos' , pos , 'table' ) 235return util . to_mark ( pos [ 1 ], pos [ 2 ]) 236end 237 238--- Creates a new |vim.Pos| from mark position (see |api-indexing|). 239--- 240--- Example: 241--- ```lua 242--- local mark_info = vim.api.nvim_get_mark('M') 243--- local lnum, col, buf, name = unpack(mark_info) 244--- 245--- if lnum == 0 and col == 0 and buf == 0 then 246--- return -- mark 'M' is not set. 247--- end 248--- 249--- local pos = vim.pos.mark(buf, lnum, col) 250--- ``` 251---@param buf integer 252---@param lnum integer 253---@param col integer 254---@return vim.Pos 255function M . mark ( buf , lnum , col ) 256validate ( 'buf' , buf , 'number' ) 257validate ( 'lnum' , lnum , 'number' ) 258validate ( 'col' , col , 'number' ) 259 260return M . new ( buf , util . from_mark ( lnum , col )) 261end 262 263--- Converts |vim.Pos| to extmark position (see |api-indexing|). 264--- 265--- Example: 266--- ```lua 267--- local pos = vim.pos(0, 3, 5) 268--- 269--- -- Convert to extmark position, you can call it in a method style. 270--- local extmark_pos = pos:to_extmark() 271--- ``` 272---@param pos vim.Pos 273---@return integer row, integer col 274function M . to_extmark ( pos ) 275validate ( 'pos' , pos , 'table' ) 276return pos [ 1 ], pos [ 2 ] 277end 278 279--- Creates a new |vim.Pos| from extmark position (see |api-indexing|). 280--- 281--- Example: 282--- ```lua 283--- local pos = vim.pos.extmark(0, 3, 5) 284--- ``` 285---@param buf integer 286---@param row integer 287---@param col integer 288---@return vim.Pos 289function M . extmark ( buf , row , col ) 290validate ( 'buf' , buf , 'number' ) 291validate ( 'row' , row , 'number' ) 292validate ( 'col' , col , 'number' ) 293 294return M . new ( buf , row , col ) 295end 296 297--- Converts |vim.Pos| to buffer (bytes) offset. 298--- 299--- Example: 300--- ```lua 301--- local p1 = vim.pos(0, 3, 5) 302--- local p2 = vim.pos(0, 4, 0) 303--- 304--- -- Convert to buffer offset, you can call it in a method style. 305--- local offset1 = p1:to_offset() 306--- local offset2 = p2:to_offset() 307--- -- Can be used to calculate the distance between two locations. 308--- local distance = offset2 - offset1 309--- ``` 310---@param pos vim.Pos 311---@return integer 312function M . to_offset ( pos ) 313validate ( 'pos' , pos , 'table' ) 314return api . nvim_buf_get_offset ( pos . buf , pos [ 1 ]) + pos [ 2 ] 315end 316 317--- Creates a new |vim.Pos| from buffer (bytes) offset. 318--- 319--- Example: 320--- ```lua 321--- local offset = vim.api.nvim_buf_get_offset(0, vim.api.nvim_buf_line_count(0)) 322--- local pos = vim.pos.offset(0, offset) 323--- ``` 324---@param buf integer 325---@param offset integer 326---@return vim.Pos 327function M . offset ( buf , offset ) 328validate ( 'buf' , buf , 'number' ) 329validate ( 'offset' , offset , 'number' ) 330 331local lnum =vim . list . bisect ( 332setmetatable ( {} , { 333--- @param idx integer 334__index =function ( _ , idx ) 335return api . nvim_buf_get_offset ( buf , idx - 1 ) 336end , 337} ), 338offset , 339{ lo =1 , hi =api . nvim_buf_line_count ( buf ) + 2 , bound ='upper' } 340) - 1 341 342local row =lnum - 1 343local col =offset - api . nvim_buf_get_offset ( buf , row ) 344return M . new ( buf , row , col ) 345end 346 347-- Overload `Range.new` to allow calling this module as a function. 348setmetatable ( M , { 349__call =function ( _ , ... ) 350return M . new ( ... ) 351end , 352} ) 353---@cast M vim.Pos & fun(buf: integer, row: integer, col: integer): vim.Pos 354 355return M