Skip to content

Commit

Permalink
Fixes AdRoll#81: add parentheses to macros inside of binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
vkatsuba committed May 6, 2021
1 parent 6c400e4 commit 2e2c7d4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/formatters/default_formatter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
truncate_strings = false :: boolean(),
parenthesize_infix_operations = false :: boolean(),
empty_lines = [] :: [pos_integer()],
encoding = epp:default_encoding() :: epp:source_encoding()}).
encoding = epp:default_encoding() :: epp:source_encoding(),
in = undefined :: atom()}).

set_prec(Ctxt, Prec) ->
Ctxt#ctxt{prec = Prec}. % used internally
Expand Down Expand Up @@ -489,7 +490,7 @@ lay_no_comments(Node, Ctxt) ->
beside(D, lay_text_float("."));
binary ->
Ctxt1 = reset_prec(Ctxt),
Es = lay_items(erl_syntax:binary_fields(Node), Ctxt1, fun lay/2),
Es = lay_items(erl_syntax:binary_fields(Node), Ctxt1#ctxt{in = binary}, fun lay/2),
beside(lay_text_float("<<"), beside(Es, lay_text_float(">>")));
binary_field ->
Ctxt1 = set_prec(Ctxt, max_prec()),
Expand Down Expand Up @@ -601,7 +602,12 @@ lay_no_comments(Node, Ctxt) ->
Args ->
lay_application(N, Args, Ctxt1)
end,
D1 = beside(lay_text_float("?"), D),
D1 = case Ctxt1#ctxt.in of
binary ->
beside(lay_text_float("(?"), beside(D, lay_text_float(")")));
_ ->
beside(lay_text_float("?"), D)
end,
maybe_parentheses(D1, 0, Ctxt1);
parentheses ->
D = lay(erl_syntax:parentheses_body(Node), reset_prec(Ctxt)),
Expand Down
9 changes: 9 additions & 0 deletions test_app/after/src/macros_in_binary.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-module(macros_in_binary).

-export([chunkify/1]).

-define(CHUNK_SIZE, 65535 - 1).
-define(CONCAT(A, B), <<A/binary, B/binary>>).

chunkify(<<Binary:(?CHUNK_SIZE)/binary, Rest/binary>>) ->
{<<(?CONCAT(Binary, Rest))/binary>>, <<(?CONCAT(Binary, Rest))/binary>>}.
9 changes: 9 additions & 0 deletions test_app/src/macros_in_binary.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-module(macros_in_binary).

-export([chunkify/1]).

-define(CHUNK_SIZE, 65535 - 1).
-define(CONCAT(A, B), <<A/binary, B/binary>>).

chunkify(<<Binary:(?CHUNK_SIZE)/binary, Rest/binary>>) ->
{<<(?CONCAT(Binary, Rest))/binary>>, <<?CONCAT(Binary, Rest)/binary>>}.

0 comments on commit 2e2c7d4

Please sign in to comment.