From f4d6e301a11b05d037a54d701d8c2ca92e26c0a4 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sat, 24 Jun 2017 20:37:43 +0200 Subject: [PATCH] The documentation for builtins will now be wrapped, to prevent extremely long lines. --- function.lua | 20 +++++++++++++++++++- test/test_function.lua | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/function.lua b/function.lua index e991ba6..defd260 100644 --- a/function.lua +++ b/function.lua @@ -186,6 +186,22 @@ local function get_docs_from_function_body (func_body) return table.concat(doc_lines, '\n') end +local function wrap_text (text, max_width) + local l, i, last_i = {}, max_width, 1 + repeat + if text:sub(i, i) == ' ' then + l[#l+1], last_i, i = text:sub(last_i, i - 1), i + 1, i + max_width + elseif i <= last_i then + -- TODO: Make sure this part works. + i = text:find(' ', last_i) or #text + else + i = i - 1 + end + until i >= #text + l[#l+1] = text:sub(last_i) + return table.concat(l, '\n') +end + -------------------------------------------------------------------------------- local function format_function_with_closure (value, depth, l, format_value) @@ -290,7 +306,9 @@ return function (value, depth, l, format_value) if info.doc then l[#l+1] = '\n' - l[#l+1] = add_indent_to_string(info.doc, l.options.indent .. '-- ') + local indent = l.options.indent .. '-- ' + local docs = not info.builtin and info.doc or wrap_text(info.doc, 80 - #indent) + l[#l+1] = add_indent_to_string(docs, indent) end -- source diff --git a/test/test_function.lua b/test/test_function.lua index 41a85ff..92c3fa3 100644 --- a/test/test_function.lua +++ b/test/test_function.lua @@ -204,12 +204,12 @@ format_test { format_test { input = math.random, - expect = 'builtin function ([m [, n])\n -- math.random\n -- When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].\n\n ...\nend', + expect = 'builtin function ([m [, n])\n -- math.random\n -- When called without arguments, returns a uniform pseudo-random real\n -- number in the range [0,1). When called with an integer number m,\n -- math.random returns a uniform pseudo-random integer in the range [1, m].\n -- When called with two integer numbers m and n, math.random returns a\n -- uniform pseudo-random integer in the range [m, n].\n\n ...\nend', } format_test { input = string.byte, - expect = 'builtin function (s [, i [, j]])\n -- string.byte\n -- Returns the internal numerical codes of the characters s[i], s[i+1], ..., s[j]. The default value for i is 1; the default value for j is i.\n -- Note that numerical codes are not necessarily portable across platforms.\n\n ...\nend', + expect = 'builtin function (s [, i [, j]])\n -- string.byte\n -- Returns the internal numerical codes of the characters s[i], s[i+1],\n -- ..., s[j]. The default value for i is 1; the default value for j is\n -- i.\n -- Note that numerical codes are not necessarily portable across\n -- platforms.\n\n ...\nend', } -- short_builtins option: If an builtin is expected to be available by some name