diff --git a/function.lua b/function.lua index 54f65af..cb1d499 100644 --- a/function.lua +++ b/function.lua @@ -55,6 +55,8 @@ end -- Constants +local NR_CHARS_IN_LONG_FUNCTION_BODY = 30 + -------------------------------------------------------------------------------- -- Util @@ -232,9 +234,12 @@ return function (value, depth, l, format_value) local function_params, function_body = nil, '...' - if info.defined_how == 'string' and l.options.embed_loaded_funcs then - -- Function was defined as a string. - function_params, function_body = get_function_paramlist_and_body(info) + if info.defined_how == 'string' then + -- Function was defined as a string + 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 if info.builtin and l.options.short_builtins then diff --git a/pretty.lua b/pretty.lua index 0761ce8..3471ae5 100644 --- a/pretty.lua +++ b/pretty.lua @@ -476,7 +476,6 @@ local KNOWN_OPTIONS = { cut_strings = { type = 'boolean', default = false }, indent = { type = 'string', default = ' ' }, 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. recursion = { type = 'string', default = 'ignore', accepted = {['ignore'] = true, ['marked'] = true} }, } diff --git a/test/test_function.lua b/test/test_function.lua index 6edc2c0..f9a0678 100644 --- a/test/test_function.lua +++ b/test/test_function.lua @@ -259,7 +259,6 @@ format_test { single = true, name = 'It\'s possible to get loadstring functions whole', input = loadstring('return function (a, b) return a + b end')(), - options = { embed_loaded_funcs = true }, expect = 'function (a, b) return a + b end', } @@ -267,14 +266,12 @@ format_test { single = true, name = 'Whitespace is automatically stripped from loadstring functions', input = loadstring('return function (a, b)\n return a + b\nend')(), - options = { embed_loaded_funcs = true }, expect = 'function (a, b) return a + b end', } format_test { single = true, adv_getlocal = true, - options = { embed_loaded_funcs = true }, input = loadstring('return function () return function () end\nend')()(), expect = 'function () end', } @@ -283,7 +280,6 @@ format_test { name = 'When finding the correct function becomes too hard, just ignore it 1', single = true, adv_getlocal = true, - options = { embed_loaded_funcs = true }, input = loadstring('return function () return function () end end')(), expect = 'function () ... end', } @@ -292,7 +288,6 @@ format_test { name = 'When finding the correct function becomes too hard, just ignore it 2', single = true, adv_getlocal = true, - options = { embed_loaded_funcs = true }, input = loadstring('return function () return function () end end')()(), expect = 'function () ... end', } @@ -301,46 +296,74 @@ format_test { name = 'Can still find body when body contains the word "function"', single = true, adv_getlocal = true, - options = { embed_loaded_funcs = true }, input = loadstring('return 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 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 }, expect = '{\n abs = builtin function (x) ... end,\n random = builtin function ([m [, n]) ... end\n}', } 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, 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}', } 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, 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}', } 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, 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}', } format_test { - -- No special indent if no special function modifier. + name = 'Align without special indent if there is function modifier.', adv_getlocal = true, input = { max = function(a, b) return a > b and a or b end, abs = function (x) return x < 0 and -x or x end @@ -350,10 +373,10 @@ format_test { if HAS_UNICODE_IDEN then format_test { - name = 'Functions with unicode-named parameters should align nicely', + name = 'Align functions with unicode-named parameters nicely', adv_getlocal = true, - input = loadstring 'return { a = function (ψ) return ψ end, b = function (a) return a end }' (), - expect = '{\n a = function (ψ) ... end\n b = function (a) ... end\n}', + input = loadstring 'return {\nψ = function (ψ) return ψ end,\nb = function (b) return b end\n}' (), + expect = '{\n ψ = function (ψ) return ψ end\n b = function (b) return b end\n}', } end @@ -456,6 +479,8 @@ do } end + + do local a_func = function (x) return x + 2 end local b_func = function (x) return a_func(x) * a_func(x) end