mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-11 14:18:04 +00:00
fix: Group fluent subscript (#7386)
This commit is contained in:
parent
1f8e2b8f14
commit
675c86c175
4 changed files with 69 additions and 17 deletions
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue