-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formatter: Comprehension ifs should break if the comprehension breaks #6063
Comments
Happy to work on this. Where would the source for this be? I tried looking in comprehensions and a few other places, but couldn't find the grouping strat. Thx. |
You're in the right file. The grouping of the ruff/crates/ruff_python_formatter/src/other/comprehension.rs Lines 83 to 89 in cd41474
|
Additionally, you might need to look at |
Thanks @konstin will get to that too!
|
A group fits on a line if all its content (including nested groups) can be printed on a line without exceeding the configured line width. The way this is determined is by measuring if the groups content and any content coming after the group up to the first line break (soft or hard doesn't matter) doesn't exceed the line width. You may also want to take a look at the documentation of the different builder methdos (like
Yeah, that's what I had in mind but it may not work (always hard to tell with formatting) |
## Summary Fixes some comprehension formatting by avoiding creating the group for the comprehension itself (so that if it breaks, all parts break on their own lines, e.g. the `for` and the `if` clauses). Closes #6063. ## Test Plan Bunch of new fixtures.
Black breaks the
if
into its own line if the comprehension breakswhile we don't
Conversely, parentheses should not require the target breaking
black:
ours:
Fixing this requires changing the grouping strategy for comprehensions and generator expressions.
The text was updated successfully, but these errors were encountered: