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 {
// Don't propagate the call chain layout.
value.format().with_options(Parentheses::Always).fmt(f)?;
@ -127,7 +127,7 @@ impl FormatNodeRule<ExprAttribute> 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<ExprAttribute> 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(
if CallChainLayout::from_expression(
self.into(),
context.comments().ranges(),
context.source(),
),
CallChainLayout::Fluent(_)
) {
)
.is_fluent()
{
OptionalParentheses::Multiline
} else if context.comments().has_dangling(self) {
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)
// )
// ```
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(
if CallChainLayout::from_expression(
self.into(),
context.comments().ranges(),
context.source(),
),
CallChainLayout::Fluent(_)
) {
)
.is_fluent()
{
OptionalParentheses::Multiline
} else if context.comments().has_dangling(self) {
OptionalParentheses::Always

View File

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

View File

@ -74,8 +74,8 @@ impl FormatNodeRule<ExprSubscript> 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(
if CallChainLayout::from_expression(
self.into(),
context.comments().ranges(),
context.source(),
),
CallChainLayout::Fluent(_)
) {
)
.is_fluent()
{
OptionalParentheses::Multiline
} else if is_expression_parenthesized(
self.value.as_ref().into(),

View File

@ -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)]