Introduce parenthesized helper (#5565)

This commit is contained in:
Micha Reiser 2023-07-07 11:28:25 +02:00 committed by GitHub
parent bf4b96c5de
commit 40ddc1604c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 64 deletions

View file

@ -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),
}