diff --git a/function.lua b/function.lua index 6def618..1fe852c 100644 --- a/function.lua +++ b/function.lua @@ -58,12 +58,13 @@ end -- FUNCTION_DEFINITION_MATCH is a lua pattern, for finding a function definition. -- NOTE: It will match malformed unicode sequences, and thus assumes that the -- string checked against have been checked by the lua interpreter. -local FUNCTION_DEFINITION_MATCH = - '.*%f[%a_]function%f[^%a_]%s*' .. -- Look for the function keyword +local FUNCTION_KEYWORD_MATCH = '%f[%a_]function%f[^%a_]' +local FUNCTION_DEFINITION_MATCH = '.-' .. -- Look for stuff before the function + FUNCTION_KEYWORD_MATCH .. '%s*' .. -- Look for the function keyword '([a-zA-Z0-9\128-\255_.:]*)%s*' .. -- Look for the function name, if any '(%([a-zA-Z0-9\128-\255_,. \t]*%))' .. -- Look for the function parameter list. '[ \t]*(.+)[ \t]*' .. -- Look for the function body - 'end' -- Look for the end keyword + 'end' -- Look for the end keyword local NR_CHARS_IN_LONG_FUNCTION_BODY = 30 @@ -250,7 +251,7 @@ return function (value, depth, l, format_value) if info.defined_how == 'string' then -- Function was defined as a string local _, body = get_function_paramlist_and_body(info) - if #body <= NR_CHARS_IN_LONG_FUNCTION_BODY and not body:find '\n' then + if #body <= NR_CHARS_IN_LONG_FUNCTION_BODY and not body:find '\n' and not body:find(FUNCTION_KEYWORD_MATCH) then function_body = body end end diff --git a/test/test_function.lua b/test/test_function.lua index ab4ad81..97021b5 100644 --- a/test/test_function.lua +++ b/test/test_function.lua @@ -270,15 +270,7 @@ format_test { } format_test { - name = 'Embed nested function, when they end on different lines', - single = true, - adv_getlocal = true, - input = loadstring('return function () return function () end\nend')()(), - expect = 'function () end', -} - -format_test { - name = 'If embedding the correct function becomes too hard, ignore 1', + name = 'Embedding nested function, when on same line is too hard 1', single = true, adv_getlocal = true, input = loadstring('return function () return function () end end')(), @@ -286,13 +278,21 @@ format_test { } format_test { - name = 'If embedding the correct function becomes too hard, ignore 2', + name = 'Embedding nested function, when on same line is too hard 2', single = true, adv_getlocal = true, input = loadstring('return function () return function () end end')()(), expect = 'function () ... end', } +format_test { + name = 'Embedding nested function, when they end on different lines is too hard', + single = true, + adv_getlocal = true, + input = loadstring('return function () return function () end\nend')()(), + expect = 'function () ... end', +} + format_test { name = 'Embed functions which contains the word "function"', single = true,