diff --git a/function.lua b/function.lua index c9f1205..71e0b24 100644 --- a/function.lua +++ b/function.lua @@ -189,7 +189,7 @@ return function (value, options, depth, l, format_value) -- Include function modifier, and alignment info. l[#l+1] = info.builtin and 'builtin ' or '' - l[#l+1] = { #l[#l], 'func_mod'} + l[#l+1] = { 'align', 'func_mod', #l[#l]} -- Build rest of function signature l[#l+1] = 'function (' @@ -197,7 +197,7 @@ return function (value, options, depth, l, format_value) for _, param in ipairs(info.params) do l[#l+1], l[#l+2] = param, ', ' end if l[#l] == ', ' then l[#l] = nil end l[#l+1] = ')' - l[#l+1] = { width_of_strings_in_l(l, top_before), 'func_def' } + l[#l+1] = { 'align', 'func_def', width_of_strings_in_l(l, top_before) } -- Cleanup and finish if not options.more_function_info or depth ~= 0 then @@ -245,7 +245,8 @@ return function (value, options, depth, l, format_value) end if options._all_function_info then - -- NOTE: This is for testing/debugging/experimentation purposes. + -- NOTE: This is for testing/debugging/experimentation purposes, and is + -- not designed to be pretty. l[#l+1] = indent l[#l+1] = '--[[ Function Body:\n\t' diff --git a/pretty.lua b/pretty.lua index 415ea5d..56f3ce1 100644 --- a/pretty.lua +++ b/pretty.lua @@ -222,7 +222,7 @@ end local function ignore_alignment_info (l, start_i, stop_i) for i = start_i or 1, stop_i or #l do - if type(l[i]) == 'table' then + if type(l[i]) == 'table' and l[i][1] == 'align' then l[i] = '' end end @@ -234,29 +234,34 @@ local function fix_alignment (l, start_i, stop_i) -- Find maximums local max = {} for i = start_i, stop_i do - if type(l[i]) == 'table' then - max[ l[i][2] ] = math.max( l[i][1], max[ l[i][2] ] or 0 ) + if type(l[i]) == 'table' and l[i][1] == 'align' then + max[ l[i][2] ] = math.max( l[i][3], max[ l[i][2] ] or 0 ) end end -- Insert the proper whitespace for i = start_i, stop_i do - if type(l[i]) == 'table' then - l[i] = string.rep(' ', max[ l[i][2] ] - l[i][1]) + if type(l[i]) == 'table' and l[i][1] == 'align' then + l[i] = string.rep(' ', max[ l[i][2] ] - l[i][3]) end end end -local function replace_seperator_info (l, replace_with, indent_char, depth, start_i, stop_i) - for i = start_i or 1, stop_i or #l do +local function fix_seperator_info (l, indent_char, max_depth) + local depth, inline_depth = 0, nil + for i = 1, #l do + --print(i, l[i], depth, inline_depth) + if type(l[i]) ~= 'table' then -- Do nothing elseif l[i][1] == 'seperator' then - l[i] = replace_with .. indent_char:rep(depth) + l[i] = inline_depth and ' ' or ('\n' .. indent_char:rep(depth)) elseif l[i][1] == 'indent' then - l[i], depth = '', depth + 1 + depth, inline_depth = depth + 1, inline_depth or l[i][3] == 'inline' and depth + 1 or nil + l[i] = l[i][2] .. (inline_depth and ' ' or ('\n' .. indent_char:rep(depth))) elseif l[i][1] == 'unindent' then - l[i], depth = '', depth - 1 + l[i] = (inline_depth and ' ' or ('\n' .. indent_char:rep(depth-1))) .. l[i][2] + depth, inline_depth = depth - 1, (depth ~= inline_depth) and inline_depth or nil end end end @@ -300,7 +305,7 @@ local format_table, format_value local function format_key_and_value_string_map (l, key, value, options, depth) l[#l+1] = key - l[#l+1] = { #key, 'key' } + l[#l+1] = { 'align', 'key', #key } l[#l+1] = ' = ' return format_value(value, options, depth, l) end @@ -310,7 +315,7 @@ local function format_key_and_value_arbitr_map (l, key, value, options, depth) l[#l+1] = '[' format_value(key, options, 'max', l) -- TODO: Outphase the usage of the "max" depth thingy. l[#l+1] = ']' - l[#l+1] = { width_of_strings_in_l(l, index_before_key), 'key' } + l[#l+1] = { 'align', 'key', width_of_strings_in_l(l, index_before_key) } l[#l+1] = ' = ' return format_value(value, options, depth, l) end @@ -341,9 +346,7 @@ local function format_map (t, options, depth, l) local pair_format_func = TABLE_TYPE_TO_PAIR_FORMAT[table_info.type] local start_of_table_i = #l + 1 - l[#l+1] = '{' - l[#l+1] = {'indent'} - l[#l+1] = {'seperator'} + l[#l+1] = {'indent', '{'} for _, pair in ipairs(key_value_pairs) do pair_format_func(l, pair[1], pair[2], options, depth + 1) @@ -352,19 +355,17 @@ local function format_map (t, options, depth, l) end if l[#l][1] == 'seperator' then l[#l-1], l[#l] = nil, nil end - l[#l+1] = {'unindent'} - l[#l+1] = {'seperator'} - l[#l+1] = '}' + l[#l+1] = {'unindent', '}'} local table_width = width_of_strings_in_l(l, start_of_table_i) + --print('Table width: '..table_width) if table_width <= MAX_WIDTH_FOR_SINGLE_LINE_TABLE then -- Is short table: Ignore the "width of key"-shit - replace_seperator_info(l, ' ', '', 0, start_of_table_i) + l[start_of_table_i][3] = 'inline' ignore_alignment_info(l, start_of_table_i) else -- Is long table: Fix whitespace alignment - replace_seperator_info(l, '\n', options.indent, depth, start_of_table_i) fix_alignment(l, start_of_table_i) end end @@ -478,6 +479,8 @@ end -------------------------------------------------------------------------------- +local DEBUG_OPTIONS = { _all_function_info = true } + local KNOWN_OPTIONS = { _all_function_info = 'boolean', cut_strings = 'boolean', @@ -510,6 +513,7 @@ local function pretty_format (value, options) format_value(value, nil, 0, l) -- If any alignment info still exists, ignore it + fix_seperator_info(l, l.options.indent, l.options.max_depth) ignore_alignment_info(l) return table.concat(l, '') diff --git a/test/test_pretty.lua b/test/test_pretty.lua index 829f1bd..e0e716a 100644 --- a/test/test_pretty.lua +++ b/test/test_pretty.lua @@ -217,7 +217,7 @@ format_test { format_test { input = { a = {1, 2, 3}, b = {4, 5, 6} }, - expect = '{\n\ta = { 1, 2, 3 },\n\tb = { 4, 5, 6 }\n}', + expect = '{ a = { 1, 2, 3 }, b = { 4, 5, 6 } }', } format_test { @@ -276,7 +276,7 @@ format_test { format_test { input = { a = {1,2}, bcdefg = {3,4} }, - expect = '{\n\ta = { 1, 2 },\n\tbcdefg = { 3, 4 }\n}', + expect = '{ a = { 1, 2 }, bcdefg = { 3, 4 } }', } format_test {