mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-30 23:27:38 +00:00
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:
parent
bb96647d66
commit
8919b6ad9a
11 changed files with 60 additions and 82 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue