diff --git a/function.lua b/function.lua index f9e8441..5120489 100644 --- a/function.lua +++ b/function.lua @@ -155,10 +155,8 @@ local function get_function_paramlist_and_body (info) if type(function_params) ~= 'string' or type(function_body) ~= 'string' then error(('[pretty.function/internal]: Could not find the function defined on lines %i-%i (indices %i-%i) for string:\n\n%s\n'):format(info.linedefined, info.lastlinedefined, start_line_index, end_line_index, str)) end - function_body = function_body:match('^%s*(.-)%s*$') - assert(type(function_body) == 'string') -- And return them. - return function_params, function_body + return function_params, function_body:match('^%s*(.-)%s*$'), function_name:match('^%s*(.-)%s*$') end local function get_function_string (...) @@ -259,15 +257,15 @@ return function (value, depth, l, format_value) local function_params, function_body = nil, '...' if info.defined_how == 'string' or not info.doc then - local params_text, body = get_function_paramlist_and_body(info) - local documentation, body = get_docs_from_function_body(body) + local _, body, name = get_function_paramlist_and_body(info) + local docs, body = get_docs_from_function_body(body) body = body:match('^%s*(.-)%s*$') if #body <= NR_CHARS_IN_LONG_FUNCTION_BODY and not body:find '\n' and not body:find(FUNCTION_KEYWORD_MATCH) then if info.defined_how == 'string' then function_body = body end end - if body then - info.doc = documentation ~= '' and documentation - end + + info.doc = not info.doc and docs and docs ~= '' and docs + info.name = not info.name and name and name ~= '' and name end if info.builtin and l.options.short_builtins then @@ -321,7 +319,7 @@ return function (value, depth, l, format_value) end -- source - if info.doc then -- Do nothing + if info.doc or info.name then -- Do nothing elseif info.defined_how == 'string' then l[#l+1] = indent l[#l+1] = '-- Loaded from string' diff --git a/test/test_function.lua b/test/test_function.lua index 1908d3a..61e664e 100644 --- a/test/test_function.lua +++ b/test/test_function.lua @@ -375,6 +375,24 @@ format_test { expect = 'function ()\n -- Loaded from string\nend', } +format_test { + name = 'Embedding function with a nice name, includes name in documentation', + input = loadstring 'local function ahab () end; return ahab' (), + expect = 'function ()\n -- ahab\nend', +} + +format_test { + name = 'Embedding function with a indexed name, includes name in documentation', + input = loadstring 'local obj = {}; function obj.ahab () return "isaac" end; return obj.ahab' (), + expect = 'function ()\n -- obj.ahab\n\n return "isaac"\nend', +} + +format_test { + name = 'Embedding function with a OOP name, includes name in documentation', + input = loadstring 'local obj = {}; function obj:ahab () end; return obj.ahab' (), + expect = 'function (self)\n -- obj:ahab\nend', +} + -------------------------------------------------------------------------------- -- Indent functions nicely