Skip to content

Commit

Permalink
Allow erlfmt:ignore to be used in addition to erlfmt-ignore
Browse files Browse the repository at this point in the history
Summary:
This puts erlfmt in line with other tools that use the format `toolname:ignore` to disable warnings, and makes it easier for developers to "guess" the correct incantation
  • Loading branch information
jcpetruzza authored and michalmuskala committed Aug 28, 2024
1 parent 81b191d commit 6a5651e
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 26 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ which after re-formatting will result in the original layout again.
## Ignoring Formatting
We found that mostly it is possible to format erlang code in an at least somewhat acceptable way, but exceptions do occur.
We have introduced the `erlfmt-ignore` comment, which when placed before a top-level expression, will indicate to `erlfmt` to skip over that expression, leave it as is and move on to the next expression. For documentation purposes, a reason for not formatting can be given..
We have introduced the `erlfmt:ignore` comment, which when placed before a top-level expression, will indicate to `erlfmt` to skip over that expression, leave it as is and move on to the next expression. For documentation purposes, a reason for not formatting can be given..
```erlang formatted matrix
%% erlfmt-ignore I like it more this way
%% erlfmt:ignore I like it more this way
-define(DELTA_MATRIX, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
Expand All @@ -372,16 +372,16 @@ We have introduced the `erlfmt-ignore` comment, which when placed before a top-l
]).
```
You can also encose multiple top-level forms in a `erlfmt-ignore-begin`, `erlfmt-ignore-end` section.
You can also encose multiple top-level forms in a `erlfmt:ignore-begin`, `erlfmt:ignore-end` section.
```erlang formatted ignore-many
%% erlfmt-ignore-begin
%% erlfmt:ignore-begin
-define(DELTA_MATRIX1, [
[0, 0, 0, 0, 0, 0]
]).
-define(DELTA_MATRIX2, [
[0, 0, 0, 0, 0, 0]
]).
%% erlfmt-ignore-end
%% erlfmt:ignore-end
-define(THIS_IS_FORMATTED, ok).
```
Expand Down
18 changes: 16 additions & 2 deletions src/erlfmt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ ignore_state([], _FileName, Acc) ->
ignore_state([Line | Lines], FileName, Loc, Rest, Acc0) ->
Acc =
case string:trim(Line, leading, "% ") of
% Old-style erlfmt-ignore
"erlfmt-ignore" ++ R when ?IS_IGNORE_REASON(R), Acc0 =:= false ->
ignore;
"erlfmt-ignore" ++ R when ?IS_IGNORE_REASON(R) ->
Expand All @@ -572,6 +573,19 @@ ignore_state([Line | Lines], FileName, Loc, Rest, Acc0) ->
'end';
"erlfmt-ignore-end" ++ R when ?IS_IGNORE_REASON(R) ->
throw({error, {FileName, Loc, ?MODULE, {invalid_ignore, 'end', Acc0}}});
% New-style erlfmt:ignore
"erlfmt:ignore" ++ R when ?IS_IGNORE_REASON(R), Acc0 =:= false ->
ignore;
"erlfmt:ignore" ++ R when ?IS_IGNORE_REASON(R) ->
throw({error, {FileName, Loc, ?MODULE, {invalid_ignore, ignore, Acc0}}});
"erlfmt:ignore-begin" ++ R when ?IS_IGNORE_REASON(R), Acc0 =:= false ->
'begin';
"erlfmt:ignore-begin" ++ R when ?IS_IGNORE_REASON(R) ->
throw({error, {FileName, Loc, ?MODULE, {invalid_ignore, 'begin', Acc0}}});
"erlfmt:ignore-end" ++ R when ?IS_IGNORE_REASON(R), Acc0 =:= 'begin' ->
'end';
"erlfmt:ignore-end" ++ R when ?IS_IGNORE_REASON(R) ->
throw({error, {FileName, Loc, ?MODULE, {invalid_ignore, 'end', Acc0}}});
_ ->
Acc0
end,
Expand Down Expand Up @@ -698,11 +712,11 @@ format_error(could_not_reparse) ->
format_error({long_line, Length, Width}) ->
io_lib:format("line too long (~p > ~p)", [Length, Width]);
format_error({invalid_ignore, ignore, 'begin'}) ->
"invalid erlfmt-ignore while in erlfmt-ignore-begin section";
"invalid erlfmt:ignore while in erlfmt:ignore-begin section";
format_error({invalid_ignore, Same, Same}) ->
"duplicate ignore comment";
format_error({invalid_ignore, 'end', false}) ->
"invalid erlfmt-ignore-end while outside of erlfmt-ignore-begin section";
"invalid erlfmt:ignore-end while outside of erlfmt:ignore-begin section";
format_error({invalid_ignore, Given, Previous}) ->
io_lib:format("invalid ignore specification ~ts while in ~ts state", [Given, Previous]).

