fix: Group fluent subscript (#7386)

This commit is contained in:
Micha Reiser 2023-09-14 13:04:14 +02:00 committed by GitHub
parent 1f8e2b8f14
commit 675c86c175
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 17 deletions

View file

@ -667,7 +667,10 @@ impl Format<PyFormatContext<'_>> for FlatBinaryExpressionSlice<'_> {
leading_comments(leading).fmt(f)?;
}
in_parentheses_only_group(&left).fmt(f)?;
match &left.0 {
[OperandOrOperator::Operand(operand)] => operand.fmt(f)?,
_ => in_parentheses_only_group(&left).fmt(f)?,
}
if let Some(trailing) = left.last_operand().trailing_binary_comments() {
trailing_comments(trailing).fmt(f)?;
@ -708,7 +711,10 @@ impl Format<PyFormatContext<'_>> for FlatBinaryExpressionSlice<'_> {
leading_comments(leading).fmt(f)?;
}
in_parentheses_only_group(&right).fmt(f)
match &right.0 {
[OperandOrOperator::Operand(operand)] => operand.fmt(f),
_ => in_parentheses_only_group(&right).fmt(f),
}
}
}

View file

@ -42,24 +42,34 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
"A subscript expression can only have a single dangling comment, the one after the bracket"
);
match value.as_ref() {
Expr::Attribute(expr) => expr.format().with_options(call_chain_layout).fmt(f)?,
Expr::Call(expr) => expr.format().with_options(call_chain_layout).fmt(f)?,
Expr::Subscript(expr) => expr.format().with_options(call_chain_layout).fmt(f)?,
_ => value.format().fmt(f)?,
}
let format_slice = format_with(|f: &mut PyFormatter| {
if let Expr::Tuple(tuple) = slice.as_ref() {
write!(f, [tuple.format().with_options(TupleParentheses::Preserve)])
} else {
write!(f, [slice.format()])
let format_inner = format_with(|f| {
match value.as_ref() {
Expr::Attribute(expr) => expr.format().with_options(call_chain_layout).fmt(f)?,
Expr::Call(expr) => expr.format().with_options(call_chain_layout).fmt(f)?,
Expr::Subscript(expr) => expr.format().with_options(call_chain_layout).fmt(f)?,
_ => value.format().fmt(f)?,
}
let format_slice = format_with(|f: &mut PyFormatter| {
if let Expr::Tuple(tuple) = slice.as_ref() {
write!(f, [tuple.format().with_options(TupleParentheses::Preserve)])
} else {
write!(f, [slice.format()])
}
});
parenthesized("[", &format_slice, "]")
.with_dangling_comments(dangling_comments)
.fmt(f)
});
parenthesized("[", &format_slice, "]")
.with_dangling_comments(dangling_comments)
.fmt(f)
let is_call_chain_root = self.call_chain_layout == CallChainLayout::Default
&& call_chain_layout == CallChainLayout::Fluent;
if is_call_chain_root {
write!(f, [group(&format_inner)])
} else {
write!(f, [format_inner])
}
}
fn fmt_dangling_comments(