From 7c332b259e95836c04ab7d6172ef301f0b723c25 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 3 Aug 2023 19:13:05 -0400 Subject: [PATCH] Tweak breaking groups for comprehensions --- .../fixtures/ruff/expression/dict_comp.py | 16 ++++++++ .../fixtures/ruff/expression/generator_exp.py | 16 ++++++++ .../fixtures/ruff/expression/list_comp.py | 18 ++++++++- .../src/expression/expr_dict_comp.rs | 2 +- .../src/expression/expr_generator_exp.rs | 8 ++-- .../src/expression/expr_list_comp.rs | 2 +- .../src/expression/expr_set_comp.rs | 2 +- .../format@expression__dict_comp.py.snap | 36 +++++++++++++++++ .../format@expression__generator_exp.py.snap | 36 +++++++++++++++++ .../format@expression__list_comp.py.snap | 39 ++++++++++++++++++- 10 files changed, 166 insertions(+), 9 deletions(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/dict_comp.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/dict_comp.py index 76e1e30f7bcff..2f6f9a3c48878 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/dict_comp.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/dict_comp.py @@ -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 +} diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/generator_exp.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/generator_exp.py index fd7072ca26dac..7baf9602e73aa 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/generator_exp.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/generator_exp.py @@ -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 +) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py index d0cf86ea2ffce..21f89cb019613 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/list_comp.py @@ -56,4 +56,20 @@ # Tuples with BinOp [i for i in (aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc)] -[(aaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccc) for i in b] \ No newline at end of file +[(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 +] diff --git a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs index 0c1f0d53a2de3..a89565018d5bd 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs @@ -44,7 +44,7 @@ impl FormatNodeRule for FormatExprDictComp { space(), value.format(), soft_line_break_or_space(), - group(&joined) + &joined )), "}" )] diff --git a/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs b/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs index 8dc2a48bc1b35..e9c967e95b794 100644 --- a/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs @@ -60,7 +60,7 @@ impl FormatNodeRule for FormatExprGeneratorExp { leading_comments(dangling), group(&elt.format()), soft_line_break_or_space(), - group(&joined), + &joined ] ) } else { @@ -69,11 +69,11 @@ impl FormatNodeRule for FormatExprGeneratorExp { [parenthesized_with_dangling_comments( "(", dangling, - &format_args!( + &group(&format_args!( group(&elt.format()), soft_line_break_or_space(), - group(&joined) - ), + &joined + )), ")" )] ) diff --git a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs index 22dc2633764bc..bc557bb581dfe 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs @@ -38,7 +38,7 @@ impl FormatNodeRule for FormatExprListComp { &group(&format_args![ group(&elt.format()), soft_line_break_or_space(), - group(&joined) + &joined ]), "]" )] diff --git a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs index 429177dd5d7c8..a5acbbd6f26bb 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs @@ -38,7 +38,7 @@ impl FormatNodeRule for FormatExprSetComp { &group(&format_args!( group(&elt.format()), soft_line_break_or_space(), - group(&joined) + &joined )), "}" )] diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__dict_comp.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__dict_comp.py.snap index 595ee6f0ac1ec..190d80a4513d4 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__dict_comp.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__dict_comp.py.snap @@ -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 @@ -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 +} ``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__generator_exp.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__generator_exp.py.snap index 54cff0fdb75f8..b634d6dd46dc1 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__generator_exp.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__generator_exp.py.snap @@ -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 @@ -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 +) ``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap index f480119b8e16c..57e59f3c0458c 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__list_comp.py.snap @@ -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 @@ -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 +] ```