diff --git a/crates/ruff_python_formatter/src/comments/map.rs b/crates/ruff_python_formatter/src/comments/map.rs index 4b303caa05..7ef01dc736 100644 --- a/crates/ruff_python_formatter/src/comments/map.rs +++ b/crates/ruff_python_formatter/src/comments/map.rs @@ -249,11 +249,24 @@ impl MultiMap { self.index.get(key).is_some() } - /// Returns an iterator over the *leading*, *dangling*, and *trailing* parts of `key`. - pub(super) fn parts(&self, key: &K) -> PartsIterator { + /// Returns the *leading*, *dangling*, and *trailing* parts of `key`. + pub(super) fn leading_dangling_trailing(&self, key: &K) -> LeadingDanglingTrailing { match self.index.get(key) { - None => PartsIterator::Slice([].iter()), - Some(entry) => PartsIterator::from_entry(entry, self), + None => LeadingDanglingTrailing { + leading: &[], + dangling: &[], + trailing: &[], + }, + Some(Entry::InOrder(entry)) => LeadingDanglingTrailing { + leading: &self.parts[entry.leading_range()], + dangling: &self.parts[entry.dangling_range()], + trailing: &self.parts[entry.trailing_range()], + }, + Some(Entry::OutOfOrder(entry)) => LeadingDanglingTrailing { + leading: &self.out_of_order_parts[entry.leading_index()], + dangling: &self.out_of_order_parts[entry.dangling_index()], + trailing: &self.out_of_order_parts[entry.trailing_index()], + }, } } @@ -262,7 +275,7 @@ impl MultiMap { pub(super) fn all_parts(&self) -> impl Iterator { self.index .values() - .flat_map(|entry| PartsIterator::from_entry(entry, self)) + .flat_map(|entry| LeadingDanglingTrailing::from_entry(entry, self)) } } @@ -281,41 +294,30 @@ where let mut builder = f.debug_map(); for (key, entry) in &self.index { - builder.entry(&key, &DebugEntry { entry, map: self }); + builder.entry(&key, &LeadingDanglingTrailing::from_entry(entry, self)); } builder.finish() } } -/// Iterates over all *leading*, *dangling*, and *trailing* parts of a key. -pub(super) enum PartsIterator<'a, V> { - /// The slice into the [CommentsMap::parts] [Vec] if this is an in-order entry or the *trailing* parts - /// of an out-of-order entry. - Slice(std::slice::Iter<'a, V>), - - /// Iterator over the *leading* parts of an out-of-order entry. Returns the *dangling* parts, and then the - /// *trailing* parts once the *leading* iterator is fully consumed. - Leading { - leading: std::slice::Iter<'a, V>, - dangling: &'a [V], - trailing: &'a [V], - }, - - /// Iterator over the *dangling* parts of an out-of-order entry. Returns the *trailing* parts - /// once the *leading* iterator is fully consumed. - Dangling { - dangling: std::slice::Iter<'a, V>, - trailing: &'a [V], - }, +#[derive(Clone)] +pub(crate) struct LeadingDanglingTrailing<'a, T> { + pub(crate) leading: &'a [T], + pub(crate) dangling: &'a [T], + pub(crate) trailing: &'a [T], } -impl<'a, V> PartsIterator<'a, V> { - fn from_entry(entry: &Entry, map: &'a MultiMap) -> Self { +impl<'a, T> LeadingDanglingTrailing<'a, T> { + fn from_entry(entry: &Entry, map: &'a MultiMap) -> Self { match entry { - Entry::InOrder(entry) => PartsIterator::Slice(map.parts[entry.range()].iter()), - Entry::OutOfOrder(entry) => PartsIterator::Leading { - leading: map.out_of_order_parts[entry.leading_index()].iter(), + Entry::InOrder(entry) => LeadingDanglingTrailing { + leading: &map.parts[entry.leading_range()], + dangling: &map.parts[entry.dangling_range()], + trailing: &map.parts[entry.trailing_range()], + }, + Entry::OutOfOrder(entry) => LeadingDanglingTrailing { + leading: &map.out_of_order_parts[entry.leading_index()], dangling: &map.out_of_order_parts[entry.dangling_index()], trailing: &map.out_of_order_parts[entry.trailing_index()], }, @@ -323,203 +325,35 @@ impl<'a, V> PartsIterator<'a, V> { } } -impl<'a, V> Iterator for PartsIterator<'a, V> { - type Item = &'a V; +impl<'a, T> IntoIterator for LeadingDanglingTrailing<'a, T> { + type Item = &'a T; + type IntoIter = std::iter::Chain< + std::iter::Chain, std::slice::Iter<'a, T>>, + std::slice::Iter<'a, T>, + >; - fn next(&mut self) -> Option { - match self { - PartsIterator::Slice(inner) => inner.next(), - - PartsIterator::Leading { - leading, - dangling, - trailing, - } => match leading.next() { - Some(next) => Some(next), - None if !dangling.is_empty() => { - let mut dangling_iterator = dangling.iter(); - let next = dangling_iterator.next().unwrap(); - *self = PartsIterator::Dangling { - dangling: dangling_iterator, - trailing, - }; - Some(next) - } - None => { - let mut trailing_iterator = trailing.iter(); - let next = trailing_iterator.next(); - *self = PartsIterator::Slice(trailing_iterator); - next - } - }, - - PartsIterator::Dangling { dangling, trailing } => dangling.next().or_else(|| { - let mut trailing_iterator = trailing.iter(); - let next = trailing_iterator.next(); - *self = PartsIterator::Slice(trailing_iterator); - next - }), - } - } - - fn fold(self, init: B, f: F) -> B - where - F: FnMut(B, Self::Item) -> B, - { - match self { - PartsIterator::Slice(slice) => slice.fold(init, f), - PartsIterator::Leading { - leading, - dangling, - trailing, - } => leading - .chain(dangling.iter()) - .chain(trailing.iter()) - .fold(init, f), - PartsIterator::Dangling { dangling, trailing } => { - dangling.chain(trailing.iter()).fold(init, f) - } - } - } - - fn all(&mut self, f: F) -> bool - where - F: FnMut(Self::Item) -> bool, - { - match self { - PartsIterator::Slice(slice) => slice.all(f), - PartsIterator::Leading { - leading, - dangling, - trailing, - } => leading.chain(dangling.iter()).chain(trailing.iter()).all(f), - PartsIterator::Dangling { dangling, trailing } => { - dangling.chain(trailing.iter()).all(f) - } - } - } - - fn any(&mut self, f: F) -> bool - where - F: FnMut(Self::Item) -> bool, - { - match self { - PartsIterator::Slice(slice) => slice.any(f), - PartsIterator::Leading { - leading, - dangling, - trailing, - } => leading.chain(dangling.iter()).chain(trailing.iter()).any(f), - PartsIterator::Dangling { dangling, trailing } => { - dangling.chain(trailing.iter()).any(f) - } - } - } - - fn find

