mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
Use single lookup for leading, dangling, and trailing comments (#6589)
This commit is contained in:
parent
81b1176f99
commit
29c0b9f91c
39 changed files with 319 additions and 387 deletions
|
@ -249,11 +249,24 @@ impl<K: std::hash::Hash + Eq, V> MultiMap<K, V> {
|
|||
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<V> {
|
||||
/// Returns the *leading*, *dangling*, and *trailing* parts of `key`.
|
||||
pub(super) fn leading_dangling_trailing(&self, key: &K) -> LeadingDanglingTrailing<V> {
|
||||
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<K: std::hash::Hash + Eq, V> MultiMap<K, V> {
|
|||
pub(super) fn all_parts(&self) -> impl Iterator<Item = &V> {
|
||||
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<K>(entry: &Entry, map: &'a MultiMap<K, V>) -> Self {
|
||||
impl<'a, T> LeadingDanglingTrailing<'a, T> {
|
||||
fn from_entry<K>(entry: &Entry, map: &'a MultiMap<K, T>) -> 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>>,
|
||||
std::slice::Iter<'a, T>,
|
||||
>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
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<B, F>(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<F>(&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<F>(&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<P>(&mut self, predicate: P) -> Option<Self::Item>
|
||||
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<B, F>(&mut self, f: F) -> Option<B>
|
||||
where
|
||||
F: FnMut(Self::Item) -> Option<B>,
|
||||
{
|
||||
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<P>(&mut self, predicate: P) -> Option<usize>
|
||||
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<usize>) {
|
||||
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<Self::Item> {
|
||||
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<V> 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<V> 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<K, V>,
|
||||
}
|
||||
|
||||
impl<K, V> 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::<Vec<_>>(),
|
||||
map.leading_dangling_trailing(&"a")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![1, 2, 3, 4]
|
||||
);
|
||||
}
|
||||
|
@ -832,7 +627,13 @@ mod tests {
|
|||
|
||||
assert!(map.has(&"a"));
|
||||
|
||||
assert_eq!(map.parts(&"a").copied().collect::<Vec<_>>(), vec![1, 2, 3]);
|
||||
assert_eq!(
|
||||
map.leading_dangling_trailing(&"a")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![1, 2, 3]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -850,7 +651,13 @@ mod tests {
|
|||
|
||||
assert!(map.has(&"a"));
|
||||
|
||||
assert_eq!(map.parts(&"a").copied().collect::<Vec<_>>(), vec![1, 2]);
|
||||
assert_eq!(
|
||||
map.leading_dangling_trailing(&"a")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![1, 2]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -866,7 +673,10 @@ mod tests {
|
|||
assert!(!map.has(&"a"));
|
||||
|
||||
assert_eq!(
|
||||
map.parts(&"a").copied().collect::<Vec<_>>(),
|
||||
map.leading_dangling_trailing(&"a")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
Vec::<i32>::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<_>>(), vec![1]);
|
||||
assert_eq!(
|
||||
map.leading_dangling_trailing(&"a")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
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<_>>(), vec![2]);
|
||||
assert_eq!(
|
||||
map.leading_dangling_trailing(&"b")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
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<_>>(), vec![3]);
|
||||
assert_eq!(
|
||||
map.leading_dangling_trailing(&"c")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
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<_>>(), vec![4, 5, 6]);
|
||||
assert_eq!(
|
||||
map.leading_dangling_trailing(&"d")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![4, 5, 6]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -919,7 +753,10 @@ mod tests {
|
|||
assert_eq!(map.trailing(&"a"), [4]);
|
||||
|
||||
assert_eq!(
|
||||
map.parts(&"a").copied().collect::<Vec<_>>(),
|
||||
map.leading_dangling_trailing(&"a")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
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::<Vec<_>>(),
|
||||
map.leading_dangling_trailing(&"a")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
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<_>>(), vec![2, 1, 3]);
|
||||
assert_eq!(
|
||||
map.leading_dangling_trailing(&"a")
|
||||
.into_iter()
|
||||
.copied()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![2, 1, 3]
|
||||
);
|
||||
|
||||
assert!(map.has(&"a"));
|
||||
}
|
||||
|
|
|
@ -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<T>(
|
||||
&self,
|
||||
node: T,
|
||||
) -> impl Iterator<Item = &SourceComment>
|
||||
) -> LeadingDanglingTrailingComments
|
||||
where
|
||||
T: Into<AnyNodeRef<'a>>,
|
||||
{
|
||||
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>,
|
||||
|
|
|
@ -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<ExprAttribute> for FormatExprAttribute {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &ExprAttribute,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// handle in `fmt_fields`
|
||||
|
|
|
@ -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<ExprBinOp> 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(())
|
||||
}
|
||||
|
|
|
@ -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<Parentheses>,
|
||||
chained: bool,
|
||||
layout: BoolOpLayout,
|
||||
}
|
||||
|
||||
pub struct BoolOpLayout {
|
||||
pub(crate) parentheses: Option<Parentheses>,
|
||||
pub(crate) chained: bool,
|
||||
#[derive(Default, Copy, Clone)]
|
||||
pub enum BoolOpLayout {
|
||||
#[default]
|
||||
Default,
|
||||
Chained,
|
||||
}
|
||||
|
||||
impl FormatRuleWithOptions<ExprBoolOp, PyFormatContext<'_>> 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<ExprBoolOp> 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<PyFormatContext<'_>> 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())]),
|
||||
}
|
||||
|
|
|
@ -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<Parentheses>,
|
||||
}
|
||||
|
||||
impl FormatRuleWithOptions<ExprCompare, PyFormatContext<'_>> for FormatExprCompare {
|
||||
type Options = Option<Parentheses>;
|
||||
|
||||
fn with_options(mut self, options: Self::Options) -> Self {
|
||||
self.parentheses = options;
|
||||
self
|
||||
}
|
||||
}
|
||||
pub struct FormatExprCompare;
|
||||
|
||||
impl FormatNodeRule<ExprCompare> for FormatExprCompare {
|
||||
fn fmt_fields(&self, item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
|
@ -70,7 +59,11 @@ impl FormatNodeRule<ExprCompare> 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(())
|
||||
}
|
||||
|
|
|
@ -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<ExprConstant> for FormatExprConstant {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &ExprConstant,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
Ok(())
|
||||
|
|
|
@ -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<ExprDict> 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(())
|
||||
}
|
||||
|
|
|
@ -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<ExprGeneratorExp> for FormatExprGeneratorExp {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &ExprGeneratorExp,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled as part of `fmt_fields`
|
||||
|
|
|
@ -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<ExprLambda> 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(())
|
||||
}
|
||||
|
|
|
@ -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<ExprList> 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(())
|
||||
}
|
||||
|
|
|
@ -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<ExprListComp> for FormatExprListComp {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &ExprListComp,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled as part of `fmt_fields`
|
||||
|
|
|
@ -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<ExprName> 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(())
|
||||
}
|
||||
|
|
|
@ -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<ExprSet> 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(())
|
||||
}
|
||||
|
|
|
@ -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<ExprSetComp> 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(())
|
||||
}
|
||||
|
|
|
@ -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<ExprStarred> 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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<ExprSubscript> for FormatExprSubscript {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &ExprSubscript,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled inside of `fmt_fields`
|
||||
|
|
|
@ -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<ExprTuple> 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(())
|
||||
}
|
||||
|
|
|
@ -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<Expr, PyFormatContext<'_>> 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<Expr, PyFormatContext<'_>> 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),
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Arguments> 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(())
|
||||
}
|
||||
|
|
|
@ -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<Comprehension> 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
|
||||
|
|
|
@ -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<ExceptHandlerExceptHandler> 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
|
||||
|
|
|
@ -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<MatchCase> 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(())
|
||||
}
|
||||
|
|
|
@ -262,7 +262,11 @@ impl FormatNodeRule<Parameters> 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(())
|
||||
}
|
||||
|
|
|
@ -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<WithItem> 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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<StmtClassDef> for FormatStmtClassDef {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtClassDef,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// handled in fmt_fields
|
||||
|
|
|
@ -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<StmtDelete> 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(())
|
||||
}
|
||||
|
|
|
@ -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<StmtFor> 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(())
|
||||
}
|
||||
|
|
|
@ -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<StmtFunctionDef> for FormatStmtFunctionDef {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtFunctionDef,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
|
|
|
@ -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<StmtIf> 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(())
|
||||
}
|
||||
|
|
|
@ -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<StmtImportFrom> for FormatStmtImportFrom {
|
|||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtImportFrom,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
|
|
|
@ -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<StmtMatch> 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(())
|
||||
}
|
||||
|
|
|
@ -93,7 +93,11 @@ impl FormatNodeRule<StmtTry> 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(())
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -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<StmtWhile> 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(())
|
||||
}
|
||||
|
|
|
@ -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<StmtWith> 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(())
|
||||
}
|
||||
|
|
|
@ -117,8 +117,10 @@ impl FormatRule<Suite, PyFormatContext<'_>> 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<Suite, PyFormatContext<'_>> 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<Suite, PyFormatContext<'_>> 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<Suite, PyFormatContext<'_>> 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))
|
||||
{
|
||||
|
|
|
@ -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<TypeParams> 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(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue