1
0

The embed_loaded_funcs option has been removed, and 'short' loaded functions will automatically be embedded.

This commit is contained in:
Jon Michael Aanes 2017-07-15 20:10:49 +02:00
parent 41b274146a
commit e5f53b9b63
3 changed files with 49 additions and 20 deletions

View File

@ -55,6 +55,8 @@ end
-- Constants -- Constants
local NR_CHARS_IN_LONG_FUNCTION_BODY = 30
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Util -- Util
@ -232,9 +234,12 @@ return function (value, depth, l, format_value)
local function_params, function_body = nil, '...' local function_params, function_body = nil, '...'
if info.defined_how == 'string' and l.options.embed_loaded_funcs then if info.defined_how == 'string' then
-- Function was defined as a string. -- Function was defined as a string
function_params, function_body = get_function_paramlist_and_body(info) local params, body = get_function_paramlist_and_body(info)
if #body <= NR_CHARS_IN_LONG_FUNCTION_BODY and not body:find '\n' then
function_params, function_body = params, body
end
end end
if info.builtin and l.options.short_builtins then if info.builtin and l.options.short_builtins then

View File

@ -476,7 +476,6 @@ local KNOWN_OPTIONS = {
cut_strings = { type = 'boolean', default = false }, cut_strings = { type = 'boolean', default = false },
indent = { type = 'string', default = ' ' }, indent = { type = 'string', default = ' ' },
max_depth = { type = 'number', default = math.huge }, max_depth = { type = 'number', default = math.huge },
embed_loaded_funcs = { type = 'boolean', default = false }, -- TODO: Outphase this, in favor of automatically embedding "small enough" functions.
short_builtins = { type = 'boolean', default = false }, -- TODO: Outphase this. Rather automatically use the short versions in places where it would be strange to find the function, like keys, etc. short_builtins = { type = 'boolean', default = false }, -- TODO: Outphase this. Rather automatically use the short versions in places where it would be strange to find the function, like keys, etc.
recursion = { type = 'string', default = 'ignore', accepted = {['ignore'] = true, ['marked'] = true} }, recursion = { type = 'string', default = 'ignore', accepted = {['ignore'] = true, ['marked'] = true} },
} }

View File

