robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
9e80f63c30
master
1local M ={} 2 3local SUBTBL ={ 4'\\000' , 5'\\001' , 6'\\002' , 7'\\003' , 8'\\004' , 9'\\005' , 10'\\006' , 11'\\007' , 12'\\008' , 13'\\t' , 14'\\n' , 15'\\011' , 16'\\012' , 17'\\r' , 18'\\014' , 19'\\015' , 20'\\016' , 21'\\017' , 22'\\018' , 23'\\019' , 24'\\020' , 25'\\021' , 26'\\022' , 27'\\023' , 28'\\024' , 29'\\025' , 30'\\026' , 31'\\027' , 32'\\028' , 33'\\029' , 34'\\030' , 35'\\031' , 36} 37 38--- @param v any 39--- @return string 40local function format_float ( v ) 41-- On windows exponent appears to have three digits and not two 42local ret =( '%.6e' ): format ( v ) 43local l , f , es , e =ret : match ( '^(%-?%d)%.(%d+)e([+%-])0*(%d%d+)$' ) 44return l .. '.' .. f .. 'e' .. es .. e 45end 46 47-- Formats Lua value `v`. 48-- 49-- TODO(justinmk): redundant with vim.inspect() ? 50-- 51-- "Nice table formatting similar to screen:snapshot_util()". 52-- Commit: 520c0b91a528 53function M . format_luav ( v , indent , opts ) 54opts =opts or {} 55local linesep ='\n' 56local next_indent_arg =nil 57local indent_shift =opts . indent_shift or ' ' 58local next_indent 59local nl ='\n' 60if indent == nil then 61indent ='' 62linesep ='' 63next_indent ='' 64nl =' ' 65else 66next_indent_arg =indent .. indent_shift 67next_indent =indent .. indent_shift 68end 69local ret ='' 70if type ( v ) == 'string' then 71if opts . literal_strings then 72ret =v 73else 74local quote =opts . dquote_strings and '"' or "'" 75ret =quote 76.. tostring ( v ) 77: gsub ( opts . dquote_strings and '["\\]' or "['\\]" , '\\%0' ) 78: gsub ( '[%z\1-\31]' , function ( match ) 79return SUBTBL [ match : byte () + 1 ] 80end ) 81.. quote 82end 83elseif type ( v ) == 'table' then 84if v == vim . NIL then 85ret ='REMOVE_THIS' 86else 87local processed_keys ={} 88ret ='{' .. linesep 89local non_empty =false 90local format_luav =M . format_luav 91for i , subv in ipairs ( v ) do 92ret =( '%s%s%s,%s' ): format ( ret , next_indent , format_luav ( subv , next_indent_arg , opts ), nl ) 93processed_keys [ i ] =true 94non_empty =true 95end 96for k , subv in pairs ( v ) do 97if not processed_keys [ k ] then 98if type ( k ) == 'string' and k : match ( '^[a-zA-Z_][a-zA-Z0-9_]*$' ) then 99ret =ret .. next_indent .. k .. ' = ' 100else 101ret =( '%s%s[%s] = ' ): format ( ret , next_indent , format_luav ( k , nil , opts )) 102end 103ret =ret .. format_luav ( subv , next_indent_arg , opts ) .. ',' .. nl 104non_empty =true 105end 106end 107if nl == ' ' and non_empty then 108ret =ret : sub ( 1 , - 3 ) 109end 110ret =ret .. indent .. '}' 111end 112elseif type ( v ) == 'number' then 113if v % 1 == 0 then 114ret =( '%d' ): format ( v ) 115else 116ret =format_float ( v ) 117end 118elseif type ( v ) == 'nil' then 119ret ='nil' 120elseif type ( v ) == 'boolean' then 121ret =( v and 'true' or 'false' ) 122else 123( type ( v )) 124-- Not implemented yet 125assert ( false ) 126end 127return ret 128end 129 130-- Like Python repr(), "{!r}".format(s) 131-- 132-- Commit: 520c0b91a528 133function M . format_string ( fmt , ... ) 134local i =0 135local args ={ ... } 136local function getarg () 137i =i + 1 138return args [ i ] 139end 140local ret =fmt : gsub ( '%%[0-9*]*%.?[0-9*]*[cdEefgGiouXxqsr%%]' , function ( match ) 141local subfmt =match : gsub ( '%*' , function () 142return tostring ( getarg ()) 143end ) 144local arg =nil 145if subfmt : sub ( - 1 ) ~= '%' then 146arg =getarg () 147end 148if subfmt : sub ( - 1 ) == 'r' or subfmt : sub ( - 1 ) == 'q' then 149-- %r is like built-in %q, but it is supposed to single-quote strings and 150-- not double-quote them, and also work not only for strings. 151-- Builtin %q is replaced here as it gives invalid and inconsistent with 152-- luajit results for e.g. "\e" on lua: luajit transforms that into `\27`, 153-- lua leaves as-is. 154arg =M . format_luav ( arg , nil , { dquote_strings =( subfmt : sub ( - 1 ) == 'q' ) } ) 155subfmt =subfmt : sub ( 1 , - 2 ) .. 's' 156end 157if subfmt == '%e' then 158return format_float ( arg ) 159else 160return subfmt : format ( arg ) 161end 162end ) 163return ret 164end 165 166return M