(&mut self, predicate: P) -> Option - where - P: FnMut(&Self::Item) -> bool, - { - match self { - PartsIterator::Slice(slice) => slice.find(predicate), - PartsIterator::Leading { - leading, - dangling, - trailing, - } => leading - .chain(dangling.iter()) - .chain(trailing.iter()) - .find(predicate), - PartsIterator::Dangling { dangling, trailing } => { - dangling.chain(trailing.iter()).find(predicate) - } - } - } - - fn find_map(&mut self, f: F) -> Option - where - F: FnMut(Self::Item) -> Option, - { - match self { - PartsIterator::Slice(slice) => slice.find_map(f), - PartsIterator::Leading { - leading, - dangling, - trailing, - } => leading - .chain(dangling.iter()) - .chain(trailing.iter()) - .find_map(f), - PartsIterator::Dangling { dangling, trailing } => { - dangling.chain(trailing.iter()).find_map(f) - } - } - } - - fn position

(&mut self, predicate: P) -> Option - where - P: FnMut(Self::Item) -> bool, - { - match self { - PartsIterator::Slice(slice) => slice.position(predicate), - PartsIterator::Leading { - leading, - dangling, - trailing, - } => leading - .chain(dangling.iter()) - .chain(trailing.iter()) - .position(predicate), - PartsIterator::Dangling { dangling, trailing } => { - dangling.chain(trailing.iter()).position(predicate) - } - } - } - - fn size_hint(&self) -> (usize, Option) { - match self { - PartsIterator::Slice(slice) => slice.size_hint(), - PartsIterator::Leading { - leading, - dangling, - trailing, - } => { - let len = leading.len() + dangling.len() + trailing.len(); - - (len, Some(len)) - } - PartsIterator::Dangling { dangling, trailing } => { - let len = dangling.len() + trailing.len(); - (len, Some(len)) - } - } - } - - fn count(self) -> usize { - self.size_hint().0 - } - - fn last(self) -> Option { - match self { - PartsIterator::Slice(slice) => slice.last(), - PartsIterator::Leading { - leading, - dangling, - trailing, - } => trailing - .last() - .or_else(|| dangling.last()) - .or_else(|| leading.last()), - PartsIterator::Dangling { dangling, trailing } => { - trailing.last().or_else(|| dangling.last()) - } - } + fn into_iter(self) -> Self::IntoIter { + self.leading + .iter() + .chain(self.dangling) + .chain(self.trailing) } } -impl ExactSizeIterator for PartsIterator<'_, V> {} +impl<'a, T> Debug for LeadingDanglingTrailing<'a, T> +where + T: Debug, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let mut list = f.debug_list(); -impl FusedIterator for PartsIterator<'_, V> {} + list.entries(self.leading.iter().map(DebugValue::Leading)); + list.entries(self.dangling.iter().map(DebugValue::Dangling)); + list.entries(self.trailing.iter().map(DebugValue::Trailing)); + + list.finish() + } +} #[derive(Clone, Debug)] enum Entry { @@ -527,48 +361,6 @@ enum Entry { OutOfOrder(OutOfOrderEntry), } -struct DebugEntry<'a, K, V> { - entry: &'a Entry, - map: &'a MultiMap, -} - -impl Debug for DebugEntry<'_, K, V> -where - K: Debug, - V: Debug, -{ - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let leading = match self.entry { - Entry::OutOfOrder(entry) => { - self.map.out_of_order_parts[entry.leading_index()].as_slice() - } - Entry::InOrder(entry) => &self.map.parts[entry.leading_range()], - }; - - let dangling = match self.entry { - Entry::OutOfOrder(entry) => { - self.map.out_of_order_parts[entry.dangling_index()].as_slice() - } - Entry::InOrder(entry) => &self.map.parts[entry.dangling_range()], - }; - - let trailing = match self.entry { - Entry::OutOfOrder(entry) => { - self.map.out_of_order_parts[entry.trailing_index()].as_slice() - } - Entry::InOrder(entry) => &self.map.parts[entry.trailing_range()], - }; - - let mut list = f.debug_list(); - - list.entries(leading.iter().map(DebugValue::Leading)); - list.entries(dangling.iter().map(DebugValue::Dangling)); - list.entries(trailing.iter().map(DebugValue::Trailing)); - - list.finish() - } -} - enum DebugValue<'a, V> { Leading(&'a V), Dangling(&'a V), @@ -811,7 +603,10 @@ mod tests { assert!(map.has(&"a")); assert_eq!( - map.parts(&"a").copied().collect::>(), + map.leading_dangling_trailing(&"a") + .into_iter() + .copied() + .collect::>(), vec![1, 2, 3, 4] ); } @@ -832,7 +627,13 @@ mod tests { assert!(map.has(&"a")); - assert_eq!(map.parts(&"a").copied().collect::>(), vec![1, 2, 3]); + assert_eq!( + map.leading_dangling_trailing(&"a") + .into_iter() + .copied() + .collect::>(), + vec![1, 2, 3] + ); } #[test] @@ -850,7 +651,13 @@ mod tests { assert!(map.has(&"a")); - assert_eq!(map.parts(&"a").copied().collect::>(), vec![1, 2]); + assert_eq!( + map.leading_dangling_trailing(&"a") + .into_iter() + .copied() + .collect::>(), + vec![1, 2] + ); } #[test] @@ -866,7 +673,10 @@ mod tests { assert!(!map.has(&"a")); assert_eq!( - map.parts(&"a").copied().collect::>(), + map.leading_dangling_trailing(&"a") + .into_iter() + .copied() + .collect::>(), Vec::::new() ); } @@ -887,22 +697,46 @@ mod tests { assert_eq!(map.leading(&"a"), &[1]); assert_eq!(map.dangling(&"a"), &EMPTY); assert_eq!(map.trailing(&"a"), &EMPTY); - assert_eq!(map.parts(&"a").copied().collect::>(), vec![1]); + assert_eq!( + map.leading_dangling_trailing(&"a") + .into_iter() + .copied() + .collect::>(), + vec![1] + ); assert_eq!(map.leading(&"b"), &EMPTY); assert_eq!(map.dangling(&"b"), &[2]); assert_eq!(map.trailing(&"b"), &EMPTY); - assert_eq!(map.parts(&"b").copied().collect::>(), vec![2]); + assert_eq!( + map.leading_dangling_trailing(&"b") + .into_iter() + .copied() + .collect::>(), + vec![2] + ); assert_eq!(map.leading(&"c"), &EMPTY); assert_eq!(map.dangling(&"c"), &EMPTY); assert_eq!(map.trailing(&"c"), &[3]); - assert_eq!(map.parts(&"c").copied().collect::>(), vec![3]); + assert_eq!( + map.leading_dangling_trailing(&"c") + .into_iter() + .copied() + .collect::>(), + vec![3] + ); assert_eq!(map.leading(&"d"), &[4]); assert_eq!(map.dangling(&"d"), &[5]); assert_eq!(map.trailing(&"d"), &[6]); - assert_eq!(map.parts(&"d").copied().collect::>(), vec![4, 5, 6]); + assert_eq!( + map.leading_dangling_trailing(&"d") + .into_iter() + .copied() + .collect::>(), + vec![4, 5, 6] + ); } #[test] @@ -919,7 +753,10 @@ mod tests { assert_eq!(map.trailing(&"a"), [4]); assert_eq!( - map.parts(&"a").copied().collect::>(), + map.leading_dangling_trailing(&"a") + .into_iter() + .copied() + .collect::>(), vec![2, 1, 3, 4] ); @@ -940,7 +777,10 @@ mod tests { assert_eq!(map.trailing(&"a"), [1, 4]); assert_eq!( - map.parts(&"a").copied().collect::>(), + map.leading_dangling_trailing(&"a") + .into_iter() + .copied() + .collect::>(), vec![2, 3, 1, 4] ); @@ -959,7 +799,13 @@ mod tests { assert_eq!(map.dangling(&"a"), &[2]); assert_eq!(map.trailing(&"a"), &[1, 3]); - assert_eq!(map.parts(&"a").copied().collect::>(), vec![2, 1, 3]); + assert_eq!( + map.leading_dangling_trailing(&"a") + .into_iter() + .copied() + .collect::>(), + vec![2, 1, 3] + ); assert!(map.has(&"a")); } diff --git a/crates/ruff_python_formatter/src/comments/mod.rs b/crates/ruff_python_formatter/src/comments/mod.rs index 8b1a108f7a..e1b144b151 100644 --- a/crates/ruff_python_formatter/src/comments/mod.rs +++ b/crates/ruff_python_formatter/src/comments/mod.rs @@ -106,7 +106,7 @@ use ruff_python_index::CommentRanges; use ruff_python_trivia::PythonWhitespace; use crate::comments::debug::{DebugComment, DebugComments}; -use crate::comments::map::MultiMap; +use crate::comments::map::{LeadingDanglingTrailing, MultiMap}; use crate::comments::node_key::NodeRefEqualityKey; use crate::comments::visitor::CommentsVisitor; @@ -405,13 +405,13 @@ impl<'a> Comments<'a> { pub(crate) fn leading_dangling_trailing_comments( &self, node: T, - ) -> impl Iterator + ) -> LeadingDanglingTrailingComments where T: Into>, { self.data .comments - .parts(&NodeRefEqualityKey::from_ref(node.into())) + .leading_dangling_trailing(&NodeRefEqualityKey::from_ref(node.into())) } #[inline(always)] @@ -464,6 +464,8 @@ impl<'a> Comments<'a> { } } +pub(crate) type LeadingDanglingTrailingComments<'a> = LeadingDanglingTrailing<'a, SourceComment>; + #[derive(Debug, Default)] struct CommentsData<'a> { comments: CommentsMap<'a>, diff --git a/crates/ruff_python_formatter/src/expression/expr_attribute.rs b/crates/ruff_python_formatter/src/expression/expr_attribute.rs index aef904218d..a0a66494a1 100644 --- a/crates/ruff_python_formatter/src/expression/expr_attribute.rs +++ b/crates/ruff_python_formatter/src/expression/expr_attribute.rs @@ -2,7 +2,7 @@ use ruff_formatter::{write, FormatRuleWithOptions}; use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::{Constant, Expr, ExprAttribute, ExprConstant}; -use crate::comments::{leading_comments, trailing_comments}; +use crate::comments::{leading_comments, trailing_comments, SourceComment}; use crate::expression::parentheses::{ is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parentheses, }; @@ -150,7 +150,7 @@ impl FormatNodeRule for FormatExprAttribute { fn fmt_dangling_comments( &self, - _node: &ExprAttribute, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { // handle in `fmt_fields` 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 175db909ca..26071e293d 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs @@ -9,7 +9,7 @@ use smallvec::SmallVec; use ruff_formatter::{format_args, write, FormatOwnedWithRule, FormatRefWithRule}; use ruff_python_ast::node::{AnyNodeRef, AstNode}; -use crate::comments::{trailing_comments, trailing_node_comments}; +use crate::comments::{trailing_comments, trailing_node_comments, SourceComment}; use crate::expression::expr_constant::ExprConstantLayout; use crate::expression::parentheses::{ in_parentheses_only_group, in_parentheses_only_soft_line_break, @@ -147,7 +147,11 @@ impl FormatNodeRule for FormatExprBinOp { } } - fn fmt_dangling_comments(&self, _node: &ExprBinOp, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled inside of `fmt_fields` Ok(()) } 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 1717bdc262..9449c6662a 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bool_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bool_op.rs @@ -1,7 +1,7 @@ use crate::comments::leading_comments; use crate::expression::parentheses::{ in_parentheses_only_group, in_parentheses_only_soft_line_break_or_space, NeedsParentheses, - OptionalParentheses, Parentheses, + OptionalParentheses, }; use crate::prelude::*; use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions}; @@ -12,20 +12,20 @@ use super::parentheses::is_expression_parenthesized; #[derive(Default)] pub struct FormatExprBoolOp { - parentheses: Option, - chained: bool, + layout: BoolOpLayout, } -pub struct BoolOpLayout { - pub(crate) parentheses: Option, - pub(crate) chained: bool, +#[derive(Default, Copy, Clone)] +pub enum BoolOpLayout { + #[default] + Default, + Chained, } impl FormatRuleWithOptions> for FormatExprBoolOp { type Options = BoolOpLayout; fn with_options(mut self, options: Self::Options) -> Self { - self.parentheses = options.parentheses; - self.chained = options.chained; + self.layout = options; self } } @@ -68,7 +68,7 @@ impl FormatNodeRule for FormatExprBoolOp { Ok(()) }); - if self.chained { + if matches!(self.layout, BoolOpLayout::Chained) { // Chained boolean operations should not be given a new group inner.fmt(f) } else { @@ -101,13 +101,7 @@ impl Format> for FormatValue<'_> { ) => { // Mark chained boolean operations e.g. `x and y or z` and avoid creating a new group - write!( - f, - [bool_op.format().with_options(BoolOpLayout { - parentheses: None, - chained: true, - })] - ) + write!(f, [bool_op.format().with_options(BoolOpLayout::Chained)]) } _ => write!(f, [in_parentheses_only_group(&self.value.format())]), } diff --git a/crates/ruff_python_formatter/src/expression/expr_compare.rs b/crates/ruff_python_formatter/src/expression/expr_compare.rs index 07c85526cf..9e7bf38978 100644 --- a/crates/ruff_python_formatter/src/expression/expr_compare.rs +++ b/crates/ruff_python_formatter/src/expression/expr_compare.rs @@ -1,27 +1,16 @@ -use crate::comments::leading_comments; +use crate::comments::{leading_comments, SourceComment}; use crate::expression::parentheses::{ in_parentheses_only_group, in_parentheses_only_soft_line_break_or_space, NeedsParentheses, - OptionalParentheses, Parentheses, + OptionalParentheses, }; use crate::prelude::*; use crate::FormatNodeRule; -use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions}; +use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule}; use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::{CmpOp, ExprCompare}; #[derive(Default)] -pub struct FormatExprCompare { - parentheses: Option, -} - -impl FormatRuleWithOptions> for FormatExprCompare { - type Options = Option; - - fn with_options(mut self, options: Self::Options) -> Self { - self.parentheses = options; - self - } -} +pub struct FormatExprCompare; impl FormatNodeRule for FormatExprCompare { fn fmt_fields(&self, item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> { @@ -70,7 +59,11 @@ impl FormatNodeRule for FormatExprCompare { in_parentheses_only_group(&inner).fmt(f) } - fn fmt_dangling_comments(&self, _node: &ExprCompare, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Node can not have dangling comments Ok(()) } diff --git a/crates/ruff_python_formatter/src/expression/expr_constant.rs b/crates/ruff_python_formatter/src/expression/expr_constant.rs index 58e8b48e58..64cfb8cd4c 100644 --- a/crates/ruff_python_formatter/src/expression/expr_constant.rs +++ b/crates/ruff_python_formatter/src/expression/expr_constant.rs @@ -1,3 +1,4 @@ +use crate::comments::SourceComment; use ruff_formatter::FormatRuleWithOptions; use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::{Constant, ExprConstant, Ranged}; @@ -65,7 +66,7 @@ impl FormatNodeRule for FormatExprConstant { fn fmt_dangling_comments( &self, - _node: &ExprConstant, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { Ok(()) diff --git a/crates/ruff_python_formatter/src/expression/expr_dict.rs b/crates/ruff_python_formatter/src/expression/expr_dict.rs index 39fae66527..659354cddd 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict.rs @@ -4,7 +4,7 @@ use ruff_python_ast::Ranged; use ruff_python_ast::{Expr, ExprDict}; use ruff_text_size::TextRange; -use crate::comments::leading_comments; +use crate::comments::{leading_comments, SourceComment}; use crate::expression::parentheses::{ empty_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses, }; @@ -89,7 +89,11 @@ impl FormatNodeRule for FormatExprDict { .fmt(f) } - fn fmt_dangling_comments(&self, _node: &ExprDict, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled by `fmt_fields` Ok(()) } 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 f99773fabc..5181ce090f 100644 --- a/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs @@ -2,7 +2,7 @@ use ruff_formatter::{format_args, write, Buffer, FormatResult, FormatRuleWithOpt use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::ExprGeneratorExp; -use crate::comments::leading_comments; +use crate::comments::{leading_comments, SourceComment}; use crate::context::PyFormatContext; use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::prelude::*; @@ -81,7 +81,7 @@ impl FormatNodeRule for FormatExprGeneratorExp { fn fmt_dangling_comments( &self, - _node: &ExprGeneratorExp, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { // Handled as part of `fmt_fields` diff --git a/crates/ruff_python_formatter/src/expression/expr_lambda.rs b/crates/ruff_python_formatter/src/expression/expr_lambda.rs index c8a23a18f0..d663084f39 100644 --- a/crates/ruff_python_formatter/src/expression/expr_lambda.rs +++ b/crates/ruff_python_formatter/src/expression/expr_lambda.rs @@ -1,4 +1,4 @@ -use crate::comments::dangling_node_comments; +use crate::comments::{dangling_node_comments, SourceComment}; use crate::context::PyFormatContext; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; use crate::other::parameters::ParametersParentheses; @@ -55,7 +55,11 @@ impl FormatNodeRule for FormatExprLambda { ) } - fn fmt_dangling_comments(&self, _node: &ExprLambda, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Override. Dangling comments are handled in `fmt_fields`. Ok(()) } diff --git a/crates/ruff_python_formatter/src/expression/expr_list.rs b/crates/ruff_python_formatter/src/expression/expr_list.rs index 7c91b4ef36..1b5a93b7c4 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list.rs @@ -1,3 +1,4 @@ +use crate::comments::SourceComment; use ruff_formatter::prelude::format_with; use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::{ExprList, Ranged}; @@ -37,7 +38,11 @@ impl FormatNodeRule for FormatExprList { .fmt(f) } - fn fmt_dangling_comments(&self, _node: &ExprList, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled as part of `fmt_fields` Ok(()) } 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 a5476e4b82..54db366d12 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs @@ -2,6 +2,7 @@ use ruff_formatter::{format_args, write, FormatResult}; use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::ExprListComp; +use crate::comments::SourceComment; use crate::context::PyFormatContext; use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::prelude::*; @@ -45,7 +46,7 @@ impl FormatNodeRule for FormatExprListComp { fn fmt_dangling_comments( &self, - _node: &ExprListComp, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { // Handled as part of `fmt_fields` diff --git a/crates/ruff_python_formatter/src/expression/expr_name.rs b/crates/ruff_python_formatter/src/expression/expr_name.rs index 99d5374498..a2aef80593 100644 --- a/crates/ruff_python_formatter/src/expression/expr_name.rs +++ b/crates/ruff_python_formatter/src/expression/expr_name.rs @@ -1,3 +1,4 @@ +use crate::comments::SourceComment; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; use crate::prelude::*; use crate::FormatNodeRule; @@ -23,7 +24,11 @@ impl FormatNodeRule for FormatExprName { write!(f, [source_text_slice(*range, ContainsNewlines::No)]) } - fn fmt_dangling_comments(&self, _node: &ExprName, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Node cannot have dangling comments Ok(()) } diff --git a/crates/ruff_python_formatter/src/expression/expr_set.rs b/crates/ruff_python_formatter/src/expression/expr_set.rs index 4259be8049..cccb7ea0e6 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set.rs @@ -1,3 +1,4 @@ +use crate::comments::SourceComment; use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::{ExprSet, Ranged}; @@ -28,7 +29,11 @@ impl FormatNodeRule for FormatExprSet { .fmt(f) } - fn fmt_dangling_comments(&self, _node: &ExprSet, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled as part of `fmt_fields` Ok(()) } 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 5e3247a249..126b0a7038 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs @@ -2,6 +2,7 @@ use ruff_formatter::{format_args, write, Buffer, FormatResult}; use ruff_python_ast::node::AnyNodeRef; use ruff_python_ast::ExprSetComp; +use crate::comments::SourceComment; use crate::context::PyFormatContext; use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses}; use crate::prelude::*; @@ -43,7 +44,11 @@ impl FormatNodeRule for FormatExprSetComp { ) } - fn fmt_dangling_comments(&self, _node: &ExprSetComp, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled as part of `fmt_fields` Ok(()) } diff --git a/crates/ruff_python_formatter/src/expression/expr_starred.rs b/crates/ruff_python_formatter/src/expression/expr_starred.rs index 6f3e7723f3..1f63e00f52 100644 --- a/crates/ruff_python_formatter/src/expression/expr_starred.rs +++ b/crates/ruff_python_formatter/src/expression/expr_starred.rs @@ -1,5 +1,6 @@ use ruff_python_ast::ExprStarred; +use crate::comments::SourceComment; use ruff_formatter::write; use ruff_python_ast::node::AnyNodeRef; @@ -22,9 +23,11 @@ impl FormatNodeRule for FormatExprStarred { write!(f, [text("*"), value.format()]) } - fn fmt_dangling_comments(&self, node: &ExprStarred, f: &mut PyFormatter) -> FormatResult<()> { - debug_assert_eq!(f.context().comments().dangling_comments(node), []); - + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { Ok(()) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_subscript.rs b/crates/ruff_python_formatter/src/expression/expr_subscript.rs index 1a2304801d..fbee7bd7f5 100644 --- a/crates/ruff_python_formatter/src/expression/expr_subscript.rs +++ b/crates/ruff_python_formatter/src/expression/expr_subscript.rs @@ -2,7 +2,7 @@ use ruff_formatter::{format_args, write, FormatRuleWithOptions}; use ruff_python_ast::node::{AnyNodeRef, AstNode}; use ruff_python_ast::{Expr, ExprSubscript}; -use crate::comments::trailing_comments; +use crate::comments::{trailing_comments, SourceComment}; use crate::context::PyFormatContext; use crate::context::{NodeLevel, WithNodeLevel}; use crate::expression::expr_tuple::TupleParentheses; @@ -81,7 +81,7 @@ impl FormatNodeRule for FormatExprSubscript { fn fmt_dangling_comments( &self, - _node: &ExprSubscript, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { // Handled inside of `fmt_fields` diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index df1d2db7ce..b062261762 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -5,6 +5,7 @@ use ruff_python_ast::{Expr, Ranged}; use ruff_text_size::TextRange; use crate::builders::parenthesize_if_expands; +use crate::comments::SourceComment; use crate::expression::parentheses::{ empty_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses, }; @@ -162,7 +163,11 @@ impl FormatNodeRule for FormatExprTuple { } } - fn fmt_dangling_comments(&self, _node: &ExprTuple, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled in `fmt_fields` Ok(()) } diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index d5f867f8fc..97c5535985 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -16,8 +16,6 @@ use crate::expression::parentheses::{ }; use crate::prelude::*; -use self::expr_bool_op::BoolOpLayout; - pub(crate) mod expr_attribute; pub(crate) mod expr_await; pub(crate) mod expr_bin_op; @@ -69,13 +67,7 @@ impl FormatRule> for FormatExpr { let parentheses = self.parentheses; let format_expr = format_with(|f| match expression { - Expr::BoolOp(expr) => expr - .format() - .with_options(BoolOpLayout { - parentheses: Some(parentheses), - chained: false, - }) - .fmt(f), + Expr::BoolOp(expr) => expr.format().fmt(f), Expr::NamedExpr(expr) => expr.format().fmt(f), Expr::BinOp(expr) => expr.format().fmt(f), Expr::UnaryOp(expr) => expr.format().fmt(f), @@ -90,7 +82,7 @@ impl FormatRule> for FormatExpr { Expr::Await(expr) => expr.format().fmt(f), Expr::Yield(expr) => expr.format().fmt(f), Expr::YieldFrom(expr) => expr.format().fmt(f), - Expr::Compare(expr) => expr.format().with_options(Some(parentheses)).fmt(f), + Expr::Compare(expr) => expr.format().fmt(f), Expr::Call(expr) => expr.format().fmt(f), Expr::FormattedValue(expr) => expr.format().fmt(f), Expr::FString(expr) => expr.format().fmt(f), diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index 6c124f7d90..68f26b1d7b 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -15,7 +15,7 @@ use ruff_source_file::Locator; use ruff_text_size::TextLen; use crate::comments::{ - dangling_node_comments, leading_node_comments, trailing_node_comments, Comments, + dangling_comments, leading_comments, trailing_comments, Comments, SourceComment, }; use crate::context::PyFormatContext; pub use crate::options::{MagicTrailingComma, PyFormatOptions, QuoteStyle}; @@ -46,10 +46,14 @@ where N: AstNode, { fn fmt(&self, node: &N, f: &mut PyFormatter) -> FormatResult<()> { - leading_node_comments(node).fmt(f)?; + let comments = f.context().comments().clone(); + + let node_comments = comments.leading_dangling_trailing_comments(node.as_any_node_ref()); + + leading_comments(node_comments.leading).fmt(f)?; self.fmt_node(node, f)?; - self.fmt_dangling_comments(node, f)?; - trailing_node_comments(node).fmt(f) + self.fmt_dangling_comments(node_comments.dangling, f)?; + trailing_comments(node_comments.trailing).fmt(f) } /// Formats the node without comments. Ignores any suppression comments. @@ -69,8 +73,12 @@ where /// no comments are dropped. /// /// A node can have dangling comments if all its children are tokens or if all node children are optional. - fn fmt_dangling_comments(&self, node: &N, f: &mut PyFormatter) -> FormatResult<()> { - dangling_node_comments(node).fmt(f) + fn fmt_dangling_comments( + &self, + dangling_node_comments: &[SourceComment], + f: &mut PyFormatter, + ) -> FormatResult<()> { + dangling_comments(dangling_node_comments).fmt(f) } } diff --git a/crates/ruff_python_formatter/src/other/arguments.rs b/crates/ruff_python_formatter/src/other/arguments.rs index 6810397b65..40cece7d9e 100644 --- a/crates/ruff_python_formatter/src/other/arguments.rs +++ b/crates/ruff_python_formatter/src/other/arguments.rs @@ -1,3 +1,4 @@ +use crate::comments::SourceComment; use ruff_formatter::write; use ruff_python_ast::node::AstNode; use ruff_python_ast::{Arguments, Expr, Ranged}; @@ -99,7 +100,11 @@ impl FormatNodeRule for FormatArguments { ) } - fn fmt_dangling_comments(&self, _node: &Arguments, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled in `fmt_fields` Ok(()) } diff --git a/crates/ruff_python_formatter/src/other/comprehension.rs b/crates/ruff_python_formatter/src/other/comprehension.rs index 8654a67f87..951e6f71cb 100644 --- a/crates/ruff_python_formatter/src/other/comprehension.rs +++ b/crates/ruff_python_formatter/src/other/comprehension.rs @@ -1,4 +1,4 @@ -use crate::comments::{leading_comments, trailing_comments}; +use crate::comments::{leading_comments, trailing_comments, SourceComment}; use crate::expression::expr_tuple::TupleParentheses; use crate::prelude::*; use crate::AsFormat; @@ -98,7 +98,7 @@ impl FormatNodeRule for FormatComprehension { fn fmt_dangling_comments( &self, - _node: &Comprehension, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { // dangling comments are formatted as part of fmt_fields 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 052710e7c2..2f3524ba23 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 @@ -1,4 +1,4 @@ -use crate::comments::trailing_comments; +use crate::comments::{trailing_comments, SourceComment}; use crate::expression::maybe_parenthesize_expression; use crate::expression::parentheses::Parenthesize; use crate::prelude::*; @@ -81,7 +81,7 @@ impl FormatNodeRule for FormatExceptHandlerExceptHan fn fmt_dangling_comments( &self, - _node: &ExceptHandlerExceptHandler, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { // dangling comments are formatted as part of fmt_fields diff --git a/crates/ruff_python_formatter/src/other/match_case.rs b/crates/ruff_python_formatter/src/other/match_case.rs index bd3050af0e..dcbcbcce87 100644 --- a/crates/ruff_python_formatter/src/other/match_case.rs +++ b/crates/ruff_python_formatter/src/other/match_case.rs @@ -1,7 +1,7 @@ use ruff_formatter::{write, Buffer, FormatResult}; use ruff_python_ast::MatchCase; -use crate::comments::trailing_comments; +use crate::comments::{trailing_comments, SourceComment}; use crate::not_yet_implemented_custom_text; use crate::prelude::*; use crate::{FormatNodeRule, PyFormatter}; @@ -54,7 +54,11 @@ impl FormatNodeRule for FormatMatchCase { ) } - fn fmt_dangling_comments(&self, _node: &MatchCase, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled as part of `fmt_fields` Ok(()) } diff --git a/crates/ruff_python_formatter/src/other/parameters.rs b/crates/ruff_python_formatter/src/other/parameters.rs index 34696fbe52..3c10c8049f 100644 --- a/crates/ruff_python_formatter/src/other/parameters.rs +++ b/crates/ruff_python_formatter/src/other/parameters.rs @@ -262,7 +262,11 @@ impl FormatNodeRule for FormatParameters { } } - fn fmt_dangling_comments(&self, _node: &Parameters, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled in `fmt_fields` Ok(()) } diff --git a/crates/ruff_python_formatter/src/other/with_item.rs b/crates/ruff_python_formatter/src/other/with_item.rs index 49fd36393a..2bd2d90ba8 100644 --- a/crates/ruff_python_formatter/src/other/with_item.rs +++ b/crates/ruff_python_formatter/src/other/with_item.rs @@ -2,7 +2,7 @@ use ruff_python_ast::WithItem; use ruff_formatter::{write, Buffer, FormatResult}; -use crate::comments::{leading_comments, trailing_comments}; +use crate::comments::{leading_comments, trailing_comments, SourceComment}; use crate::expression::maybe_parenthesize_expression; use crate::expression::parentheses::Parenthesize; use crate::prelude::*; @@ -47,7 +47,11 @@ impl FormatNodeRule for FormatWithItem { Ok(()) } - fn fmt_dangling_comments(&self, _node: &WithItem, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { Ok(()) } } 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 56dce4a0c6..9a908b9593 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_class_def.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_class_def.rs @@ -2,7 +2,7 @@ use ruff_formatter::{write, Buffer}; use ruff_python_ast::{Ranged, StmtClassDef}; use ruff_python_trivia::lines_after_ignoring_trivia; -use crate::comments::{leading_comments, trailing_comments}; +use crate::comments::{leading_comments, trailing_comments, SourceComment}; use crate::prelude::*; use crate::statement::suite::SuiteKind; use crate::FormatNodeRule; @@ -129,7 +129,7 @@ impl FormatNodeRule for FormatStmtClassDef { fn fmt_dangling_comments( &self, - _node: &StmtClassDef, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { // handled in fmt_fields diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index 02dd1f97b1..77592c732e 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -1,5 +1,5 @@ use crate::builders::{parenthesize_if_expands, PyFormatterExtensions}; -use crate::comments::dangling_node_comments; +use crate::comments::{dangling_node_comments, SourceComment}; use crate::expression::maybe_parenthesize_expression; use crate::expression::parentheses::Parenthesize; use crate::{FormatNodeRule, PyFormatter}; @@ -53,7 +53,11 @@ impl FormatNodeRule for FormatStmtDelete { } } - fn fmt_dangling_comments(&self, _node: &StmtDelete, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled in `fmt_fields` Ok(()) } diff --git a/crates/ruff_python_formatter/src/statement/stmt_for.rs b/crates/ruff_python_formatter/src/statement/stmt_for.rs index e9cac17d23..bc2bbf3a67 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_for.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_for.rs @@ -1,7 +1,7 @@ use ruff_formatter::{format_args, write, Buffer, FormatResult}; use ruff_python_ast::{Expr, Ranged, Stmt, StmtFor}; -use crate::comments::{leading_alternate_branch_comments, trailing_comments}; +use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment}; use crate::expression::expr_tuple::TupleParentheses; use crate::expression::maybe_parenthesize_expression; use crate::expression::parentheses::Parenthesize; @@ -86,7 +86,11 @@ impl FormatNodeRule for FormatStmtFor { Ok(()) } - fn fmt_dangling_comments(&self, _node: &StmtFor, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled in `fmt_fields` Ok(()) } 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 fcf4a0f603..b5ce2e4d45 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_function_def.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_function_def.rs @@ -2,7 +2,7 @@ use ruff_formatter::write; use ruff_python_ast::{Parameters, Ranged, StmtFunctionDef}; use ruff_python_trivia::{lines_after_ignoring_trivia, SimpleTokenKind, SimpleTokenizer}; -use crate::comments::{leading_comments, trailing_comments}; +use crate::comments::{leading_comments, trailing_comments, SourceComment}; use crate::expression::maybe_parenthesize_expression; use crate::expression::parentheses::{Parentheses, Parenthesize}; use crate::prelude::*; @@ -147,7 +147,7 @@ impl FormatNodeRule for FormatStmtFunctionDef { fn fmt_dangling_comments( &self, - _node: &StmtFunctionDef, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { // Handled in `fmt_fields` diff --git a/crates/ruff_python_formatter/src/statement/stmt_if.rs b/crates/ruff_python_formatter/src/statement/stmt_if.rs index 7eb8652fc0..35596b3c32 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_if.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_if.rs @@ -1,4 +1,4 @@ -use crate::comments::{leading_alternate_branch_comments, trailing_comments}; +use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment}; use crate::expression::maybe_parenthesize_expression; use crate::expression::parentheses::Parenthesize; use crate::prelude::*; @@ -43,7 +43,11 @@ impl FormatNodeRule for FormatStmtIf { Ok(()) } - fn fmt_dangling_comments(&self, _node: &StmtIf, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled by `fmt_fields` Ok(()) } 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 433293cfa9..ab7ffd9b9a 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_import_from.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_import_from.rs @@ -4,6 +4,7 @@ use ruff_python_ast::node::AstNode; use ruff_python_ast::{Ranged, StmtImportFrom}; use crate::builders::{parenthesize_if_expands, PyFormatterExtensions, TrailingComma}; +use crate::comments::SourceComment; use crate::expression::parentheses::parenthesized; use crate::{AsFormat, FormatNodeRule, PyFormatter}; @@ -71,7 +72,7 @@ impl FormatNodeRule for FormatStmtImportFrom { fn fmt_dangling_comments( &self, - _node: &StmtImportFrom, + _dangling_comments: &[SourceComment], _f: &mut PyFormatter, ) -> FormatResult<()> { // Handled in `fmt_fields` diff --git a/crates/ruff_python_formatter/src/statement/stmt_match.rs b/crates/ruff_python_formatter/src/statement/stmt_match.rs index d56b739590..643204fafd 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_match.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_match.rs @@ -1,7 +1,7 @@ use ruff_formatter::{format_args, write, Buffer, FormatResult}; use ruff_python_ast::StmtMatch; -use crate::comments::{leading_alternate_branch_comments, trailing_comments}; +use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment}; use crate::context::{NodeLevel, WithNodeLevel}; use crate::expression::maybe_parenthesize_expression; use crate::expression::parentheses::Parenthesize; @@ -64,7 +64,11 @@ impl FormatNodeRule for FormatStmtMatch { Ok(()) } - fn fmt_dangling_comments(&self, _node: &StmtMatch, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled as part of `fmt_fields` Ok(()) } diff --git a/crates/ruff_python_formatter/src/statement/stmt_try.rs b/crates/ruff_python_formatter/src/statement/stmt_try.rs index f869fdf3bf..be5e108088 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_try.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_try.rs @@ -93,7 +93,11 @@ impl FormatNodeRule for FormatStmtTry { write!(f, [comments::dangling_comments(dangling_comments)]) } - fn fmt_dangling_comments(&self, _node: &StmtTry, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // dangling comments are formatted as part of AnyStatementTry::fmt Ok(()) } diff --git a/crates/ruff_python_formatter/src/statement/stmt_try_star.rs b/crates/ruff_python_formatter/src/statement/stmt_try_star.rs new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/crates/ruff_python_formatter/src/statement/stmt_try_star.rs @@ -0,0 +1 @@ + diff --git a/crates/ruff_python_formatter/src/statement/stmt_while.rs b/crates/ruff_python_formatter/src/statement/stmt_while.rs index 3ee9e1a7a3..504c6f8489 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_while.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_while.rs @@ -1,4 +1,4 @@ -use crate::comments::{leading_alternate_branch_comments, trailing_comments}; +use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment}; use crate::expression::maybe_parenthesize_expression; use crate::expression::parentheses::Parenthesize; use crate::prelude::*; @@ -62,7 +62,11 @@ impl FormatNodeRule for FormatStmtWhile { Ok(()) } - fn fmt_dangling_comments(&self, _node: &StmtWhile, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled in `fmt_fields` Ok(()) } diff --git a/crates/ruff_python_formatter/src/statement/stmt_with.rs b/crates/ruff_python_formatter/src/statement/stmt_with.rs index cb7493dbdd..fb08c27f76 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_with.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_with.rs @@ -4,7 +4,7 @@ use ruff_python_ast::{Ranged, StmtWith}; use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer}; use ruff_text_size::TextRange; -use crate::comments::trailing_comments; +use crate::comments::{trailing_comments, SourceComment}; use crate::expression::parentheses::{ in_parentheses_only_soft_line_break_or_space, optional_parentheses, parenthesized, }; @@ -88,7 +88,11 @@ impl FormatNodeRule for FormatStmtWith { ) } - fn fmt_dangling_comments(&self, _node: &StmtWith, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled in `fmt_fields` Ok(()) } diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index 0890e028f5..383589dde5 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -117,8 +117,10 @@ impl FormatRule> for FormatSuite { SuiteKind::TopLevel => SuiteChildStatement::Other(first), }; - let (mut preceding, mut after_class_docstring) = if comments - .leading_comments(first) + let first_comments = comments.leading_dangling_trailing_comments(first); + + let (mut preceding, mut after_class_docstring) = if first_comments + .leading .iter() .any(|comment| comment.is_suppression_off_comment(source)) { @@ -128,8 +130,8 @@ impl FormatRule> for FormatSuite { )?, false, ) - } else if comments - .trailing_comments(first) + } else if first_comments + .trailing .iter() .any(|comment| comment.is_suppression_off_comment(source)) { @@ -291,8 +293,10 @@ impl FormatRule> for FormatSuite { } } - if comments - .leading_comments(following) + let following_comments = comments.leading_dangling_trailing_comments(following); + + if following_comments + .leading .iter() .any(|comment| comment.is_suppression_off_comment(source)) { @@ -301,8 +305,8 @@ impl FormatRule> for FormatSuite { &mut iter, f, )?; - } else if comments - .trailing_comments(following) + } else if following_comments + .trailing .iter() .any(|comment| comment.is_suppression_off_comment(source)) { 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 8c82caec1e..0c48581530 100644 --- a/crates/ruff_python_formatter/src/type_param/type_params.rs +++ b/crates/ruff_python_formatter/src/type_param/type_params.rs @@ -1,5 +1,5 @@ use crate::builders::PyFormatterExtensions; -use crate::comments::trailing_comments; +use crate::comments::{trailing_comments, SourceComment}; use crate::expression::parentheses::parenthesized; use crate::prelude::*; use ruff_formatter::write; @@ -34,7 +34,11 @@ impl FormatNodeRule for FormatTypeParams { parenthesized("[", &items, "]").fmt(f) } - fn fmt_dangling_comments(&self, _node: &TypeParams, _f: &mut PyFormatter) -> FormatResult<()> { + fn fmt_dangling_comments( + &self, + _dangling_comments: &[SourceComment], + _f: &mut PyFormatter, + ) -> FormatResult<()> { // Handled in `fmt_fields` Ok(()) }