typstyle icon indicating copy to clipboard operation
typstyle copied to clipboard

Improve formatting of binary op chains

Open Enter-tainer opened this issue 1 year ago • 4 comments
trafficstars

#105

https://doc.rust-lang.org/nightly/style-guide/expressions.html#binary-operations

Enter-tainer avatar Jul 13 '24 08:07 Enter-tainer

rustfmt's format result

        let a = 1111111111111
            + 2222222222222
            + 3333333333333 / 2
            + 4444444444444
            + 5555555555555
            + 444444444
            - 6666666
            + 7777777
            - 9999999 / 10000000
            + 111111111111 * 333 / 222 * 44444 / 999 * 1111 / 44444 * 1111 / 44444 * 1111 / 44444
                * 112312313123
                / 123123123123
                * 123123123123
                / 123123123123
                * 123123123123
            - 222222222222
            + 333333333333
            - 444444444444
            + 555555555555;

Enter-tainer avatar Jul 14 '24 07:07 Enter-tainer

currently, typst doesn't allow spread binary op chain into multiple lines

Enter-tainer avatar Jul 14 '24 17:07 Enter-tainer

Hello,

I noticed that typst handles line breaks in an expression in a way similar to python, by using parentheses.

Here's a counterpart of your example that compiles :

Code
#let a = (1111111111111
    + 2222222222222
    + 3333333333333 / 2
    + 4444444444444
    + 5555555555555
    + 444444444
    - 6666666
    + 7777777
    - 9999999 / 10000000
    + 111111111111 * 333 / 222 * 44444 / 999 * 1111 / 44444 * 1111 / 44444 * 1111 / 44444
        * 112312313123
        / 123123123123
        * 123123123123
        / 123123123123
        * 123123123123
    - 222222222222
    + 333333333333
    - 444444444444
    + 555555555555)

Also, I have a similar situation where chaining and or or keywords, the line break happens in a function call and not on the operators.

This code

#if a_very_long_variable_name_it_is_am_i_right and _and(false, true, true, true, true) [yes] else [no]

formats to

#if a_very_long_variable_name_it_is_am_i_right and _and(
  false,
  true,
  true,
  true,
  true,
) [yes] else [no]

I would argue that the following output is more readable:

#if (
    a_very_long_variable_name_it_is_am_i_right 
    and _and(false, true, true, true, true)
  ) [yes] else [no]

This requires breaking on the and operator, and hence the need for parentheses. I would love to hear about your feedbacks.

Thanks for the project and have a nice day :)

leana8959 avatar Sep 01 '24 11:09 leana8959

You are correct. I haven't really start working on this since typst has very special handling for newline in markup mode and scripting mode. And it is also a little bit annoying to add extra parens to make it work.

Enter-tainer avatar Sep 01 '24 15:09 Enter-tainer