diff --git a/crates/ruff_python_formatter/src/comments/format.rs b/crates/ruff_python_formatter/src/comments/format.rs index 11de444cb5..850c32882f 100644 --- a/crates/ruff_python_formatter/src/comments/format.rs +++ b/crates/ruff_python_formatter/src/comments/format.rs @@ -33,7 +33,7 @@ impl Format> for FormatLeadingComments<'_> { let comments = f.context().comments().clone(); let leading_comments = match self { - FormatLeadingComments::Node(node) => comments.leading_comments(*node), + FormatLeadingComments::Node(node) => comments.leading(*node), FormatLeadingComments::Comments(comments) => comments, }; @@ -124,7 +124,7 @@ impl Format> for FormatTrailingComments<'_> { let comments = f.context().comments().clone(); let trailing_comments = match self { - FormatTrailingComments::Node(node) => comments.trailing_comments(*node), + FormatTrailingComments::Node(node) => comments.trailing(*node), FormatTrailingComments::Comments(comments) => comments, }; @@ -198,7 +198,7 @@ impl Format> for FormatDanglingComments<'_> { let dangling_comments = match self { Self::Comments(comments) => comments, - Self::Node(node) => comments.dangling_comments(*node), + Self::Node(node) => comments.dangling(*node), }; let mut first = true; diff --git a/crates/ruff_python_formatter/src/comments/mod.rs b/crates/ruff_python_formatter/src/comments/mod.rs index 3f27a142e2..d46014ae29 100644 --- a/crates/ruff_python_formatter/src/comments/mod.rs +++ b/crates/ruff_python_formatter/src/comments/mod.rs @@ -332,16 +332,16 @@ impl<'a> Comments<'a> { /// Returns `true` if the given `node` has any [leading comments](self#leading-comments). #[inline] - pub(crate) fn has_leading_comments(&self, node: T) -> bool + pub(crate) fn has_leading(&self, node: T) -> bool where T: Into>, { - !self.leading_comments(node).is_empty() + !self.leading(node).is_empty() } /// Returns the `node`'s [leading comments](self#leading-comments). #[inline] - pub(crate) fn leading_comments(&self, node: T) -> &[SourceComment] + pub(crate) fn leading(&self, node: T) -> &[SourceComment] where T: Into>, { @@ -351,15 +351,15 @@ impl<'a> Comments<'a> { } /// Returns `true` if node has any [dangling comments](self#dangling-comments). - pub(crate) fn has_dangling_comments(&self, node: T) -> bool + pub(crate) fn has_dangling(&self, node: T) -> bool where T: Into>, { - !self.dangling_comments(node).is_empty() + !self.dangling(node).is_empty() } /// Returns the [dangling comments](self#dangling-comments) of `node` - pub(crate) fn dangling_comments(&self, node: T) -> &[SourceComment] + pub(crate) fn dangling(&self, node: T) -> &[SourceComment] where T: Into>, { @@ -370,7 +370,7 @@ impl<'a> Comments<'a> { /// Returns the `node`'s [trailing comments](self#trailing-comments). #[inline] - pub(crate) fn trailing_comments(&self, node: T) -> &[SourceComment] + pub(crate) fn trailing(&self, node: T) -> &[SourceComment] where T: Into>, { @@ -381,43 +381,35 @@ impl<'a> Comments<'a> { /// Returns `true` if the given `node` has any [trailing comments](self#trailing-comments). #[inline] - pub(crate) fn has_trailing_comments(&self, node: T) -> bool + pub(crate) fn has_trailing(&self, node: T) -> bool where T: Into>, { - !self.trailing_comments(node).is_empty() + !self.trailing(node).is_empty() } /// Returns `true` if the given `node` has any [trailing own line comments](self#trailing-comments). #[inline] - pub(crate) fn has_trailing_own_line_comments(&self, node: T) -> bool + pub(crate) fn has_trailing_own_line(&self, node: T) -> bool where T: Into>, { - self.trailing_comments(node) + self.trailing(node) .iter() .any(|comment| comment.line_position().is_own_line()) } /// Returns an iterator over the [leading](self#leading-comments) and [trailing comments](self#trailing-comments) of `node`. - pub(crate) fn leading_trailing_comments( - &self, - node: T, - ) -> impl Iterator + pub(crate) fn leading_trailing(&self, node: T) -> impl Iterator where T: Into>, { - let node = node.into(); - self.leading_comments(node) - .iter() - .chain(self.trailing_comments(node).iter()) + let comments = self.leading_dangling_trailing(node); + comments.leading.iter().chain(comments.trailing) } /// Returns an iterator over the [leading](self#leading-comments), [dangling](self#dangling-comments), and [trailing](self#trailing) comments of `node`. - pub(crate) fn leading_dangling_trailing_comments( - &self, - node: T, - ) -> LeadingDanglingTrailingComments + pub(crate) fn leading_dangling_trailing(&self, node: T) -> LeadingDanglingTrailingComments where T: Into>, { @@ -426,30 +418,12 @@ impl<'a> Comments<'a> { .leading_dangling_trailing(&NodeRefEqualityKey::from_ref(node.into())) } - /// Returns any comments on the open parenthesis of a `node`. - /// - /// For example, `# comment` in: - /// ```python - /// ( # comment - /// foo.bar - /// ) - /// ``` - #[inline] - pub(crate) fn open_parenthesis_comment(&self, node: T) -> Option<&SourceComment> - where - T: Into>, - { - self.leading_comments(node) - .first() - .filter(|comment| comment.line_position.is_end_of_line()) - } - #[inline(always)] #[cfg(not(debug_assertions))] - pub(crate) fn assert_formatted_all_comments(&self, _source_code: SourceCode) {} + pub(crate) fn assert_all_formatted(&self, _source_code: SourceCode) {} #[cfg(debug_assertions)] - pub(crate) fn assert_formatted_all_comments(&self, source_code: SourceCode) { + pub(crate) fn assert_all_formatted(&self, source_code: SourceCode) { use std::fmt::Write; let mut output = String::new(); @@ -481,7 +455,7 @@ impl<'a> Comments<'a> { /// normally if `node` is the first or last node of a suppression range. #[cfg(debug_assertions)] pub(crate) fn mark_verbatim_node_comments_formatted(&self, node: AnyNodeRef) { - for dangling in self.dangling_comments(node) { + for dangling in self.dangling(node) { dangling.mark_formatted(); } @@ -505,7 +479,7 @@ struct MarkVerbatimCommentsAsFormattedVisitor<'a>(&'a Comments<'a>); impl<'a> PreorderVisitor<'a> for MarkVerbatimCommentsAsFormattedVisitor<'a> { fn enter_node(&mut self, node: AnyNodeRef<'a>) -> TraversalSignal { - for comment in self.0.leading_dangling_trailing_comments(node) { + for comment in self.0.leading_dangling_trailing(node) { comment.mark_formatted(); } diff --git a/crates/ruff_python_formatter/src/expression/expr_attribute.rs b/crates/ruff_python_formatter/src/expression/expr_attribute.rs index a0a66494a1..a19c567fab 100644 --- a/crates/ruff_python_formatter/src/expression/expr_attribute.rs +++ b/crates/ruff_python_formatter/src/expression/expr_attribute.rs @@ -45,7 +45,7 @@ impl FormatNodeRule for FormatExprAttribute { ); let comments = f.context().comments().clone(); - let dangling_comments = comments.dangling_comments(item); + let dangling_comments = comments.dangling(item); let leading_attribute_comments_start = dangling_comments .partition_point(|comment| comment.line_position().is_end_of_line()); let (trailing_dot_comments, leading_attribute_comments) = @@ -88,7 +88,7 @@ impl FormatNodeRule for FormatExprAttribute { value.format().fmt(f)?; } - if comments.has_trailing_own_line_comments(value.as_ref()) { + if comments.has_trailing_own_line(value.as_ref()) { hard_line_break().fmt(f)?; } @@ -171,10 +171,10 @@ impl NeedsParentheses for ExprAttribute { OptionalParentheses::Multiline } else if context .comments() - .dangling_comments(self) + .dangling(self) .iter() .any(|comment| comment.line_position().is_own_line()) - || context.comments().has_trailing_own_line_comments(self) + || context.comments().has_trailing_own_line(self) { OptionalParentheses::Always } else { diff --git a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs index c8e7cd51da..fd8dd96a9e 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs @@ -29,10 +29,7 @@ impl FormatNodeRule for FormatExprBinOp { match Self::layout(item, f.context()) { BinOpLayout::LeftString(expression) => { - let right_has_leading_comment = f - .context() - .comments() - .has_leading_comments(item.right.as_ref()); + let right_has_leading_comment = comments.has_leading(item.right.as_ref()); let format_right_and_op = format_with(|f| { if right_has_leading_comment { @@ -98,7 +95,7 @@ impl FormatNodeRule for FormatExprBinOp { right, } = current; - let operator_comments = comments.dangling_comments(current); + let operator_comments = comments.dangling(current); let needs_space = !is_simple_power_expression(current); let before_operator_space = if needs_space { @@ -117,9 +114,7 @@ impl FormatNodeRule for FormatExprBinOp { )?; // Format the operator on its own line if the right side has any leading comments. - if comments.has_leading_comments(right.as_ref()) - || !operator_comments.is_empty() - { + if comments.has_leading(right.as_ref()) || !operator_comments.is_empty() { hard_line_break().fmt(f)?; } else if needs_space { space().fmt(f)?; @@ -171,8 +166,8 @@ impl FormatExprBinOp { if bin_op.op == Operator::Mod && context.node_level().is_parenthesized() - && !comments.has_dangling_comments(constant) - && !comments.has_dangling_comments(bin_op) + && !comments.has_dangling(constant) + && !comments.has_dangling(bin_op) { BinOpLayout::LeftString(constant) } else { diff --git a/crates/ruff_python_formatter/src/expression/expr_bool_op.rs b/crates/ruff_python_formatter/src/expression/expr_bool_op.rs index ce8c9a1d00..8b2f9feeda 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bool_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bool_op.rs @@ -50,7 +50,7 @@ impl FormatNodeRule for FormatExprBoolOp { FormatValue { value: first }.fmt(f)?; for value in values { - let leading_value_comments = comments.leading_comments(value); + let leading_value_comments = comments.leading(value); // Format the expressions leading comments **before** the operator if leading_value_comments.is_empty() { write!(f, [in_parentheses_only_soft_line_break_or_space()])?; diff --git a/crates/ruff_python_formatter/src/expression/expr_compare.rs b/crates/ruff_python_formatter/src/expression/expr_compare.rs index 9e7bf38978..a217d45db6 100644 --- a/crates/ruff_python_formatter/src/expression/expr_compare.rs +++ b/crates/ruff_python_formatter/src/expression/expr_compare.rs @@ -29,7 +29,7 @@ impl FormatNodeRule for FormatExprCompare { assert_eq!(comparators.len(), ops.len()); for (operator, comparator) in ops.iter().zip(comparators) { - let leading_comparator_comments = comments.leading_comments(comparator); + let leading_comparator_comments = comments.leading(comparator); if leading_comparator_comments.is_empty() { write!(f, [in_parentheses_only_soft_line_break_or_space()])?; } else { diff --git a/crates/ruff_python_formatter/src/expression/expr_dict.rs b/crates/ruff_python_formatter/src/expression/expr_dict.rs index 659354cddd..840f973297 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict.rs @@ -43,7 +43,7 @@ impl Format> for KeyValuePair<'_> { ) } else { let comments = f.context().comments().clone(); - let leading_value_comments = comments.leading_comments(self.value); + let leading_value_comments = comments.leading(self.value); write!( f, [ @@ -67,7 +67,7 @@ impl FormatNodeRule for FormatExprDict { debug_assert_eq!(keys.len(), values.len()); let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); if values.is_empty() { return empty_parenthesized("{", dangling, "}").fmt(f); 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 ad23d72fab..333a4082dc 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs @@ -29,7 +29,7 @@ impl FormatNodeRule for FormatExprDictComp { }); let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); write!( f, 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 5bbd674418..32fc627f42 100644 --- a/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs @@ -51,7 +51,7 @@ impl FormatNodeRule for FormatExprGeneratorExp { }); let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); if self.parentheses == GeneratorExpParentheses::StripIfOnlyFunctionArg && dangling.is_empty() diff --git a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs index 959413664a..00c78c9b9c 100644 --- a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs @@ -30,12 +30,12 @@ impl FormatNodeRule for FormatExprIfExp { [in_parentheses_only_group(&format_args![ body.format(), in_parentheses_only_soft_line_break_or_space(), - leading_comments(comments.leading_comments(test.as_ref())), + leading_comments(comments.leading(test.as_ref())), text("if"), space(), test.format(), in_parentheses_only_soft_line_break_or_space(), - leading_comments(comments.leading_comments(orelse.as_ref())), + leading_comments(comments.leading(orelse.as_ref())), text("else"), space(), orelse.format() diff --git a/crates/ruff_python_formatter/src/expression/expr_list.rs b/crates/ruff_python_formatter/src/expression/expr_list.rs index 1b5a93b7c4..47e0a83535 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list.rs @@ -21,7 +21,7 @@ impl FormatNodeRule for FormatExprList { } = item; let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); if elts.is_empty() { return empty_parenthesized("[", dangling, "]").fmt(f); 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 4e460137a0..703126466c 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs @@ -27,7 +27,7 @@ impl FormatNodeRule for FormatExprListComp { }); let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); write!( f, diff --git a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs index 703b7d5d70..0d2ac80fc7 100644 --- a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs +++ b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs @@ -22,7 +22,7 @@ impl FormatNodeRule for FormatExprNamedExpr { // This context, a dangling comment is an end-of-line comment on the same line as the `:=`. let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); write!( f, diff --git a/crates/ruff_python_formatter/src/expression/expr_set.rs b/crates/ruff_python_formatter/src/expression/expr_set.rs index cccb7ea0e6..5027b46380 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set.rs @@ -22,7 +22,7 @@ impl FormatNodeRule for FormatExprSet { }); let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); parenthesized("{", &joined, "}") .with_dangling_comments(dangling) 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 9479060847..eb31a346a7 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs @@ -27,7 +27,7 @@ impl FormatNodeRule for FormatExprSetComp { }); let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); write!( f, diff --git a/crates/ruff_python_formatter/src/expression/expr_slice.rs b/crates/ruff_python_formatter/src/expression/expr_slice.rs index 1a83c6ebe5..ea23e41cdc 100644 --- a/crates/ruff_python_formatter/src/expression/expr_slice.rs +++ b/crates/ruff_python_formatter/src/expression/expr_slice.rs @@ -39,7 +39,7 @@ impl FormatNodeRule for FormatExprSlice { // to handle newlines and spacing, or the node is None and we insert the corresponding // slice of dangling comments let comments = f.context().comments().clone(); - let slice_dangling_comments = comments.dangling_comments(item.as_any_node_ref()); + let slice_dangling_comments = comments.dangling(item.as_any_node_ref()); // Put the dangling comments (where the nodes are missing) into buckets let first_colon_partition_index = slice_dangling_comments.partition_point(|x| x.slice().start() < first_colon.start()); @@ -102,7 +102,7 @@ impl FormatNodeRule for FormatExprSlice { // Upper if let Some(upper) = upper { - let upper_leading_comments = comments.leading_comments(upper.as_ref()); + let upper_leading_comments = comments.leading(upper.as_ref()); leading_comments_spacing(f, upper_leading_comments)?; write!(f, [upper.format(), line_suffix_boundary()])?; } else { @@ -134,7 +134,7 @@ impl FormatNodeRule for FormatExprSlice { space().fmt(f)?; } if let Some(step) = step { - let step_leading_comments = comments.leading_comments(step.as_ref()); + let step_leading_comments = comments.leading(step.as_ref()); leading_comments_spacing(f, step_leading_comments)?; step.format().fmt(f)?; } else if !dangling_step_comments.is_empty() { diff --git a/crates/ruff_python_formatter/src/expression/expr_starred.rs b/crates/ruff_python_formatter/src/expression/expr_starred.rs index a06cc3752a..42ee9f7999 100644 --- a/crates/ruff_python_formatter/src/expression/expr_starred.rs +++ b/crates/ruff_python_formatter/src/expression/expr_starred.rs @@ -21,7 +21,7 @@ impl FormatNodeRule for FormatExprStarred { } = item; let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); write!(f, [text("*"), dangling_comments(dangling), value.format()]) } diff --git a/crates/ruff_python_formatter/src/expression/expr_subscript.rs b/crates/ruff_python_formatter/src/expression/expr_subscript.rs index fbee7bd7f5..90c1d5c32d 100644 --- a/crates/ruff_python_formatter/src/expression/expr_subscript.rs +++ b/crates/ruff_python_formatter/src/expression/expr_subscript.rs @@ -37,7 +37,7 @@ impl FormatNodeRule for FormatExprSubscript { let call_chain_layout = self.call_chain_layout.apply_in_node(item, f); let comments = f.context().comments().clone(); - let dangling_comments = comments.dangling_comments(item.as_any_node_ref()); + let dangling_comments = comments.dangling(item.as_any_node_ref()); debug_assert!( dangling_comments.len() <= 1, "A subscript expression can only have a single dangling comment, the one after the bracket" diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index bb9cd298ec..d101c5fdb4 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -107,7 +107,7 @@ impl FormatNodeRule for FormatExprTuple { } = item; let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); // Handle the edge cases of an empty tuple and a tuple with one element // diff --git a/crates/ruff_python_formatter/src/expression/expr_unary_op.rs b/crates/ruff_python_formatter/src/expression/expr_unary_op.rs index 48ce152555..7523edc777 100644 --- a/crates/ruff_python_formatter/src/expression/expr_unary_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_unary_op.rs @@ -39,7 +39,7 @@ impl FormatNodeRule for FormatExprUnaryOp { // (not # comment // a) // ``` - let leading_operand_comments = comments.leading_comments(operand.as_ref()); + let leading_operand_comments = comments.leading(operand.as_ref()); let trailing_operator_comments_end = leading_operand_comments.partition_point(|p| p.line_position().is_end_of_line()); let (trailing_operator_comments, leading_operand_comments) = diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 64b6ac8348..718c03d0c4 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -108,7 +108,20 @@ impl FormatRule> for FormatExpr { if parenthesize { let comments = f.context().comments().clone(); - let open_parenthesis_comment = comments.open_parenthesis_comment(expression); + + // Any comments on the open parenthesis of a `node`. + // + // For example, `# comment` in: + // ```python + // ( # comment + // foo.bar + // ) + // ``` + let open_parenthesis_comment = comments + .leading(expression) + .first() + .filter(|comment| comment.line_position().is_end_of_line()); + parenthesized("(", &format_expr, ")") .with_dangling_comments( open_parenthesis_comment @@ -167,8 +180,8 @@ impl Format> for MaybeParenthesizeExpression<'_> { let preserve_parentheses = parenthesize.is_optional() && is_expression_parenthesized((*expression).into(), f.context().source()); - let has_comments = comments.has_leading_comments(*expression) - || comments.has_trailing_own_line_comments(*expression); + let has_comments = + comments.has_leading(*expression) || comments.has_trailing_own_line(*expression); // If the expression has comments, we always want to preserve the parentheses. This also // ensures that we correctly handle parenthesized comments, and don't need to worry about @@ -655,11 +668,7 @@ pub(crate) fn has_own_parentheses( Expr::List(ast::ExprList { elts, .. }) | Expr::Set(ast::ExprSet { elts, .. }) | Expr::Tuple(ast::ExprTuple { elts, .. }) => { - if !elts.is_empty() - || context - .comments() - .has_dangling_comments(AnyNodeRef::from(expr)) - { + if !elts.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) { Some(OwnParentheses::NonEmpty) } else { Some(OwnParentheses::Empty) @@ -667,22 +676,14 @@ pub(crate) fn has_own_parentheses( } Expr::Dict(ast::ExprDict { keys, .. }) => { - if !keys.is_empty() - || context - .comments() - .has_dangling_comments(AnyNodeRef::from(expr)) - { + if !keys.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) { Some(OwnParentheses::NonEmpty) } else { Some(OwnParentheses::Empty) } } Expr::Call(ast::ExprCall { arguments, .. }) => { - if !arguments.is_empty() - || context - .comments() - .has_dangling_comments(AnyNodeRef::from(expr)) - { + if !arguments.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) { Some(OwnParentheses::NonEmpty) } else { Some(OwnParentheses::Empty) diff --git a/crates/ruff_python_formatter/src/expression/string.rs b/crates/ruff_python_formatter/src/expression/string.rs index a19e405c1d..01fa93b372 100644 --- a/crates/ruff_python_formatter/src/expression/string.rs +++ b/crates/ruff_python_formatter/src/expression/string.rs @@ -160,7 +160,7 @@ impl Format> for FormatStringContinuation<'_> { let comments = f.context().comments().clone(); let locator = f.context().locator(); let quote_style = f.options().quote_style(); - let mut dangling_comments = comments.dangling_comments(self.string); + let mut dangling_comments = comments.dangling(self.string); let string_range = self.string.range(); let string_content = locator.slice(string_range); diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index ebbe758e8f..34699c5800 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -49,7 +49,7 @@ where fn fmt(&self, node: &N, f: &mut PyFormatter) -> FormatResult<()> { let comments = f.context().comments().clone(); - let node_comments = comments.leading_dangling_trailing_comments(node.as_any_node_ref()); + let node_comments = comments.leading_dangling_trailing(node.as_any_node_ref()); if self.is_suppressed(node_comments.trailing, f.context()) { suppressed_node(node.as_any_node_ref()).fmt(f) @@ -161,7 +161,7 @@ pub fn format_node<'a>( formatted .context() .comments() - .assert_formatted_all_comments(SourceCode::new(source)); + .assert_all_formatted(SourceCode::new(source)); Ok(formatted) } diff --git a/crates/ruff_python_formatter/src/other/arguments.rs b/crates/ruff_python_formatter/src/other/arguments.rs index f4d8d1ea27..6c03eb46e8 100644 --- a/crates/ruff_python_formatter/src/other/arguments.rs +++ b/crates/ruff_python_formatter/src/other/arguments.rs @@ -24,7 +24,7 @@ impl FormatNodeRule for FormatArguments { // ``` if item.args.is_empty() && item.keywords.is_empty() { let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); return write!(f, [empty_parenthesized("(", dangling, ")")]); } @@ -77,7 +77,7 @@ impl FormatNodeRule for FormatArguments { // c, // ) let comments = f.context().comments().clone(); - let dangling_comments = comments.dangling_comments(item.as_any_node_ref()); + let dangling_comments = comments.dangling(item.as_any_node_ref()); write!( f, diff --git a/crates/ruff_python_formatter/src/other/comprehension.rs b/crates/ruff_python_formatter/src/other/comprehension.rs index c80679d2d0..a71cca2b2d 100644 --- a/crates/ruff_python_formatter/src/other/comprehension.rs +++ b/crates/ruff_python_formatter/src/other/comprehension.rs @@ -15,7 +15,7 @@ impl FormatNodeRule for FormatComprehension { impl Format> for Spacer<'_> { fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> { - if f.context().comments().has_leading_comments(self.0) { + if f.context().comments().has_leading(self.0) { soft_line_break_or_space().fmt(f) } else { space().fmt(f) @@ -36,13 +36,13 @@ impl FormatNodeRule for FormatComprehension { } let comments = f.context().comments().clone(); - let dangling_item_comments = comments.dangling_comments(item); + let dangling_item_comments = comments.dangling(item); let (before_target_comments, before_in_comments) = dangling_item_comments.split_at( dangling_item_comments .partition_point(|comment| comment.slice().end() < target.start()), ); - let trailing_in_comments = comments.dangling_comments(iter); + let trailing_in_comments = comments.dangling(iter); let in_spacer = format_with(|f| { if before_in_comments.is_empty() { @@ -73,7 +73,7 @@ impl FormatNodeRule for FormatComprehension { let joined = format_with(|f| { let mut joiner = f.join_with(soft_line_break_or_space()); for if_case in ifs { - let dangling_if_comments = comments.dangling_comments(if_case); + let dangling_if_comments = comments.dangling(if_case); let (own_line_if_comments, end_of_line_if_comments) = dangling_if_comments .split_at( diff --git a/crates/ruff_python_formatter/src/other/except_handler_except_handler.rs b/crates/ruff_python_formatter/src/other/except_handler_except_handler.rs index 15e1ef6205..c4c847da2f 100644 --- a/crates/ruff_python_formatter/src/other/except_handler_except_handler.rs +++ b/crates/ruff_python_formatter/src/other/except_handler_except_handler.rs @@ -46,7 +46,7 @@ impl FormatNodeRule for FormatExceptHandlerExceptHan } = item; let comments_info = f.context().comments().clone(); - let dangling_comments = comments_info.dangling_comments(item); + let dangling_comments = comments_info.dangling(item); write!( f, diff --git a/crates/ruff_python_formatter/src/other/match_case.rs b/crates/ruff_python_formatter/src/other/match_case.rs index d501ecd8cf..008c1f325e 100644 --- a/crates/ruff_python_formatter/src/other/match_case.rs +++ b/crates/ruff_python_formatter/src/other/match_case.rs @@ -22,7 +22,7 @@ impl FormatNodeRule for FormatMatchCase { } = item; let comments = f.context().comments().clone(); - let dangling_item_comments = comments.dangling_comments(item); + let dangling_item_comments = comments.dangling(item); write!( f, @@ -33,7 +33,7 @@ impl FormatNodeRule for FormatMatchCase { &format_with(|f| { write!(f, [text("case"), space()])?; - let leading_pattern_comments = comments.leading_comments(pattern); + let leading_pattern_comments = comments.leading(pattern); if !leading_pattern_comments.is_empty() { parenthesized( "(", diff --git a/crates/ruff_python_formatter/src/other/parameters.rs b/crates/ruff_python_formatter/src/other/parameters.rs index 09fd227ad2..45ee028b7e 100644 --- a/crates/ruff_python_formatter/src/other/parameters.rs +++ b/crates/ruff_python_formatter/src/other/parameters.rs @@ -63,7 +63,7 @@ impl FormatNodeRule for FormatParameters { let (slash, star) = find_parameter_separators(f.context().source(), item); let comments = f.context().comments().clone(); - let dangling = comments.dangling_comments(item); + let dangling = comments.dangling(item); // First dangling comment: trailing the opening parenthesis, e.g.: // ```python diff --git a/crates/ruff_python_formatter/src/other/with_item.rs b/crates/ruff_python_formatter/src/other/with_item.rs index a193d9e137..3d8e373636 100644 --- a/crates/ruff_python_formatter/src/other/with_item.rs +++ b/crates/ruff_python_formatter/src/other/with_item.rs @@ -19,7 +19,7 @@ impl FormatNodeRule for FormatWithItem { } = item; let comments = f.context().comments().clone(); - let trailing_as_comments = comments.dangling_comments(item); + let trailing_as_comments = comments.dangling(item); write!( f, diff --git a/crates/ruff_python_formatter/src/statement/stmt_assign.rs b/crates/ruff_python_formatter/src/statement/stmt_assign.rs index 49759a7ee4..d22e7adb0f 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_assign.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_assign.rs @@ -63,7 +63,7 @@ impl Format> for FormatTargets<'_> { if let Some((first, rest)) = self.targets.split_first() { let comments = f.context().comments(); - let parenthesize = if comments.has_leading_comments(first) { + let parenthesize = if comments.has_leading(first) { ParenthesizeTarget::Always } else if has_own_parentheses(first, f.context()).is_some() { ParenthesizeTarget::Never diff --git a/crates/ruff_python_formatter/src/statement/stmt_class_def.rs b/crates/ruff_python_formatter/src/statement/stmt_class_def.rs index 2957950506..78b3408d71 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_class_def.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_class_def.rs @@ -25,7 +25,7 @@ impl FormatNodeRule for FormatStmtClassDef { let comments = f.context().comments().clone(); - let dangling_comments = comments.dangling_comments(item); + let dangling_comments = comments.dangling(item); let trailing_definition_comments_start = dangling_comments.partition_point(|comment| comment.line_position().is_own_line()); @@ -93,11 +93,11 @@ impl FormatNodeRule for FormatStmtClassDef { // ``` if arguments.is_empty() && comments - .dangling_comments(arguments) + .dangling(arguments) .iter() .all(|comment| comment.line_position().is_end_of_line()) { - let dangling = comments.dangling_comments(arguments); + let dangling = comments.dangling(arguments); write!(f, [trailing_comments(dangling)])?; } else { write!(f, [arguments.format()])?; diff --git a/crates/ruff_python_formatter/src/statement/stmt_for.rs b/crates/ruff_python_formatter/src/statement/stmt_for.rs index 779f315025..e47065a3c3 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_for.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_for.rs @@ -39,7 +39,7 @@ impl FormatNodeRule for FormatStmtFor { } = item; let comments = f.context().comments().clone(); - let dangling_comments = comments.dangling_comments(item); + let dangling_comments = comments.dangling(item); let body_start = body.first().map_or(iter.end(), Stmt::start); let or_else_comments_start = dangling_comments.partition_point(|comment| comment.slice().end() < body_start); diff --git a/crates/ruff_python_formatter/src/statement/stmt_function_def.rs b/crates/ruff_python_formatter/src/statement/stmt_function_def.rs index 771ef48721..03e423b47c 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_function_def.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_function_def.rs @@ -29,7 +29,7 @@ impl FormatNodeRule for FormatStmtFunctionDef { let comments = f.context().comments().clone(); - let dangling_comments = comments.dangling_comments(item); + let dangling_comments = comments.dangling(item); let trailing_definition_comments_start = dangling_comments.partition_point(|comment| comment.line_position().is_own_line()); @@ -64,19 +64,17 @@ impl FormatNodeRule for FormatStmtFunctionDef { write!(f, [space(), text("->"), space()])?; if return_annotation.is_tuple_expr() { - let parentheses = if comments - .has_leading_comments(return_annotation.as_ref()) - { - Parentheses::Always - } else { - Parentheses::Never - }; + let parentheses = + if comments.has_leading(return_annotation.as_ref()) { + Parentheses::Always + } else { + Parentheses::Never + }; write!( f, [return_annotation.format().with_options(parentheses)] )?; - } else if comments.has_trailing_comments(return_annotation.as_ref()) - { + } else if comments.has_trailing(return_annotation.as_ref()) { // Intentionally parenthesize any return annotations with trailing comments. // This avoids an instability in cases like: // ```python diff --git a/crates/ruff_python_formatter/src/statement/stmt_global.rs b/crates/ruff_python_formatter/src/statement/stmt_global.rs index 682d9c66fb..620848c2c9 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_global.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_global.rs @@ -14,10 +14,7 @@ impl FormatNodeRule for FormatStmtGlobal { // Join the `global` names, breaking across continuation lines if necessary, unless the // `global` statement has a trailing comment, in which case, breaking the names would // move the comment "off" of the `global` statement. - if f.context() - .comments() - .has_trailing_comments(item.as_any_node_ref()) - { + if f.context().comments().has_trailing(item.as_any_node_ref()) { let joined = format_with(|f| { f.join_with(format_args![text(","), space()]) .entries(item.names.iter().formatted()) diff --git a/crates/ruff_python_formatter/src/statement/stmt_if.rs b/crates/ruff_python_formatter/src/statement/stmt_if.rs index 18470728dc..0ff8a816b6 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_if.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_if.rs @@ -22,7 +22,7 @@ impl FormatNodeRule for FormatStmtIf { } = item; let comments = f.context().comments().clone(); - let trailing_colon_comment = comments.dangling_comments(item); + let trailing_colon_comment = comments.dangling(item); write!( f, @@ -73,8 +73,8 @@ pub(crate) fn format_elif_else_clause( } = item; let comments = f.context().comments().clone(); - let trailing_colon_comment = comments.dangling_comments(item); - let leading_comments = comments.leading_comments(item); + let trailing_colon_comment = comments.dangling(item); + let leading_comments = comments.leading(item); write!( f, diff --git a/crates/ruff_python_formatter/src/statement/stmt_import_from.rs b/crates/ruff_python_formatter/src/statement/stmt_import_from.rs index a53a73611c..fb894f7e2b 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_import_from.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_import_from.rs @@ -59,7 +59,7 @@ impl FormatNodeRule for FormatStmtImportFrom { // ) // ``` let comments = f.context().comments().clone(); - let parenthesized_comments = comments.dangling_comments(item.as_any_node_ref()); + let parenthesized_comments = comments.dangling(item.as_any_node_ref()); if parenthesized_comments.is_empty() { parenthesize_if_expands(&names).fmt(f) diff --git a/crates/ruff_python_formatter/src/statement/stmt_match.rs b/crates/ruff_python_formatter/src/statement/stmt_match.rs index 9c9b790eae..9b1a3b1ad3 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_match.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_match.rs @@ -21,7 +21,7 @@ impl FormatNodeRule for FormatStmtMatch { } = item; let comments = f.context().comments().clone(); - let dangling_item_comments = comments.dangling_comments(item); + let dangling_item_comments = comments.dangling(item); // There can be at most one dangling comment after the colon in a match statement. debug_assert!(dangling_item_comments.len() <= 1); @@ -53,7 +53,7 @@ impl FormatNodeRule for FormatStmtMatch { f, [block_indent(&format_args!( leading_alternate_branch_comments( - comments.leading_comments(case), + comments.leading(case), last_case.body.last(), ), case.format() diff --git a/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs b/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs index f00d67fb28..c185b2ec69 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs @@ -14,10 +14,7 @@ impl FormatNodeRule for FormatStmtNonlocal { // Join the `nonlocal` names, breaking across continuation lines if necessary, unless the // `nonlocal` statement has a trailing comment, in which case, breaking the names would // move the comment "off" of the `nonlocal` statement. - if f.context() - .comments() - .has_trailing_comments(item.as_any_node_ref()) - { + if f.context().comments().has_trailing(item.as_any_node_ref()) { let joined = format_with(|f| { f.join_with(format_args![text(","), space()]) .entries(item.names.iter().formatted()) diff --git a/crates/ruff_python_formatter/src/statement/stmt_try.rs b/crates/ruff_python_formatter/src/statement/stmt_try.rs index 17263898a4..e619496824 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_try.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_try.rs @@ -63,13 +63,13 @@ impl FormatNodeRule for FormatStmtTry { } = item; let comments_info = f.context().comments().clone(); - let mut dangling_comments = comments_info.dangling_comments(item); + let mut dangling_comments = comments_info.dangling(item); (_, dangling_comments) = format_case(item, CaseKind::Try, None, dangling_comments, f)?; let mut previous_node = body.last(); for handler in handlers { - let handler_comments = comments_info.leading_comments(handler); + let handler_comments = comments_info.leading(handler); write!( f, [ diff --git a/crates/ruff_python_formatter/src/statement/stmt_while.rs b/crates/ruff_python_formatter/src/statement/stmt_while.rs index e01dd5ae6e..7507ac44e9 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_while.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_while.rs @@ -22,7 +22,7 @@ impl FormatNodeRule for FormatStmtWhile { } = item; let comments = f.context().comments().clone(); - let dangling_comments = comments.dangling_comments(item.as_any_node_ref()); + let dangling_comments = comments.dangling(item.as_any_node_ref()); let body_start = body.first().map_or(test.end(), Stmt::start); let or_else_comments_start = diff --git a/crates/ruff_python_formatter/src/statement/stmt_with.rs b/crates/ruff_python_formatter/src/statement/stmt_with.rs index c3f555abd2..f789078285 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_with.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_with.rs @@ -33,7 +33,7 @@ impl FormatNodeRule for FormatStmtWith { // ... // ``` let comments = f.context().comments().clone(); - let dangling_comments = comments.dangling_comments(item.as_any_node_ref()); + let dangling_comments = comments.dangling(item.as_any_node_ref()); let partition_point = dangling_comments.partition_point(|comment| { item.items .first() @@ -86,9 +86,7 @@ impl FormatNodeRule for FormatStmtWith { } else if let [item] = item.items.as_slice() { // This is similar to `maybe_parenthesize_expression`, but we're not dealing with an // expression here, it's a `WithItem`. - if comments.has_leading_comments(item) - || comments.has_trailing_own_line_comments(item) - { + if comments.has_leading(item) || comments.has_trailing_own_line(item) { optional_parentheses(&item.format()).fmt(f)?; } else { item.format().fmt(f)?; diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index 96e3b54d50..8e46540cdc 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -70,9 +70,7 @@ impl FormatRule> for FormatSuite { // Format the first statement in the body, which often has special formatting rules. let first = match self.kind { SuiteKind::Other => { - if is_class_or_function_definition(first) - && !comments.has_leading_comments(first) - { + if is_class_or_function_definition(first) && !comments.has_leading(first) { // Add an empty line for any nested functions or classes defined within // non-function or class compound statements, e.g., this is stable formatting: // ```python @@ -97,7 +95,7 @@ impl FormatRule> for FormatSuite { SuiteKind::Class => { if let Some(docstring) = DocstringStmt::try_from_statement(first) { - if !comments.has_leading_comments(first) + if !comments.has_leading(first) && lines_before(first.start(), source) > 1 { // Allow up to one empty line before a class docstring, e.g., this is @@ -118,7 +116,7 @@ impl FormatRule> for FormatSuite { SuiteKind::TopLevel => SuiteChildStatement::Other(first), }; - let first_comments = comments.leading_dangling_trailing_comments(first); + let first_comments = comments.leading_dangling_trailing(first); let (mut preceding, mut after_class_docstring) = if first_comments .leading @@ -158,8 +156,8 @@ impl FormatRule> for FormatSuite { match self.kind { SuiteKind::TopLevel if source_type.is_stub() => { // Preserve the empty line if the definitions are separated by a comment - if comments.has_trailing_comments(preceding) - || comments.has_leading_comments(following) + if comments.has_trailing(preceding) + || comments.has_leading(following) { empty_line().fmt(f)?; } else { @@ -220,8 +218,7 @@ impl FormatRule> for FormatSuite { // which is 0 instead of 1, the number of lines between the trailing comment and // the leading comment. This is why the suite handling counts the lines before the // start of the next statement or before the first leading comments for compound statements. - let start = if let Some(first_leading) = - comments.leading_comments(following).first() + let start = if let Some(first_leading) = comments.leading(following).first() { first_leading.slice().start() } else { @@ -294,7 +291,7 @@ impl FormatRule> for FormatSuite { } } - let following_comments = comments.leading_dangling_trailing_comments(following); + let following_comments = comments.leading_dangling_trailing(following); if following_comments .leading @@ -401,7 +398,7 @@ impl<'a> DocstringStmt<'a> { impl Format> for DocstringStmt<'_> { fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { let comments = f.context().comments().clone(); - let node_comments = comments.leading_dangling_trailing_comments(self.0); + let node_comments = comments.leading_dangling_trailing(self.0); if FormatStmtExpr.is_suppressed(node_comments.trailing, f.context()) { suppressed_node(self.0).fmt(f) diff --git a/crates/ruff_python_formatter/src/type_param/type_params.rs b/crates/ruff_python_formatter/src/type_param/type_params.rs index 4228c2da36..456d54d5e0 100644 --- a/crates/ruff_python_formatter/src/type_param/type_params.rs +++ b/crates/ruff_python_formatter/src/type_param/type_params.rs @@ -22,7 +22,7 @@ impl FormatNodeRule for FormatTypeParams { // c, // ] = ... let comments = f.context().comments().clone(); - let dangling_comments = comments.dangling_comments(item.as_any_node_ref()); + let dangling_comments = comments.dangling(item.as_any_node_ref()); write!(f, [trailing_comments(dangling_comments)])?; let items = format_with(|f| { diff --git a/crates/ruff_python_formatter/src/verbatim.rs b/crates/ruff_python_formatter/src/verbatim.rs index e6e4fa7fc5..eb3322d11f 100644 --- a/crates/ruff_python_formatter/src/verbatim.rs +++ b/crates/ruff_python_formatter/src/verbatim.rs @@ -36,7 +36,7 @@ pub(crate) fn write_suppressed_statements_starting_with_leading_comment<'a>( let source = f.context().source(); let mut leading_comment_ranges = - CommentRangeIter::outside_suppression(comments.leading_comments(first_suppressed), source); + CommentRangeIter::outside_suppression(comments.leading(first_suppressed), source); let before_format_off = leading_comment_ranges .next() @@ -88,7 +88,7 @@ pub(crate) fn write_suppressed_statements_starting_with_trailing_comment<'a>( let source = f.context().source(); let indentation = Indentation::from_stmt(last_formatted.statement(), source); - let trailing_node_comments = comments.trailing_comments(last_formatted); + let trailing_node_comments = comments.trailing(last_formatted); let mut trailing_comment_ranges = CommentRangeIter::outside_suppression(trailing_node_comments, source); @@ -192,7 +192,7 @@ pub(crate) fn write_suppressed_statements_starting_with_trailing_comment<'a>( write_suppressed_statements( format_off_comment, SuiteChildStatement::Other(first_suppressed), - comments.leading_comments(first_suppressed), + comments.leading(first_suppressed), statements, f, ) @@ -300,7 +300,7 @@ fn write_suppressed_statements<'a>( // Suppression ends here. Test if the node has a trailing suppression comment and, if so, // recurse and format the trailing comments and the following statements as suppressed. return if comments - .trailing_comments(statement) + .trailing(statement) .iter() .any(|comment| comment.is_suppression_off_comment(source)) { @@ -325,8 +325,7 @@ fn write_suppressed_statements<'a>( comments.mark_verbatim_node_comments_formatted(AnyNodeRef::from(statement)); - for range in CommentRangeIter::in_suppression(comments.trailing_comments(statement), source) - { + for range in CommentRangeIter::in_suppression(comments.trailing(statement), source) { match range { // All leading comments are suppressed // ```python @@ -393,10 +392,10 @@ fn write_suppressed_statements<'a>( if let Some(next_statement) = statements.next() { statement = SuiteChildStatement::Other(next_statement); - leading_node_comments = comments.leading_comments(next_statement); + leading_node_comments = comments.leading(next_statement); } else { let end = comments - .trailing_comments(statement) + .trailing(statement) .last() .map_or(statement.end(), Ranged::end); @@ -903,7 +902,7 @@ pub(crate) struct FormatSuppressedNode<'a> { impl Format> for FormatSuppressedNode<'_> { fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { let comments = f.context().comments().clone(); - let node_comments = comments.leading_dangling_trailing_comments(self.node); + let node_comments = comments.leading_dangling_trailing(self.node); // Mark all comments as formatted that fall into the node range for comment in node_comments.leading { @@ -948,7 +947,7 @@ pub(crate) fn write_suppressed_clause_header( let comments = f.context().comments(); header.visit(&mut |child| { - for comment in comments.leading_trailing_comments(child) { + for comment in comments.leading_trailing(child) { comment.mark_formatted(); } comments.mark_verbatim_node_comments_formatted(child);