Expand Down
54 changes: 51 additions & 3 deletions test/erlfmt_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,19 @@
snapshot_script/1,
snapshot_ignore_format/1,
snapshot_ignore_format_many/1,
snapshot_ignore_format_old/1,
snapshot_ignore_format_many_old/1,
snapshot_empty/1,
format_string_unicode/1,
error_ignore_begin_ignore/1,
error_ignore_begin_ignore_begin/1,
error_ignore_end/1,
error_ignore_ignore/1,
error_ignore_ignore_old/1,
error_ignore_begin_ignore_old/1,
error_ignore_begin_ignore_begin_old/1,
error_ignore_end_old/1,
error_ignore_ignore_old/1,
simple_comments_range/1,
broken_range/1,
snapshot_range_whole_comments/1,
Expand Down Expand Up @@ -163,14 +170,20 @@ groups() ->
snapshot_script,
snapshot_ignore_format,
snapshot_ignore_format_many,
snapshot_ignore_format_old,
snapshot_ignore_format_many_old,
snapshot_empty,
format_string_unicode
]},
{error_tests, [parallel], [
error_ignore_begin_ignore,
error_ignore_begin_ignore_begin,
error_ignore_end,
error_ignore_ignore
error_ignore_ignore,
error_ignore_begin_ignore_old,
error_ignore_begin_ignore_begin_old,
error_ignore_end_old,
error_ignore_ignore_old
]},
{range_tests, [parallel], [
simple_comments_range,
Expand Down Expand Up @@ -1078,6 +1091,10 @@ snapshot_ignore_format(Config) -> snapshot_formatted("ignore_format.erl", Config

snapshot_ignore_format_many(Config) -> snapshot_formatted("ignore_format_many.erl", Config).

snapshot_ignore_format_old(Config) -> snapshot_formatted("ignore_format_old.erl", Config).

snapshot_ignore_format_many_old(Config) -> snapshot_formatted("ignore_format_many_old.erl", Config).

snapshot_empty(Config) -> snapshot_same("empty.erl", Config).

snapshot_insert_pragma_with(Config) when is_list(Config) ->
Expand Down Expand Up @@ -1113,14 +1130,30 @@ format_string_unicode(_) ->
assert_diagnostic:assert_snapshot_match(Original, Output).

error_ignore_begin_ignore(_) ->
assert_error(
"% erlfmt:ignore-begin\n"
"% erlfmt:ignore\n"
"foo() -> ok.\n",
"nofile:1:1: invalid erlfmt:ignore while in erlfmt:ignore-begin section"
).

error_ignore_begin_ignore_old(_) ->
assert_error(
"% erlfmt-ignore-begin\n"
"% erlfmt-ignore\n"
"foo() -> ok.\n",
"nofile:1:1: invalid erlfmt-ignore while in erlfmt-ignore-begin section"
"nofile:1:1: invalid erlfmt:ignore while in erlfmt:ignore-begin section"
).

error_ignore_begin_ignore_begin(_) ->
assert_error(
"% erlfmt:ignore-begin\n"
"% erlfmt:ignore-begin\n"
"foo() -> ok.\n",
"nofile:1:1: duplicate ignore comment"
).

error_ignore_begin_ignore_begin_old(_) ->
assert_error(
"% erlfmt-ignore-begin\n"
"% erlfmt-ignore-begin\n"
Expand All @@ -1129,13 +1162,28 @@ error_ignore_begin_ignore_begin(_) ->
).

error_ignore_end(_) ->
assert_error(
"% erlfmt:ignore-end\n"
"foo() -> ok.\n",
"nofile:1:1: invalid erlfmt:ignore-end while outside of erlfmt:ignore-begin section"
).

error_ignore_end_old(_) ->
assert_error(
"% erlfmt-ignore-end\n"
"foo() -> ok.\n",
"nofile:1:1: invalid erlfmt-ignore-end while outside of erlfmt-ignore-begin section"
"nofile:1:1: invalid erlfmt:ignore-end while outside of erlfmt:ignore-begin section"
).

error_ignore_ignore(_) ->
assert_error(
"% erlfmt:ignore\n"
"% erlfmt:ignore\n"
"foo() -> ok.\n",
"nofile:1:1: duplicate ignore comment"
).

error_ignore_ignore_old(_) ->
assert_error(
"% erlfmt-ignore\n"
"% erlfmt-ignore\n"
Expand Down
6 changes: 3 additions & 3 deletions test/erlfmt_SUITE_data/ignore_format.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(ignore_format).

%%% erlfmt-ignore
%%% erlfmt:ignore
-define(DELTA_MATRIX, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
Expand All @@ -20,7 +20,7 @@
]).

%% some comment
%%erlfmt-ignore %
%%erlfmt:ignore %
%% another comment
gen_part_decode_funcs({constructed,bif},TypeName,
{_Name,parts,Tag,_Type}) ->
Expand All @@ -33,7 +33,7 @@ gen_part_decode_funcs({constructed,bif},TypeName,
" Res",nl,
" end"]).

% erlfmt-ignore I like the comment next to the statement
% erlfmt:ignore I like the comment next to the statement
f() -> ok. % this is ok

%% TODO write emit
Expand Down
6 changes: 3 additions & 3 deletions test/erlfmt_SUITE_data/ignore_format.erl.formatted
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(ignore_format).

%%% erlfmt-ignore
%%% erlfmt:ignore
-define(DELTA_MATRIX, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
Expand All @@ -20,7 +20,7 @@
]).

%% some comment
%%erlfmt-ignore %
%%erlfmt:ignore %
%% another comment
gen_part_decode_funcs({constructed,bif},TypeName,
{_Name,parts,Tag,_Type}) ->
Expand All @@ -33,7 +33,7 @@ gen_part_decode_funcs({constructed,bif},TypeName,
" Res",nl,
" end"]).

