You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When formatting code that involves multiline binary operator expressions and comments in-between, the Elixir formatter produces an unhelpful result. The following:
# Comment about expression 1expression1||# Comment about expression 2expression2||# Comment about expression 3expression3
formats as:
# Comment about expression 1# Comment about expression 2# Comment about expression 3expression1||expression2||expression3
Expected behavior
I would expect the formatter to allow placing comments between the lines, so that we can comment the specific expressions properly:
# Comment about expression 1expression1||# Comment about expression 2expression2||# Comment about expression 3expression3
The text was updated successfully, but these errors were encountered:
Correct, the formatter cannot preserve code comments in all situations. Some of them were already documented but this particular one was not. I have pushed updates to the documentation.
Unfortunately tackling this is non-trivial. If someone wants to give it a try, pull requests are welcome, but it is not something we intend to pursue ourselves. Thanks!
Okay, thanks for the explanation and update to the docs. 👍
If someone is curious how to work around this, you'd basically have to break up the expressions and use a result variable:
# Comment about expression 1result=expression1# Comment about expression 2result=result||expression2# Comment about expression 3result=result||expression3
You can also break the expressions into private functions, if they are complex enough to warrant for comments. With the advantage that the function names can also provide meaningful context.
Elixir and Erlang/OTP versions
Erlang/OTP 27 [erts-15.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]
Elixir 1.17.2 (compiled with Erlang/OTP 27)
Operating system
NixOS
Current behavior
When formatting code that involves multiline binary operator expressions and comments in-between, the Elixir formatter produces an unhelpful result. The following:
formats as:
Expected behavior
I would expect the formatter to allow placing comments between the lines, so that we can comment the specific expressions properly:
The text was updated successfully, but these errors were encountered: