diff --git a/crates/ruff_python_formatter/src/expression/expr_dict.rs b/crates/ruff_python_formatter/src/expression/expr_dict.rs index f55c02dd203b6..2cd41626ed169 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict.rs @@ -6,9 +6,7 @@ use ruff_text_size::TextRange; use crate::builders::empty_parenthesized_with_dangling_comments; use crate::comments::leading_comments; -use crate::expression::parentheses::{ - parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses, -}; +use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::prelude::*; use crate::FormatNodeRule; @@ -86,7 +84,9 @@ impl FormatNodeRule for FormatExprDict { joiner.finish() }); - parenthesized_with_dangling_comments("{", dangling, &format_pairs, "}").fmt(f) + parenthesized("{", &format_pairs, "}") + .with_dangling_comments(dangling) + .fmt(f) } fn fmt_dangling_comments(&self, _node: &ExprDict, _f: &mut PyFormatter) -> FormatResult<()> { diff --git a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs index a89565018d5bd..e16d2e1900d01 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs @@ -6,9 +6,7 @@ use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::ExprDictComp; use crate::context::PyFormatContext; -use crate::expression::parentheses::{ - parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses, -}; +use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::AsFormat; use crate::{FormatNodeRule, FormattedIterExt, PyFormatter}; @@ -35,9 +33,8 @@ impl FormatNodeRule for FormatExprDictComp { write!( f, - [parenthesized_with_dangling_comments( + [parenthesized( "{", - dangling, &group(&format_args!( group(&key.format()), text(":"), @@ -47,7 +44,8 @@ impl FormatNodeRule for FormatExprDictComp { &joined )), "}" - )] + ) + .with_dangling_comments(dangling)] ) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs b/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs index e9c967e95b794..f99773fabce4d 100644 --- a/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs @@ -4,8 +4,7 @@ use ruff_python_ast::ExprGeneratorExp; use crate::comments::leading_comments; use crate::context::PyFormatContext; -use crate::expression::parentheses::parenthesized_with_dangling_comments; -use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; +use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::prelude::*; use crate::AsFormat; use crate::{FormatNodeRule, PyFormatter}; @@ -66,16 +65,16 @@ impl FormatNodeRule for FormatExprGeneratorExp { } else { write!( f, - [parenthesized_with_dangling_comments( + [parenthesized( "(", - dangling, &group(&format_args!( group(&elt.format()), soft_line_break_or_space(), &joined )), ")" - )] + ) + .with_dangling_comments(dangling)] ) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_list.rs b/crates/ruff_python_formatter/src/expression/expr_list.rs index 73482f3814ee4..89ef07bcd6ae4 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list.rs @@ -3,9 +3,7 @@ use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::{ExprList, Ranged}; use crate::builders::empty_parenthesized_with_dangling_comments; -use crate::expression::parentheses::{ - parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses, -}; +use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::prelude::*; use crate::FormatNodeRule; @@ -34,7 +32,9 @@ impl FormatNodeRule for FormatExprList { .finish() }); - parenthesized_with_dangling_comments("[", dangling, &items, "]").fmt(f) + parenthesized("[", &items, "]") + .with_dangling_comments(dangling) + .fmt(f) } fn fmt_dangling_comments(&self, _node: &ExprList, _f: &mut PyFormatter) -> FormatResult<()> { diff --git a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs index bc557bb581dfe..a5476e4b82769 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs @@ -3,9 +3,7 @@ use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::ExprListComp; use crate::context::PyFormatContext; -use crate::expression::parentheses::{ - parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses, -}; +use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::prelude::*; use crate::AsFormat; use crate::{FormatNodeRule, PyFormatter}; @@ -32,16 +30,16 @@ impl FormatNodeRule for FormatExprListComp { write!( f, - [parenthesized_with_dangling_comments( + [parenthesized( "[", - dangling, &group(&format_args![ group(&elt.format()), soft_line_break_or_space(), &joined ]), "]" - )] + ) + .with_dangling_comments(dangling)] ) } diff --git a/crates/ruff_python_formatter/src/expression/expr_set.rs b/crates/ruff_python_formatter/src/expression/expr_set.rs index 66795cb1a90f4..4259be8049a3b 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set.rs @@ -1,9 +1,7 @@ use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::{ExprSet, Ranged}; -use crate::expression::parentheses::{ - parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses, -}; +use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::prelude::*; use crate::FormatNodeRule; @@ -25,7 +23,9 @@ impl FormatNodeRule for FormatExprSet { let comments = f.context().comments().clone(); let dangling = comments.dangling_comments(item); - parenthesized_with_dangling_comments("{", dangling, &joined, "}").fmt(f) + parenthesized("{", &joined, "}") + .with_dangling_comments(dangling) + .fmt(f) } fn fmt_dangling_comments(&self, _node: &ExprSet, _f: &mut PyFormatter) -> FormatResult<()> { diff --git a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs index a5acbbd6f26bb..5e3247a249457 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs @@ -3,9 +3,7 @@ use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::ExprSetComp; use crate::context::PyFormatContext; -use crate::expression::parentheses::{ - parenthesized_with_dangling_comments, NeedsParentheses, OptionalParentheses, -}; +use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::prelude::*; use crate::AsFormat; use crate::{FormatNodeRule, PyFormatter}; @@ -32,16 +30,16 @@ impl FormatNodeRule for FormatExprSetComp { write!( f, - [parenthesized_with_dangling_comments( + [parenthesized( "{", - dangling, &group(&format_args!( group(&elt.format()), soft_line_break_or_space(), &joined )), "}" - )] + ) + .with_dangling_comments(dangling)] ) } diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index a2e2af857cd28..da21a5a21f727 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -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 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 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 { diff --git a/crates/ruff_python_formatter/src/expression/parentheses.rs b/crates/ruff_python_formatter/src/expression/parentheses.rs index cc61902ce937a..9e72231172dac 100644 --- a/crates/ruff_python_formatter/src/expression/parentheses.rs +++ b/crates/ruff_python_formatter/src/expression/parentheses.rs @@ -116,25 +116,6 @@ where } } -/// Formats `content` enclosed by the `left` and `right` parentheses, along with any dangling -/// comments on the opening parenthesis itself. -pub(crate) fn parenthesized_with_dangling_comments<'content, 'ast, Content>( - left: &'static str, - comments: &'content [SourceComment], - content: &'content Content, - right: &'static str, -) -> FormatParenthesized<'content, 'ast> -where - Content: Format>, -{ - FormatParenthesized { - left, - comments, - content: Argument::new(content), - right, - } -} - pub(crate) struct FormatParenthesized<'content, 'ast> { left: &'static str, comments: &'content [SourceComment], @@ -142,6 +123,24 @@ pub(crate) struct FormatParenthesized<'content, 'ast> { right: &'static str, } +impl<'content, 'ast> FormatParenthesized<'content, 'ast> { + /// Inserts any dangling comments that should be placed immediately after the open parenthesis. + /// For example: + /// ```python + /// [ # comment + /// 1, + /// 2, + /// 3, + /// ] + /// ``` + pub(crate) fn with_dangling_comments( + self, + comments: &'content [SourceComment], + ) -> FormatParenthesized<'content, 'ast> { + FormatParenthesized { comments, ..self } + } +} + impl<'ast> Format> for FormatParenthesized<'_, 'ast> { fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { let inner = format_with(|f| { diff --git a/crates/ruff_python_formatter/src/other/arguments.rs b/crates/ruff_python_formatter/src/other/arguments.rs index fb192046244a1..291dd9159e39a 100644 --- a/crates/ruff_python_formatter/src/other/arguments.rs +++ b/crates/ruff_python_formatter/src/other/arguments.rs @@ -6,7 +6,7 @@ use ruff_text_size::{TextRange, TextSize}; use crate::builders::empty_parenthesized_with_dangling_comments; use crate::expression::expr_generator_exp::GeneratorExpParentheses; -use crate::expression::parentheses::{parenthesized_with_dangling_comments, Parentheses}; +use crate::expression::parentheses::{parenthesized, Parentheses}; use crate::prelude::*; use crate::FormatNodeRule; @@ -100,12 +100,8 @@ impl FormatNodeRule for FormatArguments { // hey_this_is_a_very_long_call, it_has_funny_attributes_asdf_asdf, really=True // ) // ``` - parenthesized_with_dangling_comments( - "(", - dangling_comments, - &group(&all_arguments), - ")" - ) + parenthesized("(", &group(&all_arguments), ")") + .with_dangling_comments(dangling_comments) ] ) } diff --git a/crates/ruff_python_formatter/src/other/parameters.rs b/crates/ruff_python_formatter/src/other/parameters.rs index b9daee7721f73..a9e8aa16af656 100644 --- a/crates/ruff_python_formatter/src/other/parameters.rs +++ b/crates/ruff_python_formatter/src/other/parameters.rs @@ -11,7 +11,7 @@ use crate::comments::{ leading_comments, leading_node_comments, trailing_comments, CommentLinePosition, SourceComment, }; use crate::context::{NodeLevel, WithNodeLevel}; -use crate::expression::parentheses::parenthesized_with_dangling_comments; +use crate::expression::parentheses::parenthesized; use crate::prelude::*; use crate::FormatNodeRule; @@ -256,12 +256,8 @@ impl FormatNodeRule for FormatParameters { } else { write!( f, - [parenthesized_with_dangling_comments( - "(", - parenthesis_dangling, - &group(&format_inner), - ")" - )] + [parenthesized("(", &group(&format_inner), ")") + .with_dangling_comments(parenthesis_dangling)] ) } }