1
0

Changed all function signatures to exclude the old options arguments..

This commit is contained in:
Jon Michael Aanes 2017-06-05 23:24:24 +02:00
parent 7005a080c0
commit 48ca04bcb8
3 changed files with 54 additions and 38 deletions

View File

@ -60,11 +60,13 @@ end
local function get_function_info (f) local function get_function_info (f)
-- NOTE: Works best in LuaJIT or Lua 5.2+ -- NOTE: Works best in LuaJIT or Lua 5.2+
-- Regarding get-info: -- Regarding get-info:
-- * No need to includ 'f'. Function is already known -- * No need to includ 'f'. Function is already known
-- * No need to include 'L' (active lines) option. Ignored -- * No need to include 'L' (active lines) option. Ignored
-- * No need to include 'n' (name and namewhat). Won't work. -- * No need to include 'n' (name and namewhat). Won't work.
assert(type(f) == 'function')
local info = debug.getinfo(f, 'Su') local info = debug.getinfo(f, 'Su')
info.params = {} info.params = {}
info.ups = {} info.ups = {}
@ -133,6 +135,9 @@ local function width_of_strings_in_l (l, start_i, end_i)
end end
local function add_indent_to_string (str, indent) local function add_indent_to_string (str, indent)
assert(type(str) == 'string')
assert(type(indent) == 'string')
local l = {} local l = {}
for line in string.gmatch(str, '\n', true) do for line in string.gmatch(str, '\n', true) do
l[#l+1] = indent l[#l+1] = indent
@ -143,7 +148,10 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local function format_function_with_closure (value, options, depth, l, format_value) local function format_function_with_closure (value, depth, l, format_value)
assert(type(value) == 'function')
assert(type(depth) == 'number' and type(l) == 'table' and type(format_value) == 'function')
local info = get_function_info(value) local info = get_function_info(value)
local function_str = nil local function_str = nil
@ -159,7 +167,7 @@ local function format_function_with_closure (value, options, depth, l, format_va
l[#l+1] = 'local ' l[#l+1] = 'local '
l[#l+1] = tostring(k) l[#l+1] = tostring(k)
l[#l+1] = ' = ' l[#l+1] = ' = '
format_value(v, options, depth + 1, l) format_value(v, depth + 1, l)
l[#l+1] = '; ' l[#l+1] = '; '
end end
-- Return function -- Return function
@ -169,11 +177,14 @@ local function format_function_with_closure (value, options, depth, l, format_va
if info.nups > 0 then l[#l+1] = ' end)()' end if info.nups > 0 then l[#l+1] = ' end)()' end
end end
return function (value, options, depth, l, format_value) return function (value, depth, l, format_value)
assert(type(value) == 'function')
assert(type(depth) == 'number' and type(l) == 'table' and type(format_value) == 'function')
local info = get_function_info(value) local info = get_function_info(value)
if options.include_closure and not info.builtin then if l.options.include_closure and not info.builtin then
return format_function_with_closure(value, options, depth, l, format_value) return format_function_with_closure(value, depth, l, format_value)
end end
if info.defined_how == 'string' then if info.defined_how == 'string' then
@ -182,7 +193,7 @@ return function (value, options, depth, l, format_value)
return; return;
end end
if info.builtin and options.short_builtins then if info.builtin and l.options.short_builtins then
l[#l+1] = info.name l[#l+1] = info.name
return; return;
end end
@ -200,14 +211,14 @@ return function (value, options, depth, l, format_value)
l[#l+1] = { 'align', 'func_def', width_of_strings_in_l(l, top_before) } l[#l+1] = { 'align', 'func_def', width_of_strings_in_l(l, top_before) }
-- Cleanup and finish -- Cleanup and finish
if not options.more_function_info or depth ~= 0 then if not l.options.more_function_info or depth ~= 0 then
l[#l+1] = ' ... end' l[#l+1] = ' ... end'
return; return;
end end
-- More info! -- -- More info! --
local indent = '\n' .. options.indent local indent = '\n' .. l.options.indent
-- Name -- Name
if info.name then if info.name then
@ -241,16 +252,16 @@ return function (value, options, depth, l, format_value)
if info.nups > 0 and not info.builtin then if info.nups > 0 and not info.builtin then
l[#l+1] = indent l[#l+1] = indent
l[#l+1] = '-- up_values: ' l[#l+1] = '-- up_values: '
format_value(info.ups, options, depth + 1, l) format_value(info.ups, depth + 1, l)
end end
if options._all_function_info then if l.options._all_function_info then
-- NOTE: This is for testing/debugging/experimentation purposes, and is -- NOTE: This is for testing/debugging/experimentation purposes, and is
-- not designed to be pretty. -- not designed to be pretty.
l[#l+1] = indent l[#l+1] = indent
l[#l+1] = '--[[ Function Body:\n\t' l[#l+1] = '--[[ Function Body:\n\t'
l[#l+1] = add_indent_to_string(get_function_str_from_file(info.short_src, info.linedefined, info.lastlinedefined), options.indent) l[#l+1] = add_indent_to_string(get_function_str_from_file(info.short_src, info.linedefined, info.lastlinedefined), l.options.indent)
l[#l+1] = indent l[#l+1] = indent
l[#l+1] = '--]]' l[#l+1] = '--]]'
@ -258,7 +269,7 @@ return function (value, options, depth, l, format_value)
l[#l+1] = '--[[\n\tNative repr:' l[#l+1] = '--[[\n\tNative repr:'
l[#l+1] = tostring(value) l[#l+1] = tostring(value)
l[#l+1] = indent l[#l+1] = indent
format_value(info, options, depth + 1, l) format_value(info, depth + 1, l)
l[#l+1] = indent l[#l+1] = indent
l[#l+1] = '--]]' l[#l+1] = '--]]'
end end

View File

@ -162,7 +162,9 @@ function format_num (n, shorthand)
return shortest return shortest
end end
return function (value, options, depth, l) return function (value, depth, l)
assert(type(value) == 'number')
assert(type(depth) == 'number' and type(l) == 'table')
-- TODO: Add support for more relaxed representations. -- TODO: Add support for more relaxed representations.
l[#l+1] = format_num(value, CAN_USE_SHORTHAND and options.math_shorthand) l[#l+1] = format_num(value, CAN_USE_SHORTHAND and l.options.math_shorthand)
end end

View File

@ -17,8 +17,8 @@ do
-- Load number and function formatting -- Load number and function formatting
-- Will use a very simple number formatting, if number.lua is not available. -- Will use a very simple number formatting, if number.lua is not available.
-- Will use a very simple function formatting, if function.lua is not available. -- Will use a very simple function formatting, if function.lua is not available.
format_number = import('number', true) or function (value, _, _, l) l[#l+1] = tostring(value) end format_number = import('number', true) or function (value, _, l) l[#l+1] = tostring(value) end
format_function = import('function', true) or function (value, _, _, l) l[#l+1] = 'function (...) --[['..tostring(value):sub(11)..']] end' end format_function = import('function', true) or function (value, _, l) l[#l+1] = 'function (...) --[['..tostring(value):sub(11)..']] end' end
-- Load other stuff -- Load other stuff
analyze_structure = import 'analyze_structure' analyze_structure = import 'analyze_structure'
@ -278,8 +278,6 @@ local function fix_seperator_info (l, indent_char, max_depth)
local depth, inline_depth = 0, nil local depth, inline_depth = 0, nil
for i = 1, #l do for i = 1, #l do
--print(i, l[i], depth, inline_depth)
if type(l[i]) ~= 'table' then if type(l[i]) ~= 'table' then
-- Do nothing -- Do nothing
elseif l[i][1] == 'seperator' then elseif l[i][1] == 'seperator' then
@ -301,25 +299,25 @@ local format_value
-- Ways to format keys -- Ways to format keys
local function format_key_and_value_string_map (l, key, value, _, depth) local function format_key_and_value_string_map (l, key, value, depth)
l[#l+1] = key l[#l+1] = key
l[#l+1] = { 'align', 'key', #key } l[#l+1] = { 'align', 'key', #key }
l[#l+1] = ' = ' l[#l+1] = ' = '
return format_value(value, nil, depth, l) return format_value(value, depth, l)
end end
local function format_key_and_value_arbitr_map (l, key, value, _, depth) local function format_key_and_value_arbitr_map (l, key, value, depth)
local index_before_key = #l+1 local index_before_key = #l+1
l[#l+1] = '[' l[#l+1] = '['
format_value(key, nil, math.huge, l) format_value(key, math.huge, l)
l[#l+1] = ']' l[#l+1] = ']'
l[#l+1] = { 'align', 'key', width_of_strings_in_l(l, index_before_key) } l[#l+1] = { 'align', 'key', width_of_strings_in_l(l, index_before_key) }
l[#l+1] = ' = ' l[#l+1] = ' = '
return format_value(value, nil, depth, l) return format_value(value, depth, l)
end end
local function format_key_and_value_sequence (l, key, value, _, depth) local function format_key_and_value_sequence (l, key, value, depth)
return format_value(value, nil, depth, l) return format_value(value, depth, l)
end end
local TABLE_TYPE_TO_PAIR_FORMAT = { local TABLE_TYPE_TO_PAIR_FORMAT = {
@ -333,15 +331,14 @@ local TABLE_TYPE_TO_PAIR_FORMAT = {
-- Formatting tables -- Formatting tables
function format_table (t, _, depth, l) function format_table (t, depth, l)
-- Error Checking
-- TODO: Add more nuanced formatting. -- TODO: Add more nuanced formatting.
-- Error Checking
assert(type(t) == 'table') assert(type(t) == 'table')
assert(type(depth) == 'number' and type(l) == 'table') assert(type(depth) == 'number' and type(l) == 'table')
-- Do stuff -- Do stuff
local table_info = l.info[t] or get_table_info(t) local table_info = l.info[t] or get_table_info(t)
if l.options.recursion == 'marked' and table_info.marker then if l.options.recursion == 'marked' and table_info.marker then
@ -373,7 +370,7 @@ function format_table (t, _, depth, l)
-- Begin formatting table. -- Begin formatting table.
l[#l+1] = {'indent', '{'} l[#l+1] = {'indent', '{'}
for _, pair in ipairs(key_value_pairs) do for _, pair in ipairs(key_value_pairs) do
pair_format_func(l, pair[1], pair[2], nil, depth + 1) pair_format_func(l, pair[1], pair[2], depth + 1)
l[#l+1] = ',' l[#l+1] = ','
l[#l+1] = {'seperator'} l[#l+1] = {'seperator'}
end end
@ -394,7 +391,7 @@ end
-- Formatting Strings -- Formatting Strings
local function format_string (str, _, depth, l) local function format_string (str, depth, l)
-- TODO: Add option for escaping unicode characters. -- TODO: Add option for escaping unicode characters.
-- Error checking -- Error checking
@ -440,13 +437,20 @@ local function format_string (str, _, depth, l)
l[#l+1] = right l[#l+1] = right
end end
local function format_coroutine (value, _, depth, l) local function format_coroutine (value, depth, l)
-- Error check
assert(type(value) == 'thread')
assert(type(depth) == 'number' and type(l) == 'table')
-- Do stuff
l[#l+1] = coroutine.status(value) l[#l+1] = coroutine.status(value)
l[#l+1] = ' coroutine: ' l[#l+1] = ' coroutine: '
l[#l+1] = tostring(value):sub(9) l[#l+1] = tostring(value):sub(9)
end end
local function format_primitive (value, _, depth, l) local function format_primitive (value, depth, l)
-- Error check
assert(type(depth) == 'number' and type(l) == 'table')
-- Do stuff
l[#l+1] = tostring(value) l[#l+1] = tostring(value)
end end
@ -463,12 +467,11 @@ local TYPE_TO_FORMAT_FUNC = {
['cdata'] = format_primitive, -- TODO & Luajit only ['cdata'] = format_primitive, -- TODO & Luajit only
} }
function format_value (value, _, depth, l) function format_value (value, depth, l)
assert(type(depth) == 'number' and type(l) == 'table') assert(type(depth) == 'number' and type(l) == 'table')
if _ ~= nil then print('Ugh: ' .. tostring(_)) end
local formatting = TYPE_TO_FORMAT_FUNC[type(value)] local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
if formatting then if formatting then
formatting(value, nil, depth, l, format_value) formatting(value, depth, l, format_value)
else else
error(ERROR_UNKNOWN_TYPE:format(type(value), tostring(value)), 2) error(ERROR_UNKNOWN_TYPE:format(type(value), tostring(value)), 2)
end end
@ -526,7 +529,7 @@ local function pretty_format (value, options)
l.info = (type(value) == 'table') and analyze_structure(value) or {} l.info = (type(value) == 'table') and analyze_structure(value) or {}
-- Format value. -- Format value.
format_value(value, nil, 0, l) format_value(value, 0, l)
-- If any alignment info still exists, ignore it -- If any alignment info still exists, ignore it
fix_seperator_info(l, l.options.indent, l.options.max_depth) fix_seperator_info(l, l.options.indent, l.options.max_depth)