Slight improvements to function formatting.
This commit is contained in:
parent
55265378f9
commit
b27d307da0
83
pretty.lua
83
pretty.lua
|
@ -447,54 +447,61 @@ local function format_primitive (value)
|
||||||
return tostring(value)
|
return tostring(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[
|
|
||||||
|
|
||||||
if not options.more_function_info or depth ~= 0 then return table.concat(l, '') end
|
|
||||||
|
|
||||||
-- More info! --
|
|
||||||
|
|
||||||
-- source
|
|
||||||
l[#l+1] = '\n'
|
|
||||||
l[#l+1] = options.indent
|
|
||||||
l[#l+1] = 'source = '
|
|
||||||
l[#l+1] = info.short_src -- Maybe change to a longer
|
|
||||||
|
|
||||||
-- upvalues
|
|
||||||
if info.nups > 0 then
|
|
||||||
l[#l+1] = '\n'
|
|
||||||
l[#l+1] = options.indent
|
|
||||||
l[#l+1] = 'upvalues = '
|
|
||||||
l[#l+1] = format_value(info.ups, options, depth + 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
--if info.nups > 0 and not info.builtin and info.more_function_info then
|
|
||||||
l[#l+1] = '('
|
|
||||||
l[#l+1] = tostring(value)
|
|
||||||
l[#l+1] = '): '
|
|
||||||
l[#l+1] = format_value(info, options, depth + 1)
|
|
||||||
--end
|
|
||||||
]]
|
|
||||||
|
|
||||||
local function format_function (value, options, depth)
|
local function format_function (value, options, depth)
|
||||||
local info = get_function_info(value)
|
local info = get_function_info(value)
|
||||||
|
|
||||||
local l = {}
|
local l = {}
|
||||||
|
|
||||||
-- Func def
|
-- Build function signature
|
||||||
if info.builtin then l[#l+1] = 'builtin ' end
|
if info.builtin then l[#l+1] = 'builtin ' end
|
||||||
l[#l+1] = 'function ('
|
l[#l+1] = 'function ('
|
||||||
-- List parameters
|
for _, param in ipairs(info.params) do l[#l+1], l[#l+2] = param, ', ' end
|
||||||
for _, param in ipairs(info.params) do
|
|
||||||
l[#l+1] = param
|
|
||||||
l[#l+1] = ', '
|
|
||||||
end
|
|
||||||
-- Show varg
|
|
||||||
if info.isvararg then l[#l+1] = '...' end
|
if info.isvararg then l[#l+1] = '...' end
|
||||||
|
if l[#l] == ', ' then l[#l] = nil end
|
||||||
|
l[#l+1] = ')'
|
||||||
|
|
||||||
-- Cleanup and finish
|
-- Cleanup and finish
|
||||||
if l[#l] == ', ' then l[#l] = nil end
|
if not options.more_function_info or depth ~= 0 then
|
||||||
l[#l+1] = ') ... end'
|
l[#l+1] = ' ... end'
|
||||||
|
elseif options._all_function_info then
|
||||||
|
-- NOTE: This is for testing/debugging purposes.
|
||||||
|
l[#l+1] = '\n\t--[[\n\tNative repr:'
|
||||||
|
l[#l+1] = tostring(value)
|
||||||
|
l[#l+1] = '\n\t'
|
||||||
|
l[#l+1] = format_value(info, options, depth + 1)
|
||||||
|
l[#l+1] = '--]]'
|
||||||
|
else
|
||||||
|
-- More info! --
|
||||||
|
|
||||||
|
-- source
|
||||||
|
l[#l+1] = '\n'
|
||||||
|
l[#l+1] = options.indent
|
||||||
|
l[#l+1] = '-- source_file: \''
|
||||||
|
l[#l+1] = info.short_src
|
||||||
|
l[#l+1] = '\' [Line'
|
||||||
|
if info.linedefined == info.lastlinedefined then
|
||||||
|
l[#l+1] = ': '
|
||||||
|
l[#l+1] = tostring(info.linedefined)
|
||||||
|
else
|
||||||
|
l[#l+1] = 's: '
|
||||||
|
l[#l+1] = tostring(info.linedefined)
|
||||||
|
l[#l+1] = ' - '
|
||||||
|
l[#l+1] = tostring(info.lastlinedefined)
|
||||||
|
end
|
||||||
|
l[#l+1] = ']'
|
||||||
|
|
||||||
|
-- upvalues
|
||||||
|
if info.nups > 0 then
|
||||||
|
l[#l+1] = '\n'
|
||||||
|
l[#l+1] = options.indent
|
||||||
|
l[#l+1] = '-- up_values: '
|
||||||
|
l[#l+1] = format_value(info.ups, options, depth + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
l[#l+1] = '\n\n'
|
||||||
|
l[#l+1] = options.indent
|
||||||
|
l[#l+1] = '...\nend'
|
||||||
|
end
|
||||||
|
|
||||||
return table.concat(l, '')
|
return table.concat(l, '')
|
||||||
end
|
end
|
||||||
|
|
|
@ -221,20 +221,23 @@ do
|
||||||
format_test {
|
format_test {
|
||||||
input = function () l = SOME_RANDOM_UPVALUE end,
|
input = function () l = SOME_RANDOM_UPVALUE end,
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
expect = 'function ()\n\t-- source_file = \'./test/test_pretty.lua\'\n\t-- up_values = { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend'
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 222]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend'
|
||||||
}
|
}
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
input = function () SOME_RANDOM_UPVALUE = true end,
|
input = function () SOME_RANDOM_UPVALUE = true end,
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
expect = 'function ()\n\t-- source_file = \'./test/test_pretty.lua\'\n\t-- up_values = { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend'
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 229]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
input = function () end,
|
input = function () end,
|
||||||
options = { more_function_info = true },
|
options = { more_function_info = true },
|
||||||
expect = 'function () ... end',
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 237]\n\n\t...\nend'
|
||||||
}
|
}
|
||||||
|
|
||||||
do
|
do
|
||||||
|
@ -242,7 +245,14 @@ do
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
input = function () index = index + 1; return index end,
|
input = function () index = index + 1; return index end,
|
||||||
expect = 'function ()\n\t-- source_file = \'./test/test_pretty.lua\'\n\t-- up_values = { index = 0 }\n\n\t...\nend'
|
expect = 'function () ... end'
|
||||||
|
}
|
||||||
|
|
||||||
|
format_test {
|
||||||
|
input = function () index = index + 1; return index end,
|
||||||
|
options = { more_function_info = true },
|
||||||
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 252]\n\t-- up_values: { index = 0 }\n\n\t...\nend'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -251,6 +261,35 @@ format_test {
|
||||||
expect = 'function () ... end',
|
expect = 'function () ... end',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format_test {
|
||||||
|
-- More function info allows one to even get the function whole, if it was defined in a string.
|
||||||
|
input = loadstring('return function (a, b) return a + b end')(),
|
||||||
|
options = { more_function_info = true },
|
||||||
|
expect = 'function () return a + b end',
|
||||||
|
}
|
||||||
|
|
||||||
|
format_test {
|
||||||
|
input = function ()
|
||||||
|
-- NOTE: This function must cover 3 lines of code!
|
||||||
|
end,
|
||||||
|
options = { more_function_info = true },
|
||||||
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Lines: 272 - 274]\n\n\t...\nend',
|
||||||
|
}
|
||||||
|
|
||||||
|
format_test {
|
||||||
|
input = function () --[[ NOTE: This function must cover a single line of code! ]] end,
|
||||||
|
options = { more_function_info = true },
|
||||||
|
-- TODO: Make this more general, such that it won't fail when adding more tests above it.
|
||||||
|
expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 281]\n\n\t...\nend',
|
||||||
|
}
|
||||||
|
|
||||||
|
format_test {
|
||||||
|
input = function () end,
|
||||||
|
options = { more_function_info = true, _all_function_info = true },
|
||||||
|
expect = '',
|
||||||
|
}
|
||||||
|
|
||||||
format_test {
|
format_test {
|
||||||
input = pairs,
|
input = pairs,
|
||||||
expect = 'builtin function (...) ... end',
|
expect = 'builtin function (...) ... end',
|
||||||
|
|
Loading…
Reference in New Issue
Block a user