mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Track formatting all comments
We currently don't format all comments as match statements are not yet implemented. We can work around this for the top level match statement by setting them manually formatted but the mocked-out top level match doesn't call into its children so they would still have unformatted comments
This commit is contained in:
parent
e2f7862404
commit
39beeb61f7
4 changed files with 91 additions and 25 deletions
|
@ -101,6 +101,7 @@ pub(crate) use format::{
|
|||
};
|
||||
use ruff_formatter::{SourceCode, SourceCodeSlice};
|
||||
use ruff_python_ast::node::AnyNodeRef;
|
||||
use ruff_python_ast::visitor::preorder::{PreorderVisitor, TraversalSignal};
|
||||
use ruff_python_index::CommentRanges;
|
||||
|
||||
use crate::comments::debug::{DebugComment, DebugComments};
|
||||
|
@ -401,6 +402,24 @@ impl<'a> Comments<'a> {
|
|||
);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
#[cfg(not(debug_assertions))]
|
||||
pub(crate) fn mark_verbatim_node_comments_formatted(&self, node: AnyNodeRef) {}
|
||||
|
||||
/// Marks the comments of a node printed in verbatim (suppressed) as formatted.
|
||||
///
|
||||
/// Marks the dangling comments and the comments of all descendants as formatted.
|
||||
/// The leading and trailing comments are not marked as formatted because they are formatted
|
||||
/// normally if `node` is the first or last node of a suppression range.
|
||||
#[cfg(debug_assertions)]
|
||||
pub(crate) fn mark_verbatim_node_comments_formatted(&self, node: AnyNodeRef) {
|
||||
for dangling in self.dangling_comments(node) {
|
||||
dangling.mark_formatted();
|
||||
}
|
||||
|
||||
node.visit_preorder(&mut MarkVerbatimCommentsAsFormattedVisitor(self));
|
||||
}
|
||||
|
||||
/// Returns an object that implements [Debug] for nicely printing the [`Comments`].
|
||||
pub(crate) fn debug(&'a self, source_code: SourceCode<'a>) -> DebugComments<'a> {
|
||||
DebugComments::new(&self.data.comments, source_code)
|
||||
|
@ -412,6 +431,18 @@ struct CommentsData<'a> {
|
|||
comments: CommentsMap<'a>,
|
||||
}
|
||||
|
||||
struct MarkVerbatimCommentsAsFormattedVisitor<'a>(&'a Comments<'a>);
|
||||
|
||||
impl<'a> PreorderVisitor<'a> for MarkVerbatimCommentsAsFormattedVisitor<'a> {
|
||||
fn enter_node(&mut self, node: AnyNodeRef<'a>) -> TraversalSignal {
|
||||
for comment in self.0.leading_dangling_trailing_comments(node) {
|
||||
comment.mark_formatted();
|
||||
}
|
||||
|
||||
TraversalSignal::Traverse
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use insta::assert_debug_snapshot;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue