Skip to content

Unify the way optional params are displayed. #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ldoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ldoc, a documentation generator for Lua, vs 1.4.0
-M,--merge allow module merging
-S,--simple no return or params, no summary
-O,--one one-column output layout
-P,--plain_params don't show brackets or default values in param lists
--dump debug output dump
--filter (default none) filter output as Lua data (e.g pl.pretty.dump)
--tags (default none) show all references to given tags, comma-separated
Expand Down Expand Up @@ -200,7 +201,7 @@ local ldoc_contents = {
'boilerplate','merge', 'wrap', 'not_luadoc', 'template_escape','merge_error_groups',
'no_return_or_parms','no_summary','full_description','backtick_references', 'custom_see_handler',
'no_space_before_args','parse_extra','no_lua_ref','sort_modules','use_markdown_titles',
'unqualified',
'plain_params', 'unqualified',
}
ldoc_contents = tablex.makeset(ldoc_contents)

Expand Down Expand Up @@ -317,6 +318,9 @@ if type(ldoc.custom_tags) == 'table' then -- custom tags
end
end -- custom tags

override 'plain_params'
ldoc.plain_params = args.plain_params

local source_dir = args.file
if type(source_dir) == 'table' then
source_dir = source_dir[1]
Expand Down
19 changes: 14 additions & 5 deletions ldoc/doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -791,21 +791,30 @@ function split_iden (name)
end

function build_arg_list (names,pmods)
-- if plain_params option is set, return a simple parameter list.
if doc.ldoc.plain_params then
return '(' .. table.concat(names, ', ') .. ')'
end
-- build up the string representation of the argument list,
-- using any opt and optchain modifiers if present.
-- For instance, '(a [, b])' if b is marked as optional
-- with @param[opt] b
local buffer, npending = { }, 0
local function acc(x) table.insert(buffer, x) end
local function get_opt(m)
return m and (m.opt or m.type and not not m.type:match '^%?')
end
-- a number of trailing [opt]s can be safely converted to [opt],[optchain],...
if pmods then
local m = pmods[#names]
if m and m.opt then
m.optchain = m.opt
local opt = get_opt(m)
if opt then
m.optchain = opt
for i = #names-1,1,-1 do
m = pmods[i]
if not m or not m.opt then break end
m.optchain = m.opt
opt = get_opt(m)
if not opt then break end
m.optchain = opt
end
end
end
Expand All @@ -817,7 +826,7 @@ function build_arg_list (names,pmods)
acc ((']'):rep(npending))
npending=0
end
opt = m.optchain or m.opt
opt = m.optchain or get_opt(m)
if opt then
acc('[')
npending=npending+1
Expand Down
9 changes: 7 additions & 2 deletions ldoc/html.lua
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ function html.generate_output(ldoc, args, project)
return type(t) == 'table' and t.append
end

function ldoc.typename (tp)
if not tp or tp == '' or tp:match '^@' then return '' end
function ldoc.typename (tp, pmods)
if not tp or tp == '' or tp:match '^@' then
return pmods and pmods.opt and 'optional' or ''
end
local optional
-- ?<type> is short for ?nil|<type>
if tp:match("^%?") and not tp:match '|' then
Expand All @@ -194,6 +196,9 @@ function html.generate_output(ldoc, args, project)
tp = tp2
end

if pmods and pmods.opt then
optional = true
end
local types = {}
for name in tp:gmatch("[^|]+") do
local sym = name:match '([%w%.%:]+)'
Expand Down
4 changes: 3 additions & 1 deletion ldoc/html/ldoc_ltp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ return [==[
<ul>
# end
# for p in iter(param) do
# local name,tp,def = item:display_name_of(p), ldoc.typename(item:type_of_param(p)), item:default_of_param(p)
# local name = item:display_name_of(p)
# local tp = ldoc.typename(item:type_of_param(p), item:param_modifiers(p))
# local def = item:default_of_param(p)
<li><span class="parameter">$(name)</span>
# if tp ~= '' then
<span class="types">$(tp)</span>
Expand Down