Skip to content
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

Update formatters with changes from OTP 26 #364

Merged
merged 13 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
10 changes: 5 additions & 5 deletions .github/workflows/erlang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ jobs:

strategy:
matrix:
otp: ['24.3', '25.2.1']
rebar: ['3.20.0']
otp: ['25', '26']
rebar: ['3.24']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
id: setup-beam
with:
otp-version: ${{matrix.otp}}
rebar3-version: ${{matrix.rebar}}
- name: Restore _build
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: _build
key: _build-cache-for-os-${{runner.os}}-otp-${{steps.setup-beam.outputs.otp-version}}-rebar3-${{steps.setup-beam.outputs.rebar3-version}}-hash-${{hashFiles('rebar.lock')}}
- name: Restore rebar3's cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.cache/rebar3
key: rebar3-cache-for-os-${{runner.os}}-otp-${{steps.setup-beam.outputs.otp-version}}-rebar3-${{steps.setup-beam.outputs.rebar3-version}}-hash-${{hashFiles('rebar.lock')}}
Expand Down
13 changes: 7 additions & 6 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{erl_opts, [warn_unused_import, warn_export_vars, verbose, report, debug_info]}.

{minimum_otp_vsn, "24"}.
{minimum_otp_vsn, "25"}.

{deps, [{katana_code, "~> 2.1.0"}]}.
{deps, [{katana_code, "~> 2.2.0"}]}.

{ex_doc,
[{source_url, <<"https://github.com/AdRoll/rebar3_format">>},
Expand All @@ -13,12 +13,13 @@

{project_plugins,
[{rebar3_hex, "~> 7.0.8"},
{rebar3_hank, "~> 1.4.0"},
{rebar3_lint, "~> 3.2.5"},
{rebar3_hank, "~> 1.4.1"},
{rebar3_lint, "~> 3.2.6"},
{rebar3_sheldon, "~> 0.4.3"},
{rebar3_ex_doc, "~> 0.2.23"}]}.
{rebar3_ex_doc, "~> 0.2.24"}]}.

{dialyzer, [{warnings, [no_return, unmatched_returns, error_handling, underspecs]}]}.
{dialyzer,
[{warnings, [no_unknown, no_return, unmatched_returns, error_handling, underspecs]}]}.

{edoc_opts, [{todo, true}, {overview, "priv/overview.edoc"}]}.

Expand Down
6 changes: 3 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{"1.2.0",
[{<<"katana_code">>,{pkg,<<"katana_code">>,<<"2.1.0">>},0}]}.
[{<<"katana_code">>,{pkg,<<"katana_code">>,<<"2.2.0">>},0}]}.
[
{pkg_hash,[
{<<"katana_code">>, <<"0C42BDCD7E59995876AED9F678CF62E3D12EF42E0FBB2190556E64BFEBDD15C6">>}]},
{<<"katana_code">>, <<"B67EAA70022702AFEC2803E33C89ABE3D817197E7A16246614FED42E03AA8753">>}]},
{pkg_hash_ext,[
{<<"katana_code">>, <<"AE3BBACA187511588F69695A9FF22251CB2CC672FDCCC180289779BDD25175EF">>}]}
{<<"katana_code">>, <<"A01F9FDBA838478FDFDE183A324FA12ECE9BA127935065395D01427FC5110CBB">>}]}
].
27 changes: 19 additions & 8 deletions src/formatters/default_formatter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -594,18 +594,17 @@ lay_no_comments(Node, Ctxt) ->
Ctxt1 = reset_prec(Ctxt),
D1 = lay(erl_syntax:generator_pattern(Node), Ctxt1),
D2 = lay(erl_syntax:generator_body(Node), Ctxt1),
prettypr:par([D1,
prettypr:beside(
prettypr:text("<- "), D2)],
Ctxt1#ctxt.break_indent);
lay_generator(D1, D2, "<- ", Ctxt1);
binary_generator ->
Ctxt1 = reset_prec(Ctxt),
D1 = lay(erl_syntax:binary_generator_pattern(Node), Ctxt1),
D2 = lay(erl_syntax:binary_generator_body(Node), Ctxt1),
prettypr:par([D1,
prettypr:beside(
prettypr:text("<= "), D2)],
Ctxt1#ctxt.break_indent);
lay_generator(D1, D2, "<= ", Ctxt1);
map_generator ->
Ctxt1 = reset_prec(Ctxt),
D1 = lay(erl_syntax:map_generator_pattern(Node), Ctxt1),
D2 = lay(erl_syntax:map_generator_body(Node), Ctxt1),
lay_generator(D1, D2, "<- ", Ctxt1);
implicit_fun ->
D = lay(erl_syntax:implicit_fun_name(Node), reset_prec(Ctxt)),
prettypr:beside(lay_text_float("fun "), D);
Expand All @@ -621,6 +620,12 @@ lay_no_comments(Node, Ctxt) ->
D2 = lay_items(erl_syntax:binary_comp_body(Node), Ctxt1, fun lay/2),
D3 = prettypr:beside(lay_text_float("|| "), prettypr:beside(D2, lay_text_float(" >>"))),
prettypr:beside(lay_text_float("<< "), prettypr:par([D1, D3]));
map_comp ->
Ctxt1 = reset_prec(Ctxt),
D1 = lay(erl_syntax:map_comp_template(Node), Ctxt1),
D2 = lay_items(erl_syntax:map_comp_body(Node), Ctxt1, fun lay/2),
D3 = prettypr:beside(lay_text_float("|| "), prettypr:beside(D2, lay_text_float("}"))),
prettypr:beside(lay_text_float("#{"), prettypr:par([D1, D3]));
macro ->
%% This is formatted similar to a normal function call or a variable
N = macro_name(Node, variable),
Expand Down Expand Up @@ -1084,6 +1089,12 @@ maybe_convert_to_qualifier(Node, #ctxt{force_arity_qualifiers = true}) ->
Node
end.

lay_generator(D1, D2, Connector, Ctxt) ->
prettypr:par([D1,
prettypr:beside(
prettypr:text(Connector), D2)],
Ctxt#ctxt.break_indent).

lay_nested_infix_expr(Node, #ctxt{parenthesize_infix_operations = false} = Ctxt) ->
lay(Node, Ctxt);
lay_nested_infix_expr(Node, #ctxt{parenthesize_infix_operations = true} = Ctxt) ->
Expand Down
11 changes: 11 additions & 0 deletions src/formatters/otp_formatter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ lay_no_comments(Node, Ctxt) ->
D1 = lay(erl_syntax:binary_generator_pattern(Node), Ctxt1),
D2 = lay(erl_syntax:binary_generator_body(Node), Ctxt1),
par([D1, beside(text("<= "), D2)], Ctxt1#ctxt.break_indent);
map_generator ->
Ctxt1 = reset_prec(Ctxt),
D1 = lay(erl_syntax:map_generator_pattern(Node), Ctxt1),
D2 = lay(erl_syntax:map_generator_body(Node), Ctxt1),
par([D1, beside(text("<- "), D2)], Ctxt1#ctxt.break_indent);
implicit_fun ->
D = lay(erl_syntax:implicit_fun_name(Node), reset_prec(Ctxt)),
beside(lay_text_float("fun "), D);
Expand All @@ -679,6 +684,12 @@ lay_no_comments(Node, Ctxt) ->
D2 = par(seq(erl_syntax:binary_comp_body(Node), lay_text_float(","), Ctxt1, fun lay/2)),
beside(lay_text_float("<< "),
par([D1, beside(lay_text_float(" || "), beside(D2, lay_text_float(" >>")))]));
map_comp ->
Ctxt1 = set_prec(Ctxt, max_prec()),
D1 = lay(erl_syntax:map_comp_template(Node), Ctxt1),
D2 = par(seq(erl_syntax:map_comp_body(Node), lay_text_float(","), Ctxt1, fun lay/2)),
beside(lay_text_float("#{"),
par([D1, beside(lay_text_float("|| "), beside(D2, lay_text_float("}")))]));
macro ->
%% This is formatted similar to a normal function call, but
%% prefixed with a "?".
Expand Down
8 changes: 4 additions & 4 deletions test_app/after/src/paper_and_ribbon/indent_18.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-module(indent_18).

-format #{break_indent => 1,
inline_clause_bodies => true,
paper => 50,
sub_indent => 8}.
-format #{break_indent => 1}.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and other similar changes are due to a subtle bug (fixed in OTP26's erl_syntax) that reordered map items when they were found in attributes. I decided to just put the items in the order in which erl_syntax returns them (thus, hiding the bug in OTP25) so that tests run seamlessly in 25 and 26.

-format #{inline_clause_bodies => true}.
-format #{paper => 50}.
-format #{sub_indent => 8}.

-record(record,
{fields =
Expand Down
6 changes: 3 additions & 3 deletions test_app/after/src/paper_and_ribbon/indent_81.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-module(indent_81).

-format #{break_indent => 8,
paper => 50,
sub_indent => 1}.
-format #{break_indent => 8}.
-format #{paper => 50}.
-format #{sub_indent => 1}.
-format #{inline_clause_bodies => true}.

-record(record,
Expand Down
2 changes: 1 addition & 1 deletion test_app/src/brackets.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(brackets).

-format(#{paper => 50, inline_items => all}).
-format(#{inline_items => all, paper => 50}).

-compile(export_all).

Expand Down
3 changes: 1 addition & 2 deletions test_app/src/inline_items/inline_no_items.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%% per line if they're large.
-module inline_no_items.

-format #{paper => 80, inline_items => none}.
-format #{inline_items => none, paper => 80}.

-export [short_tuple/0, short_list/0, short_fun/0].
-export [short_bin/0, short_guard/1, short_lc/0].
Expand Down Expand Up @@ -106,4 +106,3 @@ exact() ->

long() ->
[these, items, should, be, inlined, they, are, more, than, 25, $., 'We', can, be, sure, about, that, because, we, added, a, very, long, number, 'of', items, to, this, list].

8 changes: 4 additions & 4 deletions test_app/src/paper_and_ribbon/indent_18.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-module(indent_18).

-format(#{break_indent => 1,
sub_indent => 8,
paper => 50,
inline_clause_bodies => true}).
-format(#{break_indent => 1}).
-format(#{inline_clause_bodies => true}).
-format(#{paper => 50}).
-format(#{sub_indent => 8}).

-record(
record, {fields = should:be(indented_using:break_indent(1)),
Expand Down
4 changes: 3 additions & 1 deletion test_app/src/paper_and_ribbon/indent_81.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-module(indent_81).

-format(#{break_indent => 8, sub_indent => 1, paper => 50}).
-format(#{break_indent => 8}).
-format(#{paper => 50}).
-format(#{sub_indent => 1}).
-format(#{inline_clause_bodies => true}).

-record(
Expand Down
Loading