From e372e922998e86926dd6a150c51dfa3eb32044e6 Mon Sep 17 00:00:00 2001 From: dylwil3 Date: Mon, 15 Dec 2025 09:02:48 -0600 Subject: [PATCH] is_fluent helper --- .../src/expression/expr_attribute.rs | 23 +++++++++---------- .../src/expression/expr_call.rs | 19 +++++++-------- .../src/expression/expr_lambda.rs | 15 ++++++------ .../src/expression/expr_subscript.rs | 19 ++++++++------- .../src/expression/mod.rs | 4 ++++ 5 files changed, 39 insertions(+), 41 deletions(-) diff --git a/crates/ruff_python_formatter/src/expression/expr_attribute.rs b/crates/ruff_python_formatter/src/expression/expr_attribute.rs index 10c7630845..ca88313b19 100644 --- a/crates/ruff_python_formatter/src/expression/expr_attribute.rs +++ b/crates/ruff_python_formatter/src/expression/expr_attribute.rs @@ -48,7 +48,7 @@ impl FormatNodeRule for FormatExprAttribute { ) }; - if matches!(call_chain_layout, CallChainLayout::Fluent(_)) { + if call_chain_layout.is_fluent() { if parenthesize_value { // Don't propagate the call chain layout. value.format().with_options(Parentheses::Always).fmt(f)?; @@ -127,7 +127,7 @@ impl FormatNodeRule for FormatExprAttribute { // .filter() // ) // ``` - else if matches!(call_chain_layout, CallChainLayout::Fluent(_)) { + else if call_chain_layout.is_fluent() { if parenthesize_value || value.is_call_expr() || value.is_subscript_expr() @@ -177,8 +177,8 @@ impl FormatNodeRule for FormatExprAttribute { ) }); - let is_call_chain_root = self.call_chain_layout == CallChainLayout::Default - && matches!(call_chain_layout, CallChainLayout::Fluent(_)); + let is_call_chain_root = + self.call_chain_layout == CallChainLayout::Default && call_chain_layout.is_fluent(); if is_call_chain_root { write!(f, [group(&format_inner)]) } else { @@ -194,14 +194,13 @@ impl NeedsParentheses for ExprAttribute { context: &PyFormatContext, ) -> OptionalParentheses { // Checks if there are any own line comments in an attribute chain (a.b.c). - if matches!( - CallChainLayout::from_expression( - self.into(), - context.comments().ranges(), - context.source(), - ), - CallChainLayout::Fluent(_) - ) { + if CallChainLayout::from_expression( + self.into(), + context.comments().ranges(), + context.source(), + ) + .is_fluent() + { OptionalParentheses::Multiline } else if context.comments().has_dangling(self) { OptionalParentheses::Always diff --git a/crates/ruff_python_formatter/src/expression/expr_call.rs b/crates/ruff_python_formatter/src/expression/expr_call.rs index 414aa414b5..135ade2266 100644 --- a/crates/ruff_python_formatter/src/expression/expr_call.rs +++ b/crates/ruff_python_formatter/src/expression/expr_call.rs @@ -70,9 +70,7 @@ impl FormatNodeRule for FormatExprCall { // queryset.distinct().order_by(field.name).values_list(field_name_flat_long_long=True) // ) // ``` - if matches!(call_chain_layout, CallChainLayout::Fluent(_)) - && self.call_chain_layout == CallChainLayout::Default - { + if call_chain_layout.is_fluent() && self.call_chain_layout == CallChainLayout::Default { group(&fmt_func).fmt(f) } else { fmt_func.fmt(f) @@ -86,14 +84,13 @@ impl NeedsParentheses for ExprCall { _parent: AnyNodeRef, context: &PyFormatContext, ) -> OptionalParentheses { - if matches!( - CallChainLayout::from_expression( - self.into(), - context.comments().ranges(), - context.source(), - ), - CallChainLayout::Fluent(_) - ) { + if CallChainLayout::from_expression( + self.into(), + context.comments().ranges(), + context.source(), + ) + .is_fluent() + { OptionalParentheses::Multiline } else if context.comments().has_dangling(self) { OptionalParentheses::Always diff --git a/crates/ruff_python_formatter/src/expression/expr_lambda.rs b/crates/ruff_python_formatter/src/expression/expr_lambda.rs index 90836a7d21..63e8aa6d97 100644 --- a/crates/ruff_python_formatter/src/expression/expr_lambda.rs +++ b/crates/ruff_python_formatter/src/expression/expr_lambda.rs @@ -393,14 +393,13 @@ impl Format> for FormatBody<'_> { // ``` else if matches!(body, Expr::Call(_) | Expr::Subscript(_)) { let unparenthesized = body.format().with_options(Parentheses::Never); - if matches!( - CallChainLayout::from_expression( - body.into(), - comments.ranges(), - f.context().source(), - ), - CallChainLayout::Fluent(_) - ) { + if CallChainLayout::from_expression( + body.into(), + comments.ranges(), + f.context().source(), + ) + .is_fluent() + { parenthesize_if_expands(&unparenthesized).fmt(f) } else { let unparenthesized = unparenthesized.memoized(); diff --git a/crates/ruff_python_formatter/src/expression/expr_subscript.rs b/crates/ruff_python_formatter/src/expression/expr_subscript.rs index b4ac673412..c9595c4a26 100644 --- a/crates/ruff_python_formatter/src/expression/expr_subscript.rs +++ b/crates/ruff_python_formatter/src/expression/expr_subscript.rs @@ -74,8 +74,8 @@ impl FormatNodeRule for FormatExprSubscript { .fmt(f) }); - let is_call_chain_root = self.call_chain_layout == CallChainLayout::Default - && matches!(call_chain_layout, CallChainLayout::Fluent(_)); + let is_call_chain_root = + self.call_chain_layout == CallChainLayout::Default && call_chain_layout.is_fluent(); if is_call_chain_root { write!(f, [group(&format_inner)]) } else { @@ -91,14 +91,13 @@ impl NeedsParentheses for ExprSubscript { context: &PyFormatContext, ) -> OptionalParentheses { { - if matches!( - CallChainLayout::from_expression( - self.into(), - context.comments().ranges(), - context.source(), - ), - CallChainLayout::Fluent(_) - ) { + if CallChainLayout::from_expression( + self.into(), + context.comments().ranges(), + context.source(), + ) + .is_fluent() + { OptionalParentheses::Multiline } else if is_expression_parenthesized( self.value.as_ref().into(), diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 0a7ccfe13e..4d980c2aed 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -1158,6 +1158,10 @@ impl CallChainLayout { layout @ (CallChainLayout::Fluent(_) | CallChainLayout::NonFluent) => layout, } } + + pub(crate) fn is_fluent(&self) -> bool { + matches!(self, CallChainLayout::Fluent(_)) + } } #[derive(Debug, Copy, Clone, PartialEq, Eq)]