1
0

Restructure to allow more flexible formatting of key-value pairs. Hopefully this allows merging sequence and map.

This commit is contained in:
Jon Michael Aanes 2016-12-29 05:34:44 +01:00
parent cf7ed5b289
commit b3d12f8659
2 changed files with 45 additions and 22 deletions

View File

@ -253,12 +253,20 @@ local format_table, format_value
-- Ways to format keys -- Ways to format keys
local function format_key_strings (key) local function format_key_and_value_string_map (l, key, value, options, depth)
return key l[#l+1] = key
l[#l+1] = #key
l[#l+1] = ' = '
l[#l+1] = format_value(value, options, depth)
end end
local function format_key_any_type (key, options) local function format_key_and_value_arbitr_map (l, key, value, options, depth)
return '[' .. format_value(key, options, 'max') .. ']' l[#l+1] = '['
l[#l+1] = format_value(key, options, 'max')
l[#l+1] = ']'
l[#l+1] = #l[#l-1] + 2
l[#l+1] = ' = '
l[#l+1] = format_value(value, options, depth)
end end
-- Formatting tables -- Formatting tables
@ -274,15 +282,16 @@ end
local function format_single_line_map (t, options) local function format_single_line_map (t, options)
-- NOTE: Assumes that the input table was pre-checked with `is_single_line_table()` -- NOTE: Assumes that the input table was pre-checked with `is_single_line_table()`
local key_format_func = contains_only_nice_string_keys(t) and format_key_strings or format_key_any_type 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 key_value_pairs = get_key_value_pairs_in_proper_order(t)
local l = {'{ '} local l = {'{ '}
for _, pair in ipairs(key_value_pairs) do for _, pair in ipairs(key_value_pairs) do
l[#l+1] = key_format_func(pair[1], options) local top_before = #l
l[#l+1] = ' = ' pair_format_func(l, pair[1], pair[2], options, 'max')
l[#l+1] = format_value(pair[2], options)
l[#l+1] = ', ' 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 end
if l[#l] == ', ' then l[#l] = nil end if l[#l] == ', ' then l[#l] = nil end
l[#l+1] = ' }' l[#l+1] = ' }'
@ -314,26 +323,30 @@ local function format_map (t, options, depth)
end end
local key_value_pairs = get_key_value_pairs_in_proper_order(t) local key_value_pairs = get_key_value_pairs_in_proper_order(t)
local key_format_func = contains_only_nice_string_keys(t) and format_key_strings or format_key_any_type 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 max_key_len = 0
-- Figure out the max key length -- Figure out the max key length
for _, pair in pairs(key_value_pairs) do
pair[1] = key_format_func(pair[1], options)
pair[2] = format_value(pair[2], options, depth + 1)
max_key_len = math.max(max_key_len, pair[1]:len())
end
-- Finally figure out formatting
local l = {'{\n'} local l = {'{\n'}
for _, pair in ipairs(key_value_pairs) do local top_before = #l
for _, pair in pairs(key_value_pairs) do
l[#l+1] = options.indent:rep(depth + 1) l[#l+1] = options.indent:rep(depth + 1)
l[#l+1] = pair[1] pair_format_func(l, pair[1], pair[2], options, depth + 1)
l[#l+1] = string.rep(' ', max_key_len + 1 - pair[1]:len())
l[#l+1] = '= '
l[#l+1] = pair[2]
l[#l+1] = ',\n' l[#l+1] = ',\n'
end end
-- Figure out max key len
local max_key_len = 0
for i = top_before, #l do
if type(l[i]) == 'number' and l[i] > max_key_len then
max_key_len = l[i]
end
end
-- Replace in the proper whitespace
for i = top_before, #l do
if type(l[i]) == 'number' then
l[i] = string.rep(' ', max_key_len - l[i])
end
end
l[#l] = '\n' l[#l] = '\n'
l[#l+1] = options.indent:rep(depth) l[#l+1] = options.indent:rep(depth)
l[#l+1] = '}' l[#l+1] = '}'

View File

@ -266,6 +266,16 @@ format_test {
expect = '{\n\t[{...}] = \'Hello World\'\n}', expect = '{\n\t[{...}] = \'Hello World\'\n}',
} }
format_test {
input = { a = {1,2}, bcdefg = {3,4} },
expect = '{\n\ta = { 1, 2 },\n\tbcdefg = { 3, 4 }\n}',
}
format_test {
input = { [true] = 1, [1] = false },
expect = '{\n\t[true] = 1,\n\t[1] = false\n}',
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
return SUITE return SUITE