1
0

Chunks are now formatted correctly.

This commit is contained in:
Jon Michael Aanes 2017-07-22 15:09:20 +02:00
parent 9a46a959e9
commit e0552daf91
3 changed files with 41 additions and 20 deletions

View File

@ -195,27 +195,40 @@ local function get_function_body_info (info)
if not str then return info end if not str then return info end
-- Calculate indices of the lines the function should be defined at.
local start_line_index = get_line_index(str, info.linedefined)
local end_line_index = get_line_index(str, info.lastlinedefined + 1)
-- Now find some info about the function. -- Now find some info about the function.
-- NOTE: function_params is currently not used for anything. -- NOTE: function_params is currently not used for anything.
local function_name, function_params, function_body = str:sub(start_line_index, end_line_index):match(FUNCTION_DEFINITION_MATCH) local function_name, function_params, function_body
if type(function_body) ~= 'string' then if info.linedefined == 0 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)) -- A function is a "chunk" when linedefined is equal to 0.
-- This is a "toplevel" function. One without a function
-- definition. The entire string is it's function body.
function_name = ''
function_body = str
else
-- This is not a chunk. Look for function definition.
-- Calculate indices of the lines the function should be defined at.
local start_line_index = get_line_index(str, info.linedefined)
local end_line_index = get_line_index(str, info.lastlinedefined + 1)
function_name, function_params, function_body = str:sub(start_line_index, end_line_index):match(FUNCTION_DEFINITION_MATCH)
-- Throw an error if we can't find anything.
if 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
end end
local function_body = function_body:match '^%s*(.-)%s*$' local function_body = function_body:match '^%s*(.-)%s*$'
local pivot_index = get_docs_split_index (function_body) local pivot_index = get_docs_split_index (function_body)
info.name = info.name or function_name:match '^%s*(.-)%s*$' info.name = info.name or function_name:match '^%s*(.-)%s*$'
info.docs = info.docs or get_docs_from_function_body(function_body, pivot_index) info.docs = info.docs or get_docs_from_function_body(function_body, pivot_index)
info.body = info.body or function_body:sub(pivot_index, -1):match '^%s*(.-)%s*$' info.body = info.body or function_body:sub(pivot_index, -1):match '^%s*(.-)%s*$'
if info.name == '' then info.name = nil end if info.name == '' then info.name = nil end
if info.docs == '' then info.docs = nil end if info.docs == '' then info.docs = nil end
return info return info
end end

View File

@ -269,6 +269,20 @@ format_test {
expect = 'function (a, b) return a + b end', expect = 'function (a, b) return a + b end',
} }
format_test {
name = 'Embedding loaded chunk',
single = true,
input = loadstring 'return 42',
expect = 'function (...) return 42 end',
}
format_test {
name = 'Embedding loaded file',
single = true,
input = loadfile 'init.lua',
expect = 'function (...) ... end',
}
format_test { format_test {
name = 'Embedding nested function, when on same line is too hard 1', name = 'Embedding nested function, when on same line is too hard 1',
single = true, single = true,

View File

@ -341,12 +341,6 @@ SUITE:addTest('UseCase: Can load function from file that is shortly deleted', fu
assert(true) assert(true)
end) end)
SUITE:addTest('UseCase: Can use pretty on loadstring block', function ()
-- TODO: Move to more appropriate test module.
format(loadstring 'hi = 0')
assert(true)
end)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
return SUITE return SUITE