Skip to content

Commit

Permalink
fix reinsert pragma for empty lines (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
awalterschulze authored May 10, 2021
1 parent 3e41623 commit c815036
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/erlfmt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ replace_pragma_comment_block(_Prefix, []) ->
[];
replace_pragma_comment_block(Prefix, ["%% @format" | Tail]) ->
[(Prefix ++ " % @format") | Tail];
replace_pragma_comment_block(_Prefix, [Head | Tail]) ->
replace_pragma_comment_block(_Prefix, [("%" ++ _) = Head | Tail]) ->
{Prefix, _} = string:take(Head, "%"),
[Head | replace_pragma_comment_block(Prefix, Tail)];
replace_pragma_comment_block(Prefix, [Head | Tail]) ->
[Head | replace_pragma_comment_block(Prefix, Tail)].

-spec format_range(
Expand Down
14 changes: 14 additions & 0 deletions test/erlfmt_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
smoke_test_stdio_delete_pragma_with_copyright/1,
smoke_test_stdio_reinsert_pragma/1,
smoke_test_stdio_reinsert_pragma_second/1,
smoke_test_stdio_reinsert_pragma_config/1,
smoke_test_stdio_unicode/1,
smoke_test_stdio_check/1,
exclude_check/1,
Expand Down Expand Up @@ -148,6 +149,7 @@ groups() ->
smoke_test_stdio_delete_pragma_with_copyright,
smoke_test_stdio_reinsert_pragma,
smoke_test_stdio_reinsert_pragma_second,
smoke_test_stdio_reinsert_pragma_config,
smoke_test_stdio_unicode,
smoke_test_stdio_check,
exclude_check
Expand Down Expand Up @@ -1073,6 +1075,18 @@ smoke_test_stdio_reinsert_pragma_second(Config) when is_list(Config) ->
"-module(nopragma).\n",
?assertEqual(Expected, Formatted).

smoke_test_stdio_reinsert_pragma_config(Config) when is_list(Config) ->
Formatted = os:cmd(
"echo '%% @format\n\n%%% actual comment\n{}.\n' | " ++ escript() ++
" - --insert-pragma"
),
Expected =
"%%% % @format\n"
"\n"
"%%% actual comment\n"
"{}.\n",
?assertEqual(Expected, Formatted).

smoke_test_stdio_insert_and_require_pragma(Config) when is_list(Config) ->
DataDir = ?config(data_dir, Config),
Path = filename:join(DataDir, "pragma.erl"),
Expand Down

0 comments on commit c815036

Please sign in to comment.