Skip to content

Commit

Permalink
Allow providing an optional reason when using erlfmt-ignore
Browse files Browse the repository at this point in the history
Right now only trailing spaces and `%` are allowed in an
`erlfmt-ignore`. We make it so that anything after the
`erlfmt-ignore` is discarded, effectively allowing for the
user to document why formatting is disabled
  • Loading branch information
jcpetruzza committed Aug 28, 2024
1 parent d217674 commit 5dba211
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 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.
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
%% erlfmt-ignore I like it more this way
-define(DELTA_MATRIX, [
[0, 0, 0, 0, 0, 0],
[0, -16, 0, 0, 0, 0],
Expand Down
19 changes: 12 additions & 7 deletions src/erlfmt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -555,17 +555,22 @@ ignore_state([{comment, Loc, Comments} | Rest], FileName, Acc) ->
ignore_state([], _FileName, Acc) ->
Acc.

-define(IS_IGNORE_REASON(S), (S =:= [] orelse hd(S) =:= 32 orelse hd(S) =:= $%)).

ignore_state([Line | Lines], FileName, Loc, Rest, Acc0) ->
Acc =
case string:trim(Line, both, "% ") of
"erlfmt-ignore" when Acc0 =:= false -> ignore;
"erlfmt-ignore" ->
case string:trim(Line, leading, "% ") of
"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" when Acc0 =:= false -> 'begin';
"erlfmt-ignore-begin" ->
"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" when Acc0 =:= 'begin' -> 'end';
"erlfmt-ignore-end" ->
"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
Expand Down
3 changes: 3 additions & 0 deletions test/erlfmt_SUITE_data/ignore_format.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ gen_part_decode_funcs({constructed,bif},TypeName,
" Res",nl,
" end"]).

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

%% TODO write emit
emit(S) -> ok.
3 changes: 3 additions & 0 deletions test/erlfmt_SUITE_data/ignore_format.erl.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ gen_part_decode_funcs({constructed,bif},TypeName,
" Res",nl,
" end"]).

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

%% TODO write emit
emit(S) -> ok.
7 changes: 7 additions & 0 deletions test/erlfmt_SUITE_data/ignore_format_many.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,12 @@ gen_part_decode_funcs({constructed,bif},TypeName,
" 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.
8 changes: 8 additions & 0 deletions test/erlfmt_SUITE_data/ignore_format_many.erl.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,13 @@ gen_part_decode_funcs({constructed,bif},TypeName,
" 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

% blah
h() -> ok.

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

0 comments on commit 5dba211

Please sign in to comment.