@ -259,7 +259,6 @@ format_test {
single = true, single = true,
name = 'It\'s possible to get loadstring functions whole', name = 'It\'s possible to get loadstring functions whole',
input = loadstring('return function (a, b) return a + b end')(), input = loadstring('return function (a, b) return a + b end')(),
options = { embed_loaded_funcs = true },
expect = 'function (a, b) return a + b end', expect = 'function (a, b) return a + b end',
} }
@ -267,14 +266,12 @@ format_test {
single = true, single = true,
name = 'Whitespace is automatically stripped from loadstring functions', name = 'Whitespace is automatically stripped from loadstring functions',
input = loadstring('return function (a, b)\n return a + b\nend')(), input = loadstring('return function (a, b)\n return a + b\nend')(),
options = { embed_loaded_funcs = true },
expect = 'function (a, b) return a + b end', expect = 'function (a, b) return a + b end',
} }
format_test { format_test {
single = true, single = true,
adv_getlocal = true, adv_getlocal = true,
options = { embed_loaded_funcs = true },
input = loadstring('return function () return function () end\nend')()(), input = loadstring('return function () return function () end\nend')()(),
expect = 'function () end', expect = 'function () end',
} }
@ -283,7 +280,6 @@ format_test {
name = 'When finding the correct function becomes too hard, just ignore it 1', name = 'When finding the correct function becomes too hard, just ignore it 1',
single = true, single = true,
adv_getlocal = true, adv_getlocal = true,
options = { embed_loaded_funcs = true },
input = loadstring('return function () return function () end end')(), input = loadstring('return function () return function () end end')(),
expect = 'function () ... end', expect = 'function () ... end',
} }
@ -292,7 +288,6 @@ format_test {
name = 'When finding the correct function becomes too hard, just ignore it 2', name = 'When finding the correct function becomes too hard, just ignore it 2',
single = true, single = true,
adv_getlocal = true, adv_getlocal = true,
options = { embed_loaded_funcs = true },
input = loadstring('return function () return function () end end')()(), input = loadstring('return function () return function () end end')()(),
expect = 'function () ... end', expect = 'function () ... end',
} }
@ -301,46 +296,74 @@ format_test {
name = 'Can still find body when body contains the word "function"', name = 'Can still find body when body contains the word "function"',
single = true, single = true,
adv_getlocal = true, adv_getlocal = true,
options = { embed_loaded_funcs = true },
input = loadstring('return function () function_body() end')(), input = loadstring('return function () function_body() end')(),
expect = 'function () function_body() end', expect = 'function () function_body() end',
} }
format_test {
name = 'Can still find body when defined over objct',
single = true,
adv_getlocal = true,
input = loadstring('local obj = {} \n function obj:a () return self end \n return obj.a')(),
expect = 'function (self) return self end',
}
format_test {
name = 'Can find correct body, when body is on the same line, with different arguments',
single = true,
adv_getlocal = true,
input = loadstring 'return { a = function (a) return a end, b = function (b) return b end }' ()['a'],
expect = 'function (a) return a end',
}
format_test {
name = 'Use dots when function body is somewhat long',
single = true,
adv_getlocal = true,
input = loadstring('return function () return 1234578901235789012357890 end')(),
expect = 'function () ... end',
}
format_test {
name = 'Use dots when function body includes newline',
single = true,
adv_getlocal = true,
input = loadstring('return function () -- Hi\n return 1234 end')(),
expect = 'function () ... end',
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Indent functions nicely -- Indent functions nicely
format_test { format_test {
-- The tail part should align, letting people focus on the important aspects. name = 'Align the tail part, letting people focus on the important aspects.',
input = { random = math.random, abs = math.abs }, input = { random = math.random, abs = math.abs },
expect = '{\n abs = builtin function (x) ... end,\n random = builtin function ([m [, n]) ... end\n}', expect = '{\n abs = builtin function (x) ... end,\n random = builtin function ([m [, n]) ... end\n}',
} }
format_test { format_test {
-- The function part should align, if some are builtin and some are not. name = 'Align the function part, even if some are builtin and some are not.',
adv_getlocal = true, adv_getlocal = true,
input = { random = math.random, abs = function (x) return x < 0 and -x or x end }, input = { random = math.random, abs = function (x) return x < 0 and -x or x end },
expect = '{\n abs = function (x) ... end,\n random = builtin function ([m [, n]) ... end\n}', expect = '{\n abs = function (x) ... end,\n random = builtin function ([m [, n]) ... end\n}',
} }
format_test { format_test {
-- The function part should align, even if one is loaded from a string. name = 'Align the function part, even if one is loaded from a string.',
adv_getlocal = true, adv_getlocal = true,
input = { random = math.random, abs = loadstring('return function () return 1 end')() }, input = { random = math.random, abs = loadstring('return function () return 1 end')() },
options = { embed_loaded_funcs = true },
expect = '{\n abs = function () return 1 end,\n random = builtin function ([m [, n]) ... end\n}', expect = '{\n abs = function () return 1 end,\n random = builtin function ([m [, n]) ... end\n}',
} }
format_test { format_test {
-- The end part should align when both are loaded from strings. name = 'Align the end part when both are loaded from strings.',
adv_getlocal = true, adv_getlocal = true,
input = { a = loadstring'return function(a) return a end'(), b = loadstring'return function (...) return ... end'() }, input = { a = loadstring'return function(a) return a end'(), b = loadstring'return function (...) return ... end'() },
options = { embed_loaded_funcs = true },
expect = '{\n a = function (a) return a end,\n b = function (...) return ... end\n}', expect = '{\n a = function (a) return a end,\n b = function (...) return ... end\n}',
} }
format_test { format_test {
-- No special indent if no special function modifier. name = 'Align without special indent if there is function modifier.',
adv_getlocal = true, adv_getlocal = true,
input = { max = function(a, b) return a > b and a or b end, input = { max = function(a, b) return a > b and a or b end,
abs = function (x) return x < 0 and -x or x end abs = function (x) return x < 0 and -x or x end
@ -350,10 +373,10 @@ format_test {
if HAS_UNICODE_IDEN then if HAS_UNICODE_IDEN then
format_test { format_test {
name = 'Functions with unicode-named parameters should align nicely', name = 'Align functions with unicode-named parameters nicely',
adv_getlocal = true, adv_getlocal = true,
input = loadstring 'return { a = function (ψ) return ψ end, b = function (a) return a end }' (), input = loadstring 'return {\nψ = function (ψ) return ψ end,\nb = function (b) return b end\n}' (),
expect = '{\n a = function (ψ) ... end\n b = function (a) ... end\n}', expect = '{\n ψ = function (ψ) return ψ end\n b = function (b) return b end\n}',
} }
end end
@ -456,6 +479,8 @@ do
} }
end end
do do
local a_func = function (x) return x + 2 end local a_func = function (x) return x + 2 end
local b_func = function (x) return a_func(x) * a_func(x) end local b_func = function (x) return a_func(x) * a_func(x) end