1
1
use crate :: comments:: leading_comments;
2
2
use crate :: expression:: parentheses:: {
3
3
in_parentheses_only_group, in_parentheses_only_soft_line_break_or_space, NeedsParentheses ,
4
- OptionalParentheses , Parentheses ,
4
+ OptionalParentheses ,
5
5
} ;
6
6
use crate :: prelude:: * ;
7
7
use ruff_formatter:: { write, FormatOwnedWithRule , FormatRefWithRule , FormatRuleWithOptions } ;
@@ -12,20 +12,20 @@ use super::parentheses::is_expression_parenthesized;
12
12
13
13
#[ derive( Default ) ]
14
14
pub struct FormatExprBoolOp {
15
- parentheses : Option < Parentheses > ,
16
- chained : bool ,
15
+ layout : BoolOpLayout ,
17
16
}
18
17
19
- pub struct BoolOpLayout {
20
- pub ( crate ) parentheses : Option < Parentheses > ,
21
- pub ( crate ) chained : bool ,
18
+ #[ derive( Default , Copy , Clone ) ]
19
+ pub enum BoolOpLayout {
20
+ #[ default]
21
+ Default ,
22
+ Chained ,
22
23
}
23
24
24
25
impl FormatRuleWithOptions < ExprBoolOp , PyFormatContext < ' _ > > for FormatExprBoolOp {
25
26
type Options = BoolOpLayout ;
26
27
fn with_options ( mut self , options : Self :: Options ) -> Self {
27
- self . parentheses = options. parentheses ;
28
- self . chained = options. chained ;
28
+ self . layout = options;
29
29
self
30
30
}
31
31
}
@@ -68,7 +68,7 @@ impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
68
68
Ok ( ( ) )
69
69
} ) ;
70
70
71
- if self . chained {
71
+ if matches ! ( self . layout , BoolOpLayout :: Chained ) {
72
72
// Chained boolean operations should not be given a new group
73
73
inner. fmt ( f)
74
74
} else {
@@ -101,13 +101,7 @@ impl Format<PyFormatContext<'_>> for FormatValue<'_> {
101
101
) =>
102
102
{
103
103
// Mark chained boolean operations e.g. `x and y or z` and avoid creating a new group
104
- write ! (
105
- f,
106
- [ bool_op. format( ) . with_options( BoolOpLayout {
107
- parentheses: None ,
108
- chained: true ,
109
- } ) ]
110
- )
104
+ write ! ( f, [ bool_op. format( ) . with_options( BoolOpLayout :: Chained ) ] )
111
105
}
112
106
_ => write ! ( f, [ in_parentheses_only_group( & self . value. format( ) ) ] ) ,
113
107
}
0 commit comments