diff --git a/crates/ruff_python_formatter/src/expression/expr_attribute.rs b/crates/ruff_python_formatter/src/expression/expr_attribute.rs index e70316a73b58d0..4adb2bdacea15a 100644 --- a/crates/ruff_python_formatter/src/expression/expr_attribute.rs +++ b/crates/ruff_python_formatter/src/expression/expr_attribute.rs @@ -4,9 +4,9 @@ use ruff_python_ast::{Constant, Expr, ExprAttribute, ExprConstant}; use crate::comments::{leading_comments, trailing_comments}; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses}; -use crate::expression::CallChainLayout; +use crate::expression::{format_call_chain_layout, CallChainLayout}; use crate::prelude::*; -use crate::FormatNodeRule; +use crate::{FormatNodeRule}; #[derive(Default)] pub struct FormatExprAttribute { @@ -31,16 +31,7 @@ impl FormatNodeRule for FormatExprAttribute { ctx: _, } = item; - let call_chain_layout = match self.call_chain_layout { - CallChainLayout::Default => { - if f.context().node_level().is_parenthesized() { - CallChainLayout::from_expression(AnyNodeRef::from(item), f.context().source()) - } else { - CallChainLayout::NonFluent - } - } - layout @ (CallChainLayout::Fluent | CallChainLayout::NonFluent) => layout, - }; + let call_chain_layout = format_call_chain_layout(f, self.call_chain_layout, item); let needs_parentheses = matches!( value.as_ref(), diff --git a/crates/ruff_python_formatter/src/expression/expr_call.rs b/crates/ruff_python_formatter/src/expression/expr_call.rs index 822d0cdc1be060..f00b94d52d06ab 100644 --- a/crates/ruff_python_formatter/src/expression/expr_call.rs +++ b/crates/ruff_python_formatter/src/expression/expr_call.rs @@ -1,7 +1,6 @@ -use crate::expression::CallChainLayout; -use ruff_formatter::{write, FormatRuleWithOptions}; +use crate::expression::{format_call_chain_layout, CallChainLayout}; +use ruff_formatter::FormatRuleWithOptions; use ruff_python_ast::node::AnyNodeRef; -use ruff_python_ast::Expr::Call; use ruff_python_ast::{Expr, ExprCall}; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; @@ -30,16 +29,7 @@ impl FormatNodeRule for FormatExprCall { arguments, } = item; - let call_chain_layout = match self.call_chain_layout { - CallChainLayout::Default => { - if f.context().node_level().is_parenthesized() { - CallChainLayout::from_expression(AnyNodeRef::from(item), f.context().source()) - } else { - CallChainLayout::NonFluent - } - } - layout @ (CallChainLayout::Fluent | CallChainLayout::NonFluent) => layout, - }; + let call_chain_layout = format_call_chain_layout(f, self.call_chain_layout, item); let fmt_inner = format_with(|f| { match func.as_ref() { diff --git a/crates/ruff_python_formatter/src/expression/expr_subscript.rs b/crates/ruff_python_formatter/src/expression/expr_subscript.rs index 38b52b839995a8..901112102b8216 100644 --- a/crates/ruff_python_formatter/src/expression/expr_subscript.rs +++ b/crates/ruff_python_formatter/src/expression/expr_subscript.rs @@ -7,7 +7,7 @@ use crate::context::PyFormatContext; use crate::context::{NodeLevel, WithNodeLevel}; use crate::expression::expr_tuple::TupleParentheses; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; -use crate::expression::CallChainLayout; +use crate::expression::{format_call_chain_layout, CallChainLayout}; use crate::prelude::*; use crate::FormatNodeRule; @@ -34,16 +34,7 @@ impl FormatNodeRule for FormatExprSubscript { ctx: _, } = item; - let call_chain_layout = match self.call_chain_layout { - CallChainLayout::Default => { - if f.context().node_level().is_parenthesized() { - CallChainLayout::from_expression(AnyNodeRef::from(item), f.context().source()) - } else { - CallChainLayout::NonFluent - } - } - layout @ (CallChainLayout::Fluent | CallChainLayout::NonFluent) => layout, - }; + let call_chain_layout = format_call_chain_layout(f, self.call_chain_layout, item); let comments = f.context().comments().clone(); let dangling_comments = comments.dangling_comments(item.as_any_node_ref()); diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 63803a74823323..f9468b22f516f5 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -578,3 +578,21 @@ impl From for OperatorPriority { } } } + +/// Determine whether to actually apply fluent layout in attribute, call and subscript formatting +pub(crate) fn format_call_chain_layout<'a>( + f: &mut PyFormatter, + layout: CallChainLayout, + item: impl Into>, +) -> CallChainLayout { + match layout { + CallChainLayout::Default => { + if f.context().node_level().is_parenthesized() { + CallChainLayout::from_expression(item.into(), f.context().source()) + } else { + CallChainLayout::NonFluent + } + } + layout @ (CallChainLayout::Fluent | CallChainLayout::NonFluent) => layout, + } +}