Skip to content

Commit

Permalink
WIP: Use is_expression_parenthesized
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Aug 7, 2023
1 parent 1318c4c commit 059661e
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions crates/ruff_python_formatter/src/expression/expr_bool_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use crate::expression::parentheses::{
};
use crate::prelude::*;
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_ast::{BoolOp, Expr, ExprBoolOp};

use super::parentheses::is_expression_parenthesized;

#[derive(Default)]
pub struct FormatExprBoolOp {
parentheses: Option<Parentheses>,
Expand Down Expand Up @@ -36,6 +38,8 @@ impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
values,
} = item;

let source = f.context().source();

let inner = format_with(|f: &mut PyFormatter| {
let mut values = values.iter();
let comments = f.context().comments().clone();
Expand All @@ -44,18 +48,21 @@ impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
return Ok(());
};

if let Expr::BoolOp(value) = first {
// Mark chained boolean operations e.g. `x and y or z`
// and avoid creating a new group
write!(
f,
[value.format().with_options(BoolOpLayout {
parentheses: None,
chained: true,
})]
)?;
} else {
write!(f, [in_parentheses_only_group(&first.format())])?;
match first {
Expr::BoolOp(bool_op)
if !is_expression_parenthesized(bool_op.as_any_node_ref(), source) =>
{
// Mark chained boolean operations e.g. `x and y or z`
// and avoid creating a new group
write!(
f,
[bool_op.format().with_options(BoolOpLayout {
parentheses: None,
chained: true,
})]
)?;
}
_ => write!(f, [in_parentheses_only_group(&first.format())])?,
}

for value in values {
Expand All @@ -72,16 +79,21 @@ impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {

write!(f, [op.format(), space(),])?;

if let Expr::BoolOp(value) = value {
write!(
f,
[value.format().with_options(BoolOpLayout {
parentheses: None,
chained: true,
})]
)?;
} else {
write!(f, [value.format()])?;
match value {
Expr::BoolOp(bool_op)
if !is_expression_parenthesized(bool_op.as_any_node_ref(), source) =>
{
// Mark chained boolean operations e.g. `x and y or z`
// and avoid creating a new group
write!(
f,
[bool_op.format().with_options(BoolOpLayout {
parentheses: None,
chained: true,
})]
)?;
}
_ => write!(f, [in_parentheses_only_group(&value.format())])?,
}
}

Expand Down

0 comments on commit 059661e

Please sign in to comment.