robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Text processing functions. 2 3local M ={} 4 5--- Optional parameters: 6--- @class vim.text.diff.Opts 7--- @inlinedoc 8--- 9--- Invoked for each hunk in the diff. Return a negative number 10--- to cancel the callback for any remaining hunks. 11--- Arguments: 12--- - `start_a` (`integer`): Start line of hunk in {a}. 13--- - `count_a` (`integer`): Hunk size in {a}. 14--- - `start_b` (`integer`): Start line of hunk in {b}. 15--- - `count_b` (`integer`): Hunk size in {b}. 16--- @field on_hunk? fun(start_a: integer, count_a: integer, start_b: integer, count_b: integer): integer? 17--- 18--- Form of the returned diff: 19--- - `unified`: String in unified format. 20--- - `indices`: Array of hunk locations. 21--- Note: This option is ignored if `on_hunk` is used. 22--- (default: `'unified'`) 23--- @field result_type? 'unified'|'indices' 24--- 25--- Run linematch on the resulting hunks from xdiff. When integer, only hunks 26--- upto this size in lines are run through linematch. 27--- Requires `result_type = indices`, ignored otherwise. 28--- @field linematch? boolean|integer 29--- 30--- Diff algorithm to use. Values: 31--- - `myers`: the default algorithm 32--- - `minimal`: spend extra time to generate the smallest possible diff 33--- - `patience`: patience diff algorithm 34--- - `histogram`: histogram diff algorithm 35--- (default: `'myers'`) 36--- @field algorithm? 'myers'|'minimal'|'patience'|'histogram' 37--- @field ctxlen? integer Context length 38--- @field interhunkctxlen? integer Inter hunk context length 39--- @field ignore_whitespace? boolean Ignore whitespace 40--- @field ignore_whitespace_change? boolean Ignore whitespace change 41--- @field ignore_whitespace_change_at_eol? boolean Ignore whitespace change at end-of-line. 42--- @field ignore_cr_at_eol? boolean Ignore carriage return at end-of-line 43--- @field ignore_blank_lines? boolean Ignore blank lines 44--- @field indent_heuristic? boolean Use the indent heuristic for the internal diff library. 45 46-- luacheck: no unused args 47 48--- Run diff on strings {a} and {b}. Any indices returned by this function, 49--- either directly or via callback arguments, are 1-based. 50--- 51--- Examples: 52--- 53--- ```lua 54--- vim.text.diff('a\n', 'b\nc\n') 55--- -- => 56--- -- @@ -1 +1,2 @@ 57--- -- -a 58--- -- +b 59--- -- +c 60--- 61--- vim.text.diff('a\n', 'b\nc\n', {result_type = 'indices'}) 62--- -- => 63--- -- { 64--- -- {1, 1, 1, 2} 65--- -- } 66--- ``` 67--- 68---@diagnostic disable-next-line: undefined-doc-param 69---@param a string First string to compare 70---@diagnostic disable-next-line: undefined-doc-param 71---@param b string Second string to compare 72---@diagnostic disable-next-line: undefined-doc-param 73---@param opts? vim.text.diff.Opts 74---@return string|integer[][]? # See {opts.result_type}. `nil` if {opts.on_hunk} is given. 75function M . diff ( ... ) 76---@diagnostic disable-next-line: deprecated 77return vim . diff ( ... ) 78end 79 80local alphabet ='0123456789ABCDEF' 81local atoi ={} ---@type table<string, integer> 82local itoa ={} ---@type table<integer, string> 83do 84for i =1 , # alphabet do 85local char =alphabet : sub ( i , i ) 86itoa [ i - 1 ] =char 87atoi [ char ] =i - 1 88atoi [ char : lower ()] =i - 1 89end 90end 91 92--- Hex encode a string. 93--- 94--- @param str string String to encode 95--- @return string : Hex encoded string 96function M . hexencode ( str ) 97local enc ={} ---@type string[] 98for i =1 , # str do 99local byte =str : byte ( i ) 100enc [ 2 * i - 1 ] =itoa [ math . floor ( byte / 16 )] 101enc [ 2 * i ] =itoa [ byte % 16 ] 102end 103return table . concat ( enc ) 104end 105 106--- Hex decode a string. 107--- 108--- @param enc string String to decode 109--- @return string? : Decoded string 110--- @return string? : Error message, if any 111function M . hexdecode ( enc ) 112if # enc % 2 ~= 0 then 113return nil , 'string must have an even number of hex characters' 114end 115 116local str ={} ---@type string[] 117for i =1 , # enc , 2 do 118local u =atoi [ enc : sub ( i , i )] 119local l =atoi [ enc : sub ( i + 1 , i + 1 )] 120if not u or not l then 121return nil , 'string must contain only hex characters' 122end 123str [( i + 1 ) / 2 ] =string . char ( u * 16 + l ) 124end 125return table . concat ( str ), nil 126end 127 128--- Sets the indent (i.e. the common leading whitespace) of non-empty lines in `text` to `size` 129--- spaces/tabs. 130--- 131--- Indent is calculated by number of consecutive indent chars. 132--- - The first indented, non-empty line decides the indent char (space/tab): 133--- - `SPC SPC TAB …` = two-space indent. 134--- - `TAB SPC …` = one-tab indent. 135--- - Set `opts.expandtab` to treat tabs as spaces. 136--- 137--- To "dedent" (remove the common indent), pass `size=0`: 138--- ```lua 139--- vim.print(vim.text.indent(0, ' a\n b\n')) 140--- ``` 141--- 142--- To adjust relative-to an existing indent, call indent() twice: 143--- ```lua 144--- local indented, old_indent = vim.text.indent(0, ' a\n b\n') 145--- indented = vim.text.indent(old_indent + 2, indented) 146--- vim.print(indented) 147--- ``` 148--- 149--- To ignore the final, blank line when calculating the indent, use gsub() before calling indent(): 150--- ```lua 151--- local text = ' a\n b\n ' 152--- vim.print(vim.text.indent(0, (text:gsub('\n[\t ]+\n?$', '\n')))) 153--- ``` 154--- 155--- @param size integer Number of spaces. 156--- @param text string Text to indent. 157--- @param opts? { expandtab?: integer } 158--- @return string # Indented text. 159--- @return integer # Indent size _before_ modification. 160function M . indent ( size , text , opts ) 161vim . validate ( 'size' , size , 'number' ) 162vim . validate ( 'text' , text , 'string' ) 163 164-- TODO(justinmk): `opts.prefix`, `predicate` like python https://docs.python.org/3/library/textwrap.html 165vim . validate ( 'opts' , opts , 'table' , true ) 166opts =opts or {} 167vim . validate ( 'opts.expandtab' , opts . expandtab , 'number' , true ) 168 169local tabspaces =opts . expandtab and ( ' ' ): rep ( opts . expandtab ) or nil 170 171--- Minimum common indent shared by all lines. 172local old_indent --- @type integer? 173local prefix =tabspaces and ' ' or nil -- Indent char (space or tab). 174--- Check all non-empty lines, capturing leading whitespace (if any). 175--- @diagnostic disable-next-line: no-unknown 176for line_ws , extra in text : gmatch ( '([\t ]*)([^\n]+)' ) do 177line_ws =tabspaces and line_ws : gsub ( '[\t]' , tabspaces ) or line_ws 178-- XXX: blank line will miss the last whitespace char in `line_ws`, so we need to check `extra`. 179line_ws =line_ws .. ( extra : match ( '^%s+$' ) or '' ) 180if 0 == # line_ws then 181-- Optimization: If any non-empty line has indent=0, there is no common indent. 182old_indent =0 183break 184end 185prefix =prefix and prefix or line_ws : sub ( 1 , 1 ) 186local _ , end_ =line_ws : find ( '^[' .. prefix .. ']+' ) 187old_indent =math . min ( old_indent or math . huge , end_ or 0 ) --[[@as integer?]] 188end 189-- Default to 0 if all lines are empty. 190old_indent =old_indent or 0 191prefix =prefix and prefix or ' ' 192 193if not opts . expandtab and old_indent == size then 194-- Optimization: if the indent is the same, return the text unchanged. 195return text , old_indent 196end 197 198local new_indent =prefix : rep ( size ) 199 200--- Replaces indentation of a line. 201--- @param line string 202local function replace_line ( line ) 203-- Match the existing indent exactly; avoid over-matching any following whitespace. 204local pat =prefix : rep ( old_indent ) 205-- Expand tabs before replacing indentation. 206line =not tabspaces and line 207or line : gsub ( '^[\t ]+' , function ( s ) 208return s : gsub ( '\t' , assert ( tabspaces )) 209end ) 210-- Text following the indent. 211local line_text =line : match ( '^' .. pat .. '(.*)' ) or line 212return new_indent .. line_text 213end 214 215return ( text : gsub ( '[^\n]+' , replace_line )), old_indent 216end 217 218return M