Skip to content

Commit

Permalink
Tweak breaking groups for comprehensions (#6321)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
charliermarsh authored Aug 4, 2023
1 parent 99baad1 commit 4d47dfd
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,19 @@

x, aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay) in z
}

a = {
k: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
}

a = {
k: [1, 2, 3,]
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
}

aaaaaaaaaaaaaaaaaaaaa = {
k: o for o in self.registry.values if o.__class__ is not ModelAdmin
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,19 @@
a for b in c
# trailing
)

a = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
)

a = (
[1, 2, 3,]
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
)

aaaaaaaaaaaaaaaaaaaaa = (
o for o in self.registry.values if o.__class__ is not ModelAdmin
)
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@

# Tuples with BinOp
[i for i in (aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc)]
[(aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc) for i in b]
[(aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc) for i in b]

a = [
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
]

a = [
[1, 2, 3,]
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
]

aaaaaaaaaaaaaaaaaaaaa = [
o for o in self.registry.values if o.__class__ is not ModelAdmin
]
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
space(),
value.format(),
soft_line_break_or_space(),
group(&joined)
&joined
)),
"}"
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
leading_comments(dangling),
group(&elt.format()),
soft_line_break_or_space(),
group(&joined),
&joined
]
)
} else {
Expand All @@ -69,11 +69,11 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
[parenthesized_with_dangling_comments(
"(",
dangling,
&format_args!(
&group(&format_args!(
group(&elt.format()),
soft_line_break_or_space(),
group(&joined)
),
&joined
)),
")"
)]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl FormatNodeRule<ExprListComp> for FormatExprListComp {
&group(&format_args![
group(&elt.format()),
soft_line_break_or_space(),
group(&joined)
&joined
]),
"]"
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {
&group(&format_args!(
group(&elt.format()),
soft_line_break_or_space(),
group(&joined)
&joined
)),
"}"
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ selected_choices = {
x, aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay) in z
}
a = {
k: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
}
a = {
k: [1, 2, 3,]
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
}
aaaaaaaaaaaaaaaaaaaaa = {
k: o for o in self.registry.values if o.__class__ is not ModelAdmin
}
```

## Output
Expand Down Expand Up @@ -251,6 +267,26 @@ selected_choices = {
k: v
for (x, aaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaayaaaay) in z # foo
}
a = {
k: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
}
a = {
k: [
1,
2,
3,
]
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
}
aaaaaaaaaaaaaaaaaaaaa = {
k: o for o in self.registry.values if o.__class__ is not ModelAdmin
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ len(
a for b in c
# trailing
)
a = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
)
a = (
[1, 2, 3,]
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
)
aaaaaaaaaaaaaaaaaaaaa = (
o for o in self.registry.values if o.__class__ is not ModelAdmin
)
```

## Output
Expand Down Expand Up @@ -75,6 +91,26 @@ len(
for b in c
# trailing
)
a = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
)
a = (
[
1,
2,
3,
]
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
)
aaaaaaaaaaaaaaaaaaaaa = (
o for o in self.registry.values if o.__class__ is not ModelAdmin
)
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,24 @@ selected_choices = [
# Tuples with BinOp
[i for i in (aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc)]
[(aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc) for i in b]```
[(aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc) for i in b]
a = [
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
]
a = [
[1, 2, 3,]
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
]
aaaaaaaaaaaaaaaaaaaaa = [
o for o in self.registry.values if o.__class__ is not ModelAdmin
]
```
## Output
```py
Expand Down Expand Up @@ -141,6 +158,26 @@ selected_choices = [
)
for i in b
]
a = [
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
]
a = [
[
1,
2,
3,
]
for f in bbbbbbbbbbbbbbb
if f not in ccccccccccc
]
aaaaaaaaaaaaaaaaaaaaa = [
o for o in self.registry.values if o.__class__ is not ModelAdmin
]
```
Expand Down

0 comments on commit 4d47dfd

Please sign in to comment.