1
0

Merged format_map into format_table.

This commit is contained in:
Jon Michael Aanes 2017-06-05 22:47:07 +02:00
parent bf9c5a6749
commit 97ced3a173

View File

@ -297,7 +297,7 @@ end
--------------------------------------------------------------------------------
-- Formatting stuff
local format_table, format_value
local format_value
-- Ways to format keys
@ -333,37 +333,57 @@ local TABLE_TYPE_TO_PAIR_FORMAT = {
-- Formatting tables
local function format_map (t, options, depth, l)
-- TODO: Merge with `format_table`?
function format_table (t, options, depth, l)
-- Error Checking
-- TODO: Add more nuanced formatting.
assert(type(t) == 'table')
assert(type(options) == 'table' and type(depth) == 'number' and type(l) == 'table')
-- Do stuff
local table_info = l.info[t] or get_table_info(t)
if options.recursion == 'marked' and table_info.marker then
l[#l+1], l[#l+2], l[#l+3] = '<', table_info.marker, '>'
end
local already_visited = l.visited[t]
l.visited[t] = true
if table_info.type == TABLE_TYPE.EMPTY then
-- Empty Map
l[#l+1] = '{}'
return;
elseif depth >= options.max_depth or already_visited then
-- Already visited or above max depth
l[#l+1] = '{...}'
return;
end
-- Get key-value pairs, and possibly fill holes.
local key_value_pairs = get_key_value_pairs_in_proper_order(t)
if table_info.type == TABLE_TYPE.SEQUENCE and l.info[t].has_holes then
fill_holes_in_key_value_pairs(key_value_pairs)
end
-- Find format function
local pair_format_func = TABLE_TYPE_TO_PAIR_FORMAT[table_info.type]
local start_of_table_i = #l + 1
l[#l+1] = {'indent', '{'}
assert(pair_format_func)
-- Begin formatting table.
l[#l+1] = {'indent', '{'}
for _, pair in ipairs(key_value_pairs) do
pair_format_func(l, pair[1], pair[2], options, depth + 1)
l[#l+1] = ','
l[#l+1] = {'seperator'}
end
if l[#l][1] == 'seperator' then l[#l-1], l[#l] = nil, nil end
l[#l+1] = {'unindent', '}'}
-- Decide for short or long table formatting.
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
l[start_of_table_i][3] = 'inline'
@ -374,34 +394,7 @@ local function format_map (t, options, depth, l)
end
end
function format_table (t, options, depth, l)
-- Error Checking
assert(type(t) == 'table')
assert(type(options) == 'table' and type(depth) == 'number' and type(l) == 'table')
-- Do stuff
local info = l.info[t] or {}
if options.recursion == 'marked' and info.marker then
l[#l+1], l[#l+2], l[#l+3] = '<', info.marker, '>'
end
local already_visited = l.visited[t]
l.visited[t] = true
if info.type == TABLE_TYPE.EMPTY then
-- Empty Map
l[#l+1] = '{}'
elseif depth >= options.max_depth or already_visited then
-- Already visited or above max depth
l[#l+1] = '{...}'
else
-- Just a normal table
return format_map(t, options, depth, l)
end
end
-- Formatting Strings
local function format_string (str, options, depth, l)
-- TODO: Add option for escaping unicode characters.