robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1---@brief 2--- Unlike `vim.pos`, this module is used to provide utility functions 3--- for unpacked `row`, `col`. 4--- 5--- The variable names have some implications: 6--- 7--- - `row` is used to represent a 0-based index of a line. 8--- - `lnum` is used to represent a 1-based index of a line, short for "line number". 9 10local api =vim . api 11 12local M ={} 13 14---@param a_row integer 15---@param a_col integer 16---@param b_row integer 17---@param b_col integer 18---@return integer 19--- 1: a > b 20--- 0: a == b 21--- -1: a < b 22local function cmp_pos ( a_row , a_col , b_row , b_col ) 23if a_row == b_row then 24if a_col > b_col then 25return 1 26elseif a_col < b_col then 27return - 1 28else 29return 0 30end 31elseif a_row > b_row then 32return 1 33end 34 35return - 1 36end 37 38---@type table<'lt'|'le'|'gt'|'ge'|'eq'|'ne', fun(a_row: integer, a_col: integer, b_row: integer, b_col: integer): boolean> 39M . cmp_pos ={ 40lt =function ( ... ) 41return cmp_pos ( ... ) == - 1 42end , 43le =function ( ... ) 44return cmp_pos ( ... ) ~= 1 45end , 46gt =function ( ... ) 47return cmp_pos ( ... ) == 1 48end , 49ge =function ( ... ) 50return cmp_pos ( ... ) ~= - 1 51end , 52eq =function ( ... ) 53return cmp_pos ( ... ) == 0 54end , 55ne =function ( ... ) 56return cmp_pos ( ... ) ~= 0 57end , 58} 59 60setmetatable ( M . cmp_pos , { __call =cmp_pos } ) 61 62--- Gets the zero-indexed lines from the given buffer. 63--- Works on unloaded buffers by reading the file and bypass buf reading events. 64--- Falls back to loading the buffer and nvim_buf_get_lines for buffers with non-file URI. 65--- 66---@param buf integer buffer handle to get the lines from 67---@param rows integer[] zero-indexed line numbers 68---@return table<integer, string> # a table mapping rows to lines 69function M . get_lines ( buf , rows ) 70local function buf_lines () 71local row_line ={} --- @type table<integer,string> 72for _ , row in ipairs ( rows ) do 73row_line [ row ] =api . nvim_buf_get_lines ( buf , row , row + 1 , false )[ 1 ] or '' 74end 75return row_line 76end 77 78-- Use loaded buffers if available. 79if vim . fn . bufloaded ( buf ) == 1 then 80return buf_lines () 81end 82 83-- Load the buffer if this is not a file URI. 84-- Custom language server protocol extensions can result in servers sending 85-- URIs with custom schemes. Plugins are able to load these via `BufReadCmd` autocmds. 86if not vim . startswith ( vim . uri_from_bufnr ( buf ), 'file://' ) then 87vim . fn . bufload ( buf ) 88return buf_lines () 89end 90 91local row_line ={} --- @type table<integer, string> 92for _ , row in pairs ( rows ) do 93row_line [ row ] ='' 94end 95 96-- Get the data from the file. 97local success , data =pcall ( vim . fn . readblob , api . nvim_buf_get_name ( buf )) 98if not success then 99return row_line 100end 101 102--- @cast data string 103local need =vim . tbl_count ( row_line ) 104local row =0 105for line in string . gmatch ( data , '([^\n]*)\n?' ) do 106if row_line [ row ] then 107row_line [ row ] =line 108need =need - 1 109if need == 0 then 110break 111end 112end 113row =row + 1 114end 115return row_line 116end 117 118--- Gets the zero-indexed line from the given buffer. 119--- Works on unloaded buffers by reading the file and bypass buf reading events. 120--- Falls back to loading the buffer and nvim_buf_get_lines for buffers with non-file URI. 121--- 122---@param buf integer buffer handle to get the lines from 123---@param row integer zero-indexed line number 124---@return string the line at row in filename 125function M . get_line ( buf , row ) 126return M . get_lines ( buf , { row } )[ row ] 127end 128 129---@param buf integer 130---@param row integer 131---@param col integer 132---@param position_encoding lsp.PositionEncodingKind 133function M . to_lsp ( buf , row , col , position_encoding ) 134-- When on the first character, 135-- we can ignore the difference between byte and character. 136if col > 0 then 137col =vim . str_utfindex ( M . get_line ( buf , row ), position_encoding , col , false ) 138elseif col == 0 and row == api . nvim_buf_line_count ( buf ) and not vim . bo [ buf ]. endofline then 139-- Some LSP servers reject ranges that end at the virtual EOF position 140-- (i.e., `[line_count, 0]`) when the buffer has no trailing newline. 141-- Normalize such positions to the end of the last real line instead. 142row =row - 1 143col =vim . str_utfindex ( M . get_line ( buf , row ), position_encoding ) 144end 145---@type lsp.Position 146return { line =row , character =col } 147end 148 149---@param buf integer 150---@param position lsp.Position 151---@param position_encoding lsp.PositionEncodingKind 152function M . from_lsp ( buf , position , position_encoding ) 153local row , col =position . line , position . character 154-- When on the first character, 155-- we can ignore the difference between byte and character. 156if col > 0 then 157-- `strict_indexing` is disabled, because LSP responses are asynchronous, 158-- and the buffer content may have changed, causing out-of-bounds errors. 159col =vim . str_byteindex ( M . get_line ( buf , row ), position_encoding , col , false ) 160end 161return row , col 162end 163 164---@param row integer 165---@param col integer 166---@return integer lnum, integer col 167function M . to_mark ( row , col ) 168return row + 1 , col 169end 170 171---@param lnum integer 172---@param col integer 173---@return integer row, integer col 174function M . from_mark ( lnum , col ) 175return lnum - 1 , col 176end 177 178return M