robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @class (private) vim.InspectModule 2--- @overload fun(root: any, options?: vim.inspect.Opts): string 3local inspect ={ 4_VERSION ='inspect.lua 3.1.0' , 5_URL ='http://github.com/kikito/inspect.lua' , 6_DESCRIPTION ='human-readable representations of tables' , 7_LICENSE =[[ 8MIT LICENSE 9 10Copyright (c) 2013 Enrique García Cota 11 12Permission is hereby granted, free of charge, to any person obtaining a 13copy of this software and associated documentation files (the 14"Software"), to deal in the Software without restriction, including 15without limitation the rights to use, copy, modify, merge, publish, 16distribute, sublicense, and/or sell copies of the Software, and to 17permit persons to whom the Software is furnished to do so, subject to 18the following conditions: 19 20The above copyright notice and this permission notice shall be included 21in all copies or substantial portions of the Software. 22 23THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 24OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 26IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 27CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 28TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 29SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30]] , 31} 32 33inspect . KEY =setmetatable ( {} , { 34__tostring =function () 35return 'inspect.KEY' 36end , 37} ) 38inspect . METATABLE =setmetatable ( {} , { 39__tostring =function () 40return 'inspect.METATABLE' 41end , 42} ) 43 44local tostring =tostring 45local rep =string . rep 46local match =string . match 47local char =string . char 48local gsub =string . gsub 49local fmt =string . format 50 51local sbavailable , stringbuffer =pcall ( require , 'string.buffer' ) 52local buffnew 53local puts 54local render 55 56if sbavailable then 57buffnew =stringbuffer . new 58--- @param buf string.buffer 59--- @param str string 60puts =function ( buf , str ) 61buf : put ( str ) 62end 63--- @param buf string.buffer 64render =function ( buf ) 65return buf : get () 66end 67else 68buffnew =function () 69return { n =0 } 70end 71--- @param buf { [integer]: string, n: integer } 72--- @param str string 73puts =function ( buf , str ) 74buf . n =buf . n + 1 75buf [ buf . n ] =str 76end 77--- @param buf string[] 78render =function ( buf ) 79return table . concat ( buf ) 80end 81end 82 83-- The constructor, writer and renderer always use the same buffer backend. 84--- @cast puts fun(buf: string.buffer|{ [integer]: string, n: integer }, str: string) 85--- @cast render fun(buf: string.buffer|{ [integer]: string, n: integer }): string 86 87local _rawget 88if rawget then 89_rawget =rawget 90else 91--- @generic K, V 92--- @param t table<K,V> 93--- @param k K 94--- @return V? 95_rawget =function ( t , k ) 96return t [ k ] 97end 98end 99 100--- @generic K, V 101--- @param t table<K,V> 102--- @return (fun(t: table<K,V>, key?: K): K?, V?), table<K,V>, nil 103local function rawpairs ( t ) 104return next , t , nil 105end 106 107--- Apostrophizes the string if it has quotes, but not aphostrophes 108--- Otherwise, it returns a regular quoted string 109--- @param str string 110local function smartQuote ( str ) 111if match ( str , '"' ) and not match ( str , "'" ) then 112return "'" .. str .. "'" 113end 114return '"' .. gsub ( str , '"' , '\\"' ) .. '"' 115end 116 117-- \a => '\\a', \0 => '\\0', 31 => '\31' 118local shortControlCharEscapes ={ 119[ '\a' ] ='\\a' , 120[ '\b' ] ='\\b' , 121[ '\f' ] ='\\f' , 122[ '\n' ] ='\\n' , 123[ '\r' ] ='\\r' , 124[ '\t' ] ='\\t' , 125[ '\v' ] ='\\v' , 126[ '\127' ] ='\\127' , 127} 128local longControlCharEscapes ={ [ '\127' ] ='\127' } 129for i =0 , 31 do 130local ch =char ( i ) 131if not shortControlCharEscapes [ ch ] then 132shortControlCharEscapes [ ch ] ='\\' .. i 133longControlCharEscapes [ ch ] =fmt ( '\\%03d' , i ) 134end 135end 136 137--- @param str string 138local function escape ( str ) 139return ( 140gsub ( 141gsub ( gsub ( str , '\\' , '\\\\' ), '(%c)%f[0-9]' , longControlCharEscapes ), 142'%c' , 143shortControlCharEscapes 144) 145) 146end 147 148local luaKeywords ={} 149for k in 150( [[ and break do else elseif end false for function goto if 151in local nil not or repeat return then true until while 152]] ): gmatch ( '%w+' ) 153do 154luaKeywords [ k ] =true 155end 156 157--- @param str any 158local function isIdentifier ( str ) 159return type ( str ) == 'string' 160-- identifier must start with a letter and underscore, and be followed by letters, numbers, and underscores 161and not not str : match ( '^[_%a][_%a%d]*$' ) 162-- lua keywords are not valid identifiers 163and not luaKeywords [ str ] 164end 165 166local flr =math . floor 167 168--- @param k any 169--- @param sequenceLength integer 170local function isSequenceKey ( k , sequenceLength ) 171return type ( k ) == 'number' and flr ( k ) == k and 1 <= k and k <= sequenceLength 172end 173 174local defaultTypeOrders ={ 175[ 'number' ] =1 , 176[ 'boolean' ] =2 , 177[ 'string' ] =3 , 178[ 'table' ] =4 , 179[ 'function' ] =5 , 180[ 'userdata' ] =6 , 181[ 'thread' ] =7 , 182} 183 184--- @param a any 185--- @param b any 186local function sortKeys ( a , b ) 187local ta , tb =type ( a ), type ( b ) 188 189-- strings and numbers are sorted numerically/alphabetically 190if ta == tb and ( ta == 'string' or ta == 'number' ) then 191return a < b 192end 193 194local dta =defaultTypeOrders [ ta ] or 100 195local dtb =defaultTypeOrders [ tb ] or 100 196-- Two default types are compared according to the defaultTypeOrders table 197 198-- custom types are sorted out alphabetically 199return dta == dtb and ta < tb or dta < dtb 200end 201 202--- @generic K 203--- @param t table<K,any> 204--- @return K[], integer, integer 205local function getKeys ( t ) 206local seqLen =1 207while _rawget ( t , seqLen ) ~= nil do 208seqLen =seqLen + 1 209end 210seqLen =seqLen - 1 211 212local keys , keysLen ={} , 0 213for k in rawpairs ( t ) do 214if not isSequenceKey ( k , seqLen ) then 215keysLen =keysLen + 1 216keys [ keysLen ] =k 217end 218end 219table . sort ( keys , sortKeys ) 220return keys , keysLen , seqLen 221end 222 223--- @param x any 224--- @param cycles table<table,integer> 225--- @param depth number 226local function countCycles ( x , cycles , depth ) 227if type ( x ) == 'table' then 228if cycles [ x ] then 229cycles [ x ] =cycles [ x ] + 1 230else 231cycles [ x ] =1 232if depth > 0 then 233for k , v in rawpairs ( x ) do 234countCycles ( k , cycles , depth - 1 ) 235countCycles ( v , cycles , depth - 1 ) 236end 237countCycles ( getmetatable ( x ), cycles , depth - 1 ) 238end 239end 240end 241end 242 243--- @generic T 244--- @param path T[] 245--- @param a T 246--- @param b? T 247--- @return T[] 248local function makePath ( path , a , b ) 249local newPath ={} 250local len =# path 251for i =1 , len do 252newPath [ i ] =path [ i ] 253end 254 255newPath [ len + 1 ] =a 256newPath [ len + 2 ] =b 257 258return newPath 259end 260 261--- @param process fun(item: any, path: any[]): any 262--- @param item any 263--- @param path any[] 264--- @param visited table<any,any> 265local function processRecursive ( process , item , path , visited ) 266if item == nil then 267return nil 268end 269if visited [ item ] then 270return visited [ item ] 271end 272 273local processed =process ( item , path ) 274if type ( processed ) == 'table' then 275local processedCopy ={} 276visited [ item ] =processedCopy 277local processedKey 278 279for k , v in rawpairs ( processed ) do 280processedKey =processRecursive ( process , k , makePath ( path , k , inspect . KEY ), visited ) 281if processedKey ~= nil then 282processedCopy [ processedKey ] = 283processRecursive ( process , v , makePath ( path , processedKey ), visited ) 284end 285end 286 287local mt = 288processRecursive ( process , getmetatable ( processed ), makePath ( path , inspect . METATABLE ), visited ) 289if type ( mt ) ~= 'table' then 290mt =nil 291end 292setmetatable ( processedCopy , mt ) 293processed =processedCopy 294end 295return processed 296end 297 298--- @class (private) vim.inspect.Inspector 299--- @field buf string.buffer|{ [integer]: string, n: integer } 300--- @field ids table<any,integer> 301--- @field cycles table<table,integer> 302--- @field depth number 303--- @field level integer 304--- @field newline string 305--- @field indent string 306local Inspector ={} 307 308local Inspector_mt ={ __index =Inspector } 309 310--- @param inspector vim.inspect.Inspector 311local function tabify ( inspector ) 312puts ( inspector . buf , inspector . newline .. rep ( inspector . indent , inspector . level )) 313end 314 315--- @nodoc 316--- @param v any 317function Inspector : getId ( v ) 318local id =self . ids [ v ] 319local ids =self . ids 320if not id then 321local tv =type ( v ) 322id =( ids [ tv ] or 0 ) + 1 323ids [ v ], ids [ tv ] =id , id 324end 325return tostring ( id ) 326end 327 328--- @nodoc 329--- @param v any 330function Inspector : putValue ( v ) 331local buf =self . buf 332local tv =type ( v ) 333if tv == 'string' then 334puts ( buf , smartQuote ( escape ( v ))) 335elseif 336tv == 'number' 337or tv == 'boolean' 338or tv == 'nil' 339or tv == 'cdata' 340or tv == 'ctype' 341or ( vim and v == vim . NIL ) 342then 343puts ( buf , tostring ( v )) 344elseif tv == 'table' and not self . ids [ v ] then 345local t =v 346 347if t == inspect . KEY or t == inspect . METATABLE then 348puts ( buf , tostring ( t )) 349elseif self . level >= self . depth then 350puts ( buf , '{...}' ) 351else 352if self . cycles [ t ] > 1 then 353puts ( buf , fmt ( '<%d>' , self : getId ( t ))) 354end 355 356local keys , keysLen , seqLen =getKeys ( t ) 357local mt =getmetatable ( t ) 358 359if vim and seqLen == 0 and keysLen == 0 and mt == vim . _empty_dict_mt then 360puts ( buf , tostring ( t )) 361return 362end 363 364puts ( buf , '{' ) 365self . level =self . level + 1 366 367for i =1 , seqLen + keysLen do 368if i > 1 then 369puts ( buf , ',' ) 370end 371if i <= seqLen then 372puts ( buf , ' ' ) 373self : putValue ( t [ i ]) 374else 375local k =keys [ i - seqLen ] 376tabify ( self ) 377if isIdentifier ( k ) then 378puts ( buf , k ) 379else 380puts ( buf , '[' ) 381self : putValue ( k ) 382puts ( buf , ']' ) 383end 384puts ( buf , ' = ' ) 385self : putValue ( t [ k ]) 386end 387end 388 389if type ( mt ) == 'table' then 390if seqLen + keysLen > 0 then 391puts ( buf , ',' ) 392end 393tabify ( self ) 394puts ( buf , '<metatable> = ' ) 395self : putValue ( mt ) 396end 397 398self . level =self . level - 1 399 400if keysLen > 0 or type ( mt ) == 'table' then 401tabify ( self ) 402elseif seqLen > 0 then 403puts ( buf , ' ' ) 404end 405 406puts ( buf , '}' ) 407end 408else 409puts ( buf , fmt ( '<%s %d>' , tv , self : getId ( v ))) 410end 411end 412 413--- @inlinedoc 414--- @class vim.inspect.Opts 415--- 416--- Maximum table nesting depth. Deeper tables are shown as `{...}`. 417--- (default: unlimited) 418--- @field depth? integer 419--- 420--- String used for line breaks. 421--- (default: `"\n"`) 422--- @field newline? string 423--- 424--- String repeated for each level of indentation. 425--- (default: `" "`) 426--- @field indent? string 427--- 428--- Transforms values, keys, and metatables before formatting. Return a replacement value, 429--- or `nil` to omit a table entry. {path} lists the keys leading to {item}, with 430--- `vim.inspect.KEY` appended for keys or `vim.inspect.METATABLE` for metatables. 431--- The root has an empty path. 432--- @field process? fun(item: any, path: any[]): any 433 434--- @nodoc 435--- @param root any 436--- @param options? vim.inspect.Opts 437--- @return string 438function inspect . inspect ( root , options ) 439options =options or {} 440 441local depth =options . depth or math . huge 442local newline =options . newline or '\n' 443local indent =options . indent or ' ' 444local process =options . process 445 446if process then 447root =processRecursive ( process , root , {} , {} ) 448end 449 450local cycles ={} 451countCycles ( root , cycles , depth ) 452 453local inspector =setmetatable ( { 454buf =buffnew (), 455ids ={} , 456cycles =cycles , 457depth =depth , 458level =0 , 459newline =newline , 460indent =indent , 461} , Inspector_mt ) 462 463inspector : putValue ( root ) 464 465return render ( inspector . buf ) 466end 467 468setmetatable ( inspect , { 469--- Gets a human-readable representation of the given object. 470--- 471--- @see |vim.print()| 472--- @see https://github.com/kikito/inspect.lua 473--- @see https://github.com/mpeterv/vinspect 474--- @param root any 475--- @param options? vim.inspect.Opts 476--- @return string 477__call =function ( _ , root , options ) 478return inspect . inspect ( root , options ) 479end , 480} ) 481 482return inspect