Skip to content

Commit

Permalink
Merge pull request #30 from pguyot/w33/fix-specs-multiple-clauses
Browse files Browse the repository at this point in the history
Fix crash of edown with functions having several returns tags
  • Loading branch information
uwiger committed Oct 7, 2023
2 parents cb1386e + e0201c8 commit 4a0a843
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions src/edown_layout.erl
Original file line number Diff line number Diff line change
Expand Up @@ -418,22 +418,12 @@ functions(Fs, Opts) ->

function(Name, E=#xmlElement{content = Es}, Opts) ->
FHead = function_header(Name, E, " *"),
Defs = function_defs(E, Opts),
%% (label_anchor(FHead, E)
(label_anchor("", E)
++ [{h3, [lists:flatten(FHead)]},
{p, []},
{p,
case typespec(get_content(typespec, Es), Opts) of
[] ->
signature(get_content(args, Es),
get_attrval(name, E));
Spec -> Spec
end},
{p, []}]
++ case params(get_content(args, Es)) of
[] -> [];
Ps -> [{p, Ps}]
end
++ Defs
++ case returns(get_content(returns, Es)) of
[] -> [];
Rs -> [{p, Rs}]
Expand Down Expand Up @@ -523,6 +513,38 @@ typespec(Es, Opts) ->
[Type] = get_elem(type, Es),
format_spec(Name, Type, Defs, Opts) ++ local_defs(Defs, Opts).

function_defs(E=#xmlElement{content = Es}, Opts) ->
AllTypespecs = get_elem(typespec, Es),
AllArgs = get_elem(args, Es),
case function_defs1(E, AllTypespecs, AllArgs, Opts) of
%% Work around the behavior of lists:flatten() when we pass it a list
%% with a single item: it wants to flatten sub-items in that item.
[SingleElem] -> SingleElem;
ManyElems -> lists:flatten(ManyElems)
end.

function_defs1(E, [], [#xmlElement{content = ArgsContent}], _Opts) ->
%% Signature + one set of args.
[{p, signature(ArgsContent, get_attrval(name, E))},
{p, []}]
++ case params(ArgsContent) of
[] -> [];
Ps -> [{p, Ps}]
end;
function_defs1(_E, AllTypespecs, AllArgs, Opts) ->
%% Typespec/args combinations.
Pairs = lists:zip(AllTypespecs, AllArgs),
[begin
[{p, typespec(TypespecContent, Opts)},
{p, []}]
++ case params(ArgsContent) of
[] -> [];
Ps -> [{p, Ps}]
end
end
|| {#xmlElement{content = TypespecContent},
#xmlElement{content = ArgsContent}} <- Pairs].

%% <!ELEMENT typedecl (typedef, description?)>
%% <!ELEMENT typedef (erlangName, argtypes, type?, localdef*)>

Expand Down Expand Up @@ -1007,6 +1029,11 @@ get_content(Name, Es) ->
case get_elem(Name, Es) of
[#xmlElement{content = Es1}] ->
Es1;
% Workaround a bug in edoc where several returns tags are generated
% when there are several specification clauses
% See: https://github.com/erlang/otp/issues/7576
[#xmlElement{content = Es1}, #xmlElement{} | _] when Name =:= returns ->
Es1;
[] -> []
end.

Expand Down

0 comments on commit 4a0a843

Please sign in to comment.