Skip to content

Commit

Permalink
Don't drop bin op parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Aug 2, 2023
1 parent c8c8c50 commit ca3ca2a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,8 @@
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
)

# Don't drop the bin op parentheses
e1 = (1 + 2).w().t()
e2 = (1 + 2)().w().t()
e3 = (1 + 2)[1].w().t()
16 changes: 11 additions & 5 deletions crates/ruff_python_formatter/src/expression/expr_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
value.format().with_options(Parentheses::Always).fmt(f)?;
} else if self.parentheses == Some(Parentheses::FluentStyle) {
// Fluent style: We need to pass the parentheses on to inner attributes or call chains
value
.format()
.with_options(Parentheses::FluentStyle)
.fmt(f)?;
match value.as_ref() {
Expr::Attribute(_) => {
value
.format()
.with_options(Parentheses::FluentStyle)
.fmt(f)?;
}
Expr::Call(_) | Expr::Subscript(_) => {
value
.format()
.with_options(Parentheses::FluentStyle)
.fmt(f)?;
soft_line_break().fmt(f)?;
}
_ => {}
_ => value.format().fmt(f)?,
}
} else {
value.format().fmt(f)?;
Expand Down
18 changes: 2 additions & 16 deletions crates/ruff_python_formatter/src/expression/expr_bin_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use ruff_python_ast::{
};
use smallvec::SmallVec;

use ruff_formatter::{
format_args, write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions,
};
use ruff_formatter::{format_args, write, FormatOwnedWithRule, FormatRefWithRule};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_ast::str::is_implicit_concatenation;

Expand All @@ -19,23 +17,11 @@ use crate::expression::parentheses::{
NeedsParentheses, OptionalParentheses,
};
use crate::expression::string::StringLayout;
use crate::expression::Parentheses;
use crate::prelude::*;
use crate::FormatNodeRule;

#[derive(Default)]
pub struct FormatExprBinOp {
parentheses: Option<Parentheses>,
}

impl FormatRuleWithOptions<ExprBinOp, PyFormatContext<'_>> for FormatExprBinOp {
type Options = Option<Parentheses>;

fn with_options(mut self, options: Self::Options) -> Self {
self.parentheses = options;
self
}
}
pub struct FormatExprBinOp;

impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
fn fmt_fields(&self, item: &ExprBinOp, f: &mut PyFormatter) -> FormatResult<()> {
Expand Down
8 changes: 7 additions & 1 deletion crates/ruff_python_formatter/src/expression/expr_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ impl FormatNodeRule<ExprCall> for FormatExprCall {
keywords,
} = item;

if self.parentheses == Some(Parentheses::FluentStyle) {
if self.parentheses == Some(Parentheses::FluentStyle)
&& matches!(
func.as_ref(),
Expr::Attribute(_) | Expr::Call(_) | Expr::Subscript(_)
)
{
// Fluent style: We need to pass the parentheses on to inner attributes or call chains

func.format()
.with_options(Parentheses::FluentStyle)
.fmt(f)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
);

let format_value = format_with(|f| {
if self.parentheses == Some(Parentheses::FluentStyle) {
if self.parentheses == Some(Parentheses::FluentStyle)
&& matches!(
value.as_ref(),
Expr::Attribute(_) | Expr::Call(_) | Expr::Subscript(_)
)
{
// Fluent style: We need to pass the parentheses on to inner attributes or call chains
value.format().with_options(Parentheses::FluentStyle).fmt(f)
} else {
Expand Down
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::cmp::Ordering;

use ruff_python_ast as ast;
use ruff_python_ast::{Expr, Operator};

use ruff_formatter::{
write, FormatOwnedWithRule, FormatRefWithRule, FormatRule, FormatRuleWithOptions,
};
use ruff_python_ast as ast;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor};
use ruff_python_ast::{Expr, Operator};

use crate::builders::parenthesize_if_expands;
use crate::context::{NodeLevel, WithNodeLevel};
Expand Down Expand Up @@ -78,7 +77,7 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
let format_expr = format_with(|f| match expression {
Expr::BoolOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
Expr::NamedExpr(expr) => expr.format().fmt(f),
Expr::BinOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
Expr::BinOp(expr) => expr.format().fmt(f),
Expr::UnaryOp(expr) => expr.format().fmt(f),
Expr::Lambda(expr) => expr.format().fmt(f),
Expr::IfExp(expr) => expr.format().fmt(f),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ d3 = (
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
)
# Don't drop the bin op parentheses
e1 = (1 + 2).w().t()
e2 = (1 + 2)().w().t()
e3 = (1 + 2)[1].w().t()
```

## Output
Expand Down Expand Up @@ -222,6 +227,11 @@ d3 = (
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
.esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk()
)
# Don't drop the bin op parentheses
e1 = (1 + 2).w().t()
e2 = (1 + 2)().w().t()
e3 = (1 + 2)[1].w().t()
```


Expand Down

0 comments on commit ca3ca2a

Please sign in to comment.