is_fluent helper

This commit is contained in:
dylwil3 2025-12-15 09:02:48 -06:00
parent 003632bcff
commit e372e92299
5 changed files with 39 additions and 41 deletions

View File

@ -48,7 +48,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
) )
}; };
if matches!(call_chain_layout, CallChainLayout::Fluent(_)) { if call_chain_layout.is_fluent() {
if parenthesize_value { if parenthesize_value {
// Don't propagate the call chain layout. // Don't propagate the call chain layout.
value.format().with_options(Parentheses::Always).fmt(f)?; value.format().with_options(Parentheses::Always).fmt(f)?;
@ -127,7 +127,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
// .filter() // .filter()
// ) // )
// ``` // ```
else if matches!(call_chain_layout, CallChainLayout::Fluent(_)) { else if call_chain_layout.is_fluent() {
if parenthesize_value if parenthesize_value
|| value.is_call_expr() || value.is_call_expr()
|| value.is_subscript_expr() || value.is_subscript_expr()
@ -177,8 +177,8 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
) )
}); });
let is_call_chain_root = self.call_chain_layout == CallChainLayout::Default let is_call_chain_root =
&& matches!(call_chain_layout, CallChainLayout::Fluent(_)); self.call_chain_layout == CallChainLayout::Default && call_chain_layout.is_fluent();
if is_call_chain_root { if is_call_chain_root {
write!(f, [group(&format_inner)]) write!(f, [group(&format_inner)])
} else { } else {
@ -194,14 +194,13 @@ impl NeedsParentheses for ExprAttribute {
context: &PyFormatContext, context: &PyFormatContext,
) -> OptionalParentheses { ) -> OptionalParentheses {
// Checks if there are any own line comments in an attribute chain (a.b.c). // Checks if there are any own line comments in an attribute chain (a.b.c).
if matches!( if CallChainLayout::from_expression(
CallChainLayout::from_expression(
self.into(), self.into(),
context.comments().ranges(), context.comments().ranges(),
context.source(), context.source(),
), )
CallChainLayout::Fluent(_) .is_fluent()
) { {
OptionalParentheses::Multiline OptionalParentheses::Multiline
} else if context.comments().has_dangling(self) { } else if context.comments().has_dangling(self) {
OptionalParentheses::Always OptionalParentheses::Always

View File

@ -70,9 +70,7 @@ impl FormatNodeRule<ExprCall> for FormatExprCall {
// queryset.distinct().order_by(field.name).values_list(field_name_flat_long_long=True) // queryset.distinct().order_by(field.name).values_list(field_name_flat_long_long=True)
// ) // )
// ``` // ```
if matches!(call_chain_layout, CallChainLayout::Fluent(_)) if call_chain_layout.is_fluent() && self.call_chain_layout == CallChainLayout::Default {
&& self.call_chain_layout == CallChainLayout::Default
{
group(&fmt_func).fmt(f) group(&fmt_func).fmt(f)
} else { } else {
fmt_func.fmt(f) fmt_func.fmt(f)
@ -86,14 +84,13 @@ impl NeedsParentheses for ExprCall {
_parent: AnyNodeRef, _parent: AnyNodeRef,
context: &PyFormatContext, context: &PyFormatContext,
) -> OptionalParentheses { ) -> OptionalParentheses {
if matches!( if CallChainLayout::from_expression(
CallChainLayout::from_expression(
self.into(), self.into(),
context.comments().ranges(), context.comments().ranges(),
context.source(), context.source(),
), )
CallChainLayout::Fluent(_) .is_fluent()
) { {
OptionalParentheses::Multiline OptionalParentheses::Multiline
} else if context.comments().has_dangling(self) { } else if context.comments().has_dangling(self) {
OptionalParentheses::Always OptionalParentheses::Always

View File

@ -393,14 +393,13 @@ impl Format<PyFormatContext<'_>> for FormatBody<'_> {
// ``` // ```
else if matches!(body, Expr::Call(_) | Expr::Subscript(_)) { else if matches!(body, Expr::Call(_) | Expr::Subscript(_)) {
let unparenthesized = body.format().with_options(Parentheses::Never); let unparenthesized = body.format().with_options(Parentheses::Never);
if matches!( if CallChainLayout::from_expression(
CallChainLayout::from_expression(
body.into(), body.into(),
comments.ranges(), comments.ranges(),
f.context().source(), f.context().source(),
), )
CallChainLayout::Fluent(_) .is_fluent()
) { {
parenthesize_if_expands(&unparenthesized).fmt(f) parenthesize_if_expands(&unparenthesized).fmt(f)
} else { } else {
let unparenthesized = unparenthesized.memoized(); let unparenthesized = unparenthesized.memoized();

View File

@ -74,8 +74,8 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
.fmt(f) .fmt(f)
}); });
let is_call_chain_root = self.call_chain_layout == CallChainLayout::Default let is_call_chain_root =
&& matches!(call_chain_layout, CallChainLayout::Fluent(_)); self.call_chain_layout == CallChainLayout::Default && call_chain_layout.is_fluent();
if is_call_chain_root { if is_call_chain_root {
write!(f, [group(&format_inner)]) write!(f, [group(&format_inner)])
} else { } else {
@ -91,14 +91,13 @@ impl NeedsParentheses for ExprSubscript {
context: &PyFormatContext, context: &PyFormatContext,
) -> OptionalParentheses { ) -> OptionalParentheses {
{ {
if matches!( if CallChainLayout::from_expression(
CallChainLayout::from_expression(
self.into(), self.into(),
context.comments().ranges(), context.comments().ranges(),
context.source(), context.source(),
), )
CallChainLayout::Fluent(_) .is_fluent()
) { {
OptionalParentheses::Multiline OptionalParentheses::Multiline
} else if is_expression_parenthesized( } else if is_expression_parenthesized(
self.value.as_ref().into(), self.value.as_ref().into(),

View File

@ -1158,6 +1158,10 @@ impl CallChainLayout {
layout @ (CallChainLayout::Fluent(_) | CallChainLayout::NonFluent) => layout, layout @ (CallChainLayout::Fluent(_) | CallChainLayout::NonFluent) => layout,
} }
} }
pub(crate) fn is_fluent(&self) -> bool {
matches!(self, CallChainLayout::Fluent(_))
}
} }
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]