More restructure: Merge format_map and format_sequence together, same with format_single_line_map nad format_single_line_sequence.
This commit is contained in:
parent
b3d12f8659
commit
c5f3bd4420
77
pretty.lua
77
pretty.lua
|
@ -269,61 +269,50 @@ local function format_key_and_value_arbitr_map (l, key, value, options, depth)
|
|||
l[#l+1] = format_value(value, options, depth)
|
||||
end
|
||||
|
||||
-- Formatting tables
|
||||
|
||||
local function format_single_line_sequence (t, options)
|
||||
-- NOTE: Assumes that the input table was pre-checked with `is_single_line_table()`
|
||||
|
||||
local l = {}
|
||||
for i = 1, #t do l[i] = format_value(t[i], options) end
|
||||
return '{ ' .. table.concat(l, ', ') .. ' }'
|
||||
local function format_key_and_value_sequence (l, key, value, options, depth)
|
||||
l[#l+1] = format_value(value, options, depth)
|
||||
end
|
||||
|
||||
local function get_pair_format_func (t)
|
||||
local table_type = get_table_type(t)
|
||||
if table_type == TABLE_TYPE_SEQUENCE then return format_key_and_value_sequence
|
||||
elseif contains_only_nice_string_keys(t) then return format_key_and_value_string_map
|
||||
end
|
||||
return format_key_and_value_arbitr_map
|
||||
end
|
||||
|
||||
-- Formatting tables
|
||||
|
||||
local function format_single_line_map (t, options)
|
||||
-- NOTE: Assumes that the input table was pre-checked with `is_single_line_table()`
|
||||
|
||||
local pair_format_func = contains_only_nice_string_keys(t) and format_key_and_value_string_map or format_key_and_value_arbitr_map
|
||||
|
||||
local key_value_pairs = get_key_value_pairs_in_proper_order(t)
|
||||
local pair_format_func = get_pair_format_func(t)
|
||||
|
||||
local l = {'{ '}
|
||||
local top_before = #l
|
||||
|
||||
for _, pair in ipairs(key_value_pairs) do
|
||||
local top_before = #l
|
||||
pair_format_func(l, pair[1], pair[2], options, 'max')
|
||||
l[#l+1] = ', '
|
||||
-- Ignore the "width of key"-shit
|
||||
for i = top_before, #l do if type(l[i]) == 'number' then l[i] = '' end end
|
||||
end
|
||||
|
||||
-- Ignore the "width of key"-shit
|
||||
for i = top_before, #l do
|
||||
if type(l[i]) == 'number' then
|
||||
l[i] = ''
|
||||
end
|
||||
end
|
||||
|
||||
if l[#l] == ', ' then l[#l] = nil end
|
||||
l[#l+1] = ' }'
|
||||
return table.concat(l, '')
|
||||
end
|
||||
|
||||
local function format_sequence (t, options, depth)
|
||||
if depth ~= 'max' and depth >= options.max_depth then return '{...}'
|
||||
elseif is_single_line_table(t) then return format_single_line_sequence(t, options)
|
||||
elseif depth == 'max' then return '{...}'
|
||||
end
|
||||
|
||||
local l = {'{\n'}
|
||||
for index, value in ipairs(t) do
|
||||
l[#l+1] = options.indent:rep(depth + 1)
|
||||
l[#l+1] = format_value(value, options, depth + 1)
|
||||
l[#l+1] = ',\n'
|
||||
end
|
||||
l[#l] = '\n'
|
||||
l[#l+1] = options.indent:rep(depth)
|
||||
l[#l+1] = '}'
|
||||
return table.concat(l, '')
|
||||
end
|
||||
|
||||
local function format_map (t, options, depth)
|
||||
if depth ~= 'max' and depth >= options.max_depth then return '{...}'
|
||||
elseif is_single_line_table(t) then return format_single_line_map(t, options)
|
||||
elseif depth == 'max' then return '{...}'
|
||||
end
|
||||
|
||||
local key_value_pairs = get_key_value_pairs_in_proper_order(t)
|
||||
local pair_format_func = contains_only_nice_string_keys(t) and format_key_and_value_string_map or format_key_and_value_arbitr_map
|
||||
local key_value_pairs = get_key_value_pairs_in_proper_order(t)
|
||||
local pair_format_func = get_pair_format_func(t)
|
||||
|
||||
-- Figure out the max key length
|
||||
local l = {'{\n'}
|
||||
|
@ -356,10 +345,18 @@ end
|
|||
function format_table (t, options, depth)
|
||||
local table_type = get_table_type(t)
|
||||
|
||||
if table_type == TABLE_TYPE_EMPTY then return '{}'
|
||||
elseif table_type == TABLE_TYPE_SEQUENCE then return format_sequence(t, options, depth)
|
||||
else return format_map(t, options, depth)
|
||||
-- Empty or exteeding max-depth?
|
||||
if table_type == TABLE_TYPE_EMPTY then return '{}'
|
||||
elseif depth ~= 'max' and depth >= options.max_depth then return '{...}'
|
||||
end
|
||||
|
||||
-- Single line?
|
||||
if is_single_line_table(t) then return format_single_line_map(t, options) end
|
||||
|
||||
if depth == 'max' then return '{...}' end
|
||||
|
||||
-- Normal table
|
||||
return format_map(t, options, depth)
|
||||
end
|
||||
|
||||
local function format_string (str, options)
|
||||
|
|
Loading…
Reference in New Issue
Block a user