mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Format target: annotation = value?
expressions (#5661)
This commit is contained in:
parent
0c8ec80d7b
commit
f1d367655b
28 changed files with 318 additions and 988 deletions
|
@ -1,4 +1,4 @@
|
|||
use rustpython_parser::ast::ExprSubscript;
|
||||
use rustpython_parser::ast::{Expr, ExprSubscript};
|
||||
|
||||
use ruff_formatter::{format_args, write};
|
||||
use ruff_python_ast::node::AstNode;
|
||||
|
@ -6,8 +6,10 @@ use ruff_python_ast::node::AstNode;
|
|||
use crate::comments::trailing_comments;
|
||||
use crate::context::NodeLevel;
|
||||
use crate::context::PyFormatContext;
|
||||
use crate::expression::expr_tuple::TupleParentheses;
|
||||
use crate::expression::parentheses::{
|
||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
||||
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
|
||||
Parenthesize,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
|
@ -42,12 +44,31 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
|
|||
value.format().fmt(f)?;
|
||||
}
|
||||
|
||||
let format_slice = format_with(|f: &mut PyFormatter| {
|
||||
let saved_level = f.context().node_level();
|
||||
f.context_mut()
|
||||
.set_node_level(NodeLevel::ParenthesizedExpression);
|
||||
|
||||
let result = if let Expr::Tuple(tuple) = slice.as_ref() {
|
||||
tuple
|
||||
.format()
|
||||
.with_options(TupleParentheses::Subscript)
|
||||
.fmt(f)
|
||||
} else {
|
||||
slice.format().fmt(f)
|
||||
};
|
||||
|
||||
f.context_mut().set_node_level(saved_level);
|
||||
|
||||
result
|
||||
});
|
||||
|
||||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
[in_parentheses_only_group(&format_args![
|
||||
text("["),
|
||||
trailing_comments(dangling_comments),
|
||||
soft_block_indent(&slice.format()),
|
||||
soft_block_indent(&format_slice),
|
||||
text("]")
|
||||
])]
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue