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

Fix #263: Support OTP24 #300

Merged
merged 5 commits into from
Apr 11, 2022
Merged
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
7 changes: 2 additions & 5 deletions .github/workflows/erlang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ jobs:

strategy:
matrix:
otp: ['22.3', '23.3']
rebar: ['3.16.1']
include:
- otp: '21.3'
rebar: '3.15.2'
otp: ['22.3', '23.3', '24.3']
rebar: ['3.18.0']

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://github.com/AdRoll/rebar3_format/actions/workflows/erlang.yml/badge.svg)](https://github.com/AdRoll/rebar3_format)
[![Hex pm](http://img.shields.io/hexpm/v/rebar3_format.svg?style=flat)](https://hex.pm/packages/rebar3_format)

A rebar plugin for code formatting
A rebar plugin for code formatting.

## Build

Expand Down
4 changes: 2 additions & 2 deletions src/formatters/default_formatter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,10 @@ lay_no_comments(Node, Ctxt) ->
Es = lay_clause_expressions(erl_syntax:block_expr_body(Node), Ctxt1, fun lay/2),
sep([text("begin"), nest(Ctxt1#ctxt.break_indent, Es), text("end")]);
catch_expr ->
{Prec, PrecR} = preop_prec('catch'),
{_, PrecR} = preop_prec('catch'),
D = lay(erl_syntax:catch_expr_body(Node), set_prec(Ctxt, PrecR)),
D1 = follow(text("catch"), D, Ctxt#ctxt.break_indent),
maybe_parentheses(D1, Prec, Ctxt);
maybe_parentheses(D1, 0, Ctxt);
Comment on lines +512 to +515
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I basically preserved OTP23 and prior value here. Using the one in OTP24 would work, too, but nobody actually complained about the extra parentheses and maintaining a formatter that formats the code consistently is better than maintaining one that produces different output depending on the OTP version with which it was compiled.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Once we stop supporting OTP23… we can revisit this.

class_qualifier ->
Ctxt1 = set_prec(Ctxt, max_prec()),
D1 = lay(erl_syntax:class_qualifier_argument(Node), Ctxt1),
Expand Down
9 changes: 6 additions & 3 deletions src/formatters/otp_formatter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ lay_no_comments(Node, Ctxt) ->
beside(lay_text_float("["),
par([D1, beside(lay_text_float("|| "), beside(D2, lay_text_float("]")))]));
binary_comp ->
Ctxt1 = reset_prec(Ctxt),
Ctxt1 = set_prec(Ctxt, max_prec()),
D1 = lay(erl_syntax:binary_comp_template(Node), Ctxt1),
D2 = par(seq(erl_syntax:binary_comp_body(Node), lay_text_float(","), Ctxt1, fun lay/2)),
beside(lay_text_float("<< "),
Expand Down Expand Up @@ -1192,10 +1192,13 @@ lay_clause_expressions([], _) -> empty().

is_last_and_before_empty_line(H, [H2 | _], #ctxt{empty_lines = EmptyLines}) ->
try
(erl_syntax:get_pos(H2) - erl_syntax:get_pos(H) >= 2)
and sets:is_element(erl_syntax:get_pos(H) + 1, EmptyLines)
(get_line(H2) - get_line(H) >= 2) and sets:is_element(get_line(H) + 1, EmptyLines)
catch
error:badarith -> false
end.

get_line(Tree) ->
Anno = erl_syntax:get_pos(Tree),
erl_anno:line(Anno).

%% =====================================================================
4 changes: 3 additions & 1 deletion test/otp_formatter_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ test_app(_Config) ->
"src/comments.erl",
"src/ignored_file_config.erl",
"src/dodge_macros.erl",
"src/macros_in_specs.erl"]};
"src/macros_in_specs.erl",
"src/receive_after.erl"]};
_ ->
{ignore,
["src/*_ignore.erl",
"src/comments.erl",
"src/ignored_file_config.erl",
"src/dodge_macros.erl",
"src/macros_in_specs.erl",
"src/receive_after.erl",
"src/otp23.erl"]}
end,
State2 = rebar_state:set(State1, format, [Files, Formatter, IgnoredFiles]),
Expand Down
3 changes: 1 addition & 2 deletions test_app/src/empty_lines.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ a_fun(With, Some, Arguments) ->
%%
%% ^ an empty line that is preserved
an:expression(that,

has,
Some,

{empty, lines, inside},
#{and_it => uses, a => [long, list], 'of' => Arguments}),
no_empty_line_above_me.
no_empty_line_above_me.