Preserve parentheses around partial call chains (#7109)

This commit is contained in:
Charlie Marsh 2023-09-04 10:57:04 +01:00 committed by GitHub
parent 7be28a38c5
commit ece30e7c69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 207 additions and 38 deletions

View file

@ -630,20 +630,21 @@ impl CallChainLayout {
loop {
match expr {
ExpressionRef::Attribute(ast::ExprAttribute { value, .. }) => {
expr = ExpressionRef::from(value.as_ref());
// ```
// f().g
// ^^^ value
// data[:100].T
// ^^^^^^^^^^ value
// ```
if matches!(value.as_ref(), Expr::Call(_) | Expr::Subscript(_)) {
attributes_after_parentheses += 1;
} else if is_expression_parenthesized(expr, source) {
if is_expression_parenthesized(value.into(), source) {
// `(a).b`. We preserve these parentheses so don't recurse
attributes_after_parentheses += 1;
break;
} else if matches!(value.as_ref(), Expr::Call(_) | Expr::Subscript(_)) {
attributes_after_parentheses += 1;
}
expr = ExpressionRef::from(value.as_ref());
}
// ```
// f()
@ -666,9 +667,11 @@ impl CallChainLayout {
if is_expression_parenthesized(expr, source) {
attributes_after_parentheses += 1;
}
break;
}
}
// We preserve these parentheses so don't recurse
if is_expression_parenthesized(expr, source) {
break;