Skip to content

Commit 4bc30f1

Browse files
committed
customise format
1 parent 7cae13d commit 4bc30f1

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/erlfmt.erl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,14 @@ format_nodes_loop([], _PrintWidth) ->
615615
[].
616616

617617
maybe_empty_line(Node, Next) ->
618-
case has_empty_line_between(Node, Next) of
619-
true -> "\n";
620-
false -> ""
618+
case should_add_extra_newlines(Node, Next) of
619+
two -> "\n\n"; % Always add two newlines
620+
one -> "\n"; % Always add one newline
621+
zero ->
622+
case has_empty_line_between(Node, Next) of
623+
true -> "\n";
624+
false -> ""
625+
end
621626
end.
622627

623628
-spec format_node(erlfmt_parse:abstract_node(), pos_integer()) -> unicode:chardata().
@@ -630,6 +635,21 @@ format_node(Node, PrintWidth) ->
630635
has_empty_line_between(Left, Right) ->
631636
erlfmt_scan:get_end_line(Left) + 1 < erlfmt_scan:get_line(Right).
632637

638+
%% Determine if we should add extra newlines between nodes based on their types
639+
should_add_extra_newlines(Node, Next) ->
640+
case {node_type(Node), node_type(Next)} of
641+
{spec, function} -> one; % Add one newline between spec and function
642+
{function, spec} -> two; % Add two newlines between function and next spec
643+
_ -> zero
644+
end.
645+
646+
%% Get the type of a node for spacing decisions
647+
node_type({attribute, _, spec, _}) -> spec;
648+
node_type({attribute, _, callback, _}) -> spec; % Treat callback same as spec
649+
node_type({function, _, _}) -> function;
650+
node_type({raw_string, _, _}) -> raw_string;
651+
node_type(_) -> other.
652+
633653
verify_nodes(FileName, Nodes, Formatted) ->
634654
Flattened = unicode:characters_to_list(Formatted),
635655
case read_nodes_string(FileName, Flattened) of

src/erlfmt_format.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ fold_clauses_to_algebra([Clause]) ->
770770
clause_expr_to_algebra(Clause);
771771
fold_clauses_to_algebra([Clause | Clauses]) ->
772772
ClauseD = clause_expr_to_algebra(Clause),
773-
line(concat(ClauseD, <<";">>), fold_clauses_to_algebra(Clauses)).
773+
concat([concat(ClauseD, <<";">>), line(2), fold_clauses_to_algebra(Clauses)]).
774774

775775
clause_has_break({clause, _Meta, empty, Guards, [Body | _]}) ->
776776
has_break_between(Guards, Body);

0 commit comments

Comments
 (0)