mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-17 17:10:53 +00:00
Introduce parenthesized
helper (#5565)
This commit is contained in:
parent
bf4b96c5de
commit
40ddc1604c
7 changed files with 60 additions and 64 deletions
|
@ -1,7 +1,8 @@
|
|||
use crate::builders::optional_parentheses;
|
||||
use crate::comments::{dangling_node_comments, Comments};
|
||||
use crate::expression::parentheses::{
|
||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
||||
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
|
||||
Parenthesize,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
|
||||
|
@ -69,15 +70,8 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
|
|||
)
|
||||
}
|
||||
[single] => {
|
||||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
// A single element tuple always needs parentheses and a trailing comma
|
||||
&text("("),
|
||||
soft_block_indent(&format_args![single.format(), &text(",")]),
|
||||
&text(")"),
|
||||
])]
|
||||
)
|
||||
// A single element tuple always needs parentheses and a trailing comma
|
||||
parenthesized("(", &format_args![single.format(), &text(",")], ")").fmt(f)
|
||||
}
|
||||
// If the tuple has parentheses, we generally want to keep them. The exception are for
|
||||
// loops, see `TupleParentheses::StripInsideForLoop` doc comment.
|
||||
|
@ -87,15 +81,7 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
|
|||
elts if is_parenthesized(*range, elts, f)
|
||||
&& self.parentheses != TupleParentheses::StripInsideForLoop =>
|
||||
{
|
||||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
// If there were previously parentheses, keep them
|
||||
&text("("),
|
||||
soft_block_indent(&ExprSequence::new(elts)),
|
||||
&text(")"),
|
||||
])]
|
||||
)
|
||||
parenthesized("(", &ExprSequence::new(elts), ")").fmt(f)
|
||||
}
|
||||
elts => optional_parentheses(&ExprSequence::new(elts)).fmt(f),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue