Add a with_dangling_comments to the parenthesized formatter (#6402)

See: https://github.com/astral-sh/ruff/pull/6376#discussion_r1285514328.
This commit is contained in:
Charlie Marsh 2023-08-07 15:12:12 -04:00 committed by GitHub
parent bb96647d66
commit 8919b6ad9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 60 additions and 82 deletions

View file

@ -1,14 +1,11 @@
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::ExprTuple;
use ruff_python_ast::{Expr, Ranged};
use ruff_text_size::TextRange;
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;
use crate::builders::{empty_parenthesized_with_dangling_comments, parenthesize_if_expands};
use crate::expression::parentheses::{
parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses,
};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
#[derive(Eq, PartialEq, Debug, Default)]
@ -132,13 +129,9 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
_ =>
// A single element tuple always needs parentheses and a trailing comma, except when inside of a subscript
{
parenthesized_with_dangling_comments(
"(",
dangling,
&format_args![single.format(), text(",")],
")",
)
.fmt(f)
parenthesized("(", &format_args![single.format(), text(",")], ")")
.with_dangling_comments(dangling)
.fmt(f)
}
},
// If the tuple has parentheses, we generally want to keep them. The exception are for
@ -150,7 +143,8 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
&& !(self.parentheses == TupleParentheses::NeverPreserve
&& dangling.is_empty()) =>
{
parenthesized_with_dangling_comments("(", dangling, &ExprSequence::new(item), ")")
parenthesized("(", &ExprSequence::new(item), ")")
.with_dangling_comments(dangling)
.fmt(f)
}
_ => match self.parentheses {