Restructure to allow more flexible formatting of key-value pairs. Hopefully this allows merging sequence and map.
This commit is contained in:
parent
cf7ed5b289
commit
b3d12f8659
57
pretty.lua
57
pretty.lua
|
@ -253,12 +253,20 @@ local format_table, format_value
|
|||
|
||||
-- Ways to format keys
|
||||
|
||||
local function format_key_strings (key)
|
||||
return key
|
||||
local function format_key_and_value_string_map (l, key, value, options, depth)
|
||||
l[#l+1] = key
|
||||
l[#l+1] = #key
|
||||
l[#l+1] = ' = '
|
||||
l[#l+1] = format_value(value, options, depth)
|
||||
end
|
||||
|
||||
local function format_key_any_type (key, options)
|
||||
return '[' .. format_value(key, options, 'max') .. ']'
|
||||
local function format_key_and_value_arbitr_map (l, key, value, options, depth)
|
||||
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
|
||||
|
||||
-- Formatting tables
|
||||
|
@ -274,15 +282,16 @@ end
|
|||
local function format_single_line_map (t, options)
|
||||
-- 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 l = {'{ '}
|
||||
for _, pair in ipairs(key_value_pairs) do
|
||||
l[#l+1] = key_format_func(pair[1], options)
|
||||
l[#l+1] = ' = '
|
||||
l[#l+1] = format_value(pair[2], options)
|
||||
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
|
||||
if l[#l] == ', ' then l[#l] = nil end
|
||||
l[#l+1] = ' }'
|
||||
|
@ -314,26 +323,30 @@ local function format_map (t, options, depth)
|
|||
end
|
||||
|
||||
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 max_key_len = 0
|
||||
local pair_format_func = contains_only_nice_string_keys(t) and format_key_and_value_string_map or format_key_and_value_arbitr_map
|
||||
|
||||
-- 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'}
|
||||
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] = pair[1]
|
||||
l[#l+1] = string.rep(' ', max_key_len + 1 - pair[1]:len())
|
||||
l[#l+1] = '= '
|
||||
l[#l+1] = pair[2]
|
||||
pair_format_func(l, pair[1], pair[2], options, depth + 1)
|
||||
l[#l+1] = ',\n'
|
||||
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+1] = options.indent:rep(depth)
|
||||
l[#l+1] = '}'
|
||||
|
|
|
@ -266,6 +266,16 @@ format_test {
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user