mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
fix: Use BestFit layout for subscript (#7409)
This commit is contained in:
parent
2cbe1733c8
commit
916dd5b7fa
9 changed files with 116 additions and 23 deletions
|
@ -4,7 +4,7 @@ use ruff_python_ast::{Expr, ExprCall};
|
|||
|
||||
use crate::comments::{dangling_comments, SourceComment};
|
||||
use crate::expression::parentheses::{
|
||||
is_expression_parenthesized, NeedsParentheses, OptionalParentheses,
|
||||
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
|
||||
};
|
||||
use crate::expression::CallChainLayout;
|
||||
use crate::prelude::*;
|
||||
|
@ -36,13 +36,17 @@ impl FormatNodeRule<ExprCall> for FormatExprCall {
|
|||
|
||||
let call_chain_layout = self.call_chain_layout.apply_in_node(item, f);
|
||||
|
||||
let fmt_func = format_with(|f| {
|
||||
let fmt_func = format_with(|f: &mut PyFormatter| {
|
||||
// Format the function expression.
|
||||
match func.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),
|
||||
_ => func.format().fmt(f),
|
||||
if is_expression_parenthesized(func.into(), f.context().source()) {
|
||||
func.format().with_options(Parentheses::Always).fmt(f)
|
||||
} else {
|
||||
match func.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),
|
||||
_ => func.format().with_options(Parentheses::Never).fmt(f),
|
||||
}
|
||||
}?;
|
||||
|
||||
// Format comments between the function and its arguments.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue