Fix formatting of chained boolean operations (#6394)

Closes https://github.com/astral-sh/ruff/issues/6068

These commits are kind of a mess as I did some stumbling around here. 

Unrolls formatting of chained boolean operations to prevent nested
grouping which gives us Black-compatible formatting where each boolean
operation is on a new line.
This commit is contained in:
Zanie Blue 2023-08-07 12:22:33 -05:00 committed by GitHub
parent 63ffadf0b8
commit 999d88e773
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 177 additions and 16 deletions

View file

@ -16,6 +16,8 @@ use crate::expression::parentheses::{
};
use crate::prelude::*;
use self::expr_bool_op::BoolOpLayout;
pub(crate) mod expr_attribute;
pub(crate) mod expr_await;
pub(crate) mod expr_bin_op;
@ -67,7 +69,13 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
let parentheses = self.parentheses;
let format_expr = format_with(|f| match expression {
Expr::BoolOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
Expr::BoolOp(expr) => expr
.format()
.with_options(BoolOpLayout {
parentheses: Some(parentheses),
chained: false,
})
.fmt(f),
Expr::NamedExpr(expr) => expr.format().fmt(f),
Expr::BinOp(expr) => expr.format().fmt(f),
Expr::UnaryOp(expr) => expr.format().fmt(f),