% erlfmt-ignore I like the comment next to the statement
% erlfmt:ignore I like the comment next to the statement
f() -> ok. % this is ok

%% TODO write emit
Expand Down
10 changes: 5 additions & 5 deletions test/erlfmt_SUITE_data/ignore_format_many.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(ignore_format_many).

%%% erlfmt-ignore-begin
%%% erlfmt:ignore-begin
-define(DELTA_MATRIX, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
Expand All @@ -11,7 +11,7 @@
]).
-define(ALSO_IGNORED, [ 1, 2, 3]).

%%% erlfmt-ignore-end
%%% erlfmt:ignore-end
-define(DELTA_MATRIX_FORMATTED, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
Expand All @@ -22,7 +22,7 @@
]).

%% some comment
%%erlfmt-ignore %
%%erlfmt:ignore %
%% another comment
gen_part_decode_funcs({constructed,bif},TypeName,
{_Name,parts,Tag,_Type}) ->
Expand All @@ -35,10 +35,10 @@ gen_part_decode_funcs({constructed,bif},TypeName,
" Res",nl,
" end"]).

% erlfmt-ignore-begin I like the comments next to the statement
% erlfmt:ignore-begin I like the comments next to the statement
f() -> ok. % this is ok
g() -> ok. % this is also ok
% erlfmt-ignore-end I'm done with this style
% erlfmt:ignore-end I'm done with this style

h() -> ok. % blah

Expand Down
10 changes: 5 additions & 5 deletions test/erlfmt_SUITE_data/ignore_format_many.erl.formatted
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(ignore_format_many).

%%% erlfmt-ignore-begin
%%% erlfmt:ignore-begin
-define(DELTA_MATRIX, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
Expand All @@ -11,7 +11,7 @@
]).
-define(ALSO_IGNORED, [ 1, 2, 3]).

%%% erlfmt-ignore-end
%%% erlfmt:ignore-end
-define(DELTA_MATRIX_FORMATTED, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
Expand All @@ -22,7 +22,7 @@
]).

%% some comment
%%erlfmt-ignore %
%%erlfmt:ignore %
%% another comment
gen_part_decode_funcs({constructed,bif},TypeName,
{_Name,parts,Tag,_Type}) ->
Expand All @@ -35,10 +35,10 @@ gen_part_decode_funcs({constructed,bif},TypeName,
" Res",nl,
" end"]).

% erlfmt-ignore-begin I like the comments next to the statement
% erlfmt:ignore-begin I like the comments next to the statement
f() -> ok. % this is ok
g() -> ok. % this is also ok
% erlfmt-ignore-end I'm done with this style
% erlfmt:ignore-end I'm done with this style

% blah
h() -> ok.
Expand Down
46 changes: 46 additions & 0 deletions test/erlfmt_SUITE_data/ignore_format_many_old.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-module(ignore_format_many_old).

%%% erlfmt-ignore-begin
-define(DELTA_MATRIX, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
[0, 0, 15, 0, 0, 0],
[0, 0, 0, 6, 0, 0],
[0, -16, 0, 0, -14, 0],
[0, 0, 15, 0, 0, 0]
]).
-define(ALSO_IGNORED, [ 1, 2, 3]).

%%% erlfmt-ignore-end
-define(DELTA_MATRIX_FORMATTED, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
[0, 0, 15, 0, 0, 0],
[0, 0, 0, 6, 0, 0],
[0, -16, 0, 0, -14, 0],
[0, 0, 15, 0, 0, 0]
]).

%% some comment
%%erlfmt-ignore %
%% another comment
gen_part_decode_funcs({constructed,bif},TypeName,
{_Name,parts,Tag,_Type}) ->
emit([" case Data of",nl,
" L when is_list(L) ->",nl,
" 'dec_",TypeName,"'(lists:map(fun(X) -> element(1, ",
{call,ber,ber_decode_erlang,["X"]},") end, L),",{asis,Tag},");",nl,
" _ ->",nl,
" [Res] = 'dec_",TypeName,"'([Data],",{asis,Tag},"),",nl,
" Res",nl,
" end"]).

% erlfmt-ignore-begin I like the comments next to the statement
f() -> ok. % this is ok
g() -> ok. % this is also ok
% erlfmt-ignore-end I'm done with this style

h() -> ok. % blah

%% TODO write emit
emit(S) -> ok.
Loading

0 comments on commit 6a5651e

Please sign in to comment.