typstyle
typstyle copied to clipboard
Improve formatting of binary op chains
#105
https://doc.rust-lang.org/nightly/style-guide/expressions.html#binary-operations
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;
currently, typst doesn't allow spread binary op chain into multiple lines
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 :)
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.