mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Reduce comments.clone
calls (#7144)
This commit is contained in:
parent
40ee4909b5
commit
175b3702c3
1 changed files with 70 additions and 64 deletions
|
@ -32,17 +32,11 @@ pub(crate) enum FormatLeadingComments<'a> {
|
|||
|
||||
impl Format<PyFormatContext<'_>> for FormatLeadingComments<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let comments = f.context().comments().clone();
|
||||
|
||||
let leading_comments = match self {
|
||||
FormatLeadingComments::Node(node) => comments.leading(*node),
|
||||
FormatLeadingComments::Comments(comments) => comments,
|
||||
};
|
||||
|
||||
for comment in leading_comments
|
||||
.iter()
|
||||
.filter(|comment| comment.is_unformatted())
|
||||
{
|
||||
fn write_leading_comments(
|
||||
comments: &[SourceComment],
|
||||
f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
for comment in comments.iter().filter(|comment| comment.is_unformatted()) {
|
||||
let lines_after_comment = lines_after(comment.end(), f.context().source());
|
||||
write!(
|
||||
f,
|
||||
|
@ -54,6 +48,15 @@ impl Format<PyFormatContext<'_>> for FormatLeadingComments<'_> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
match self {
|
||||
FormatLeadingComments::Node(node) => {
|
||||
let comments = f.context().comments().clone();
|
||||
write_leading_comments(comments.leading(*node), f)
|
||||
}
|
||||
FormatLeadingComments::Comments(comments) => write_leading_comments(comments, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats the leading `comments` of an alternate branch and ensures that it preserves the right
|
||||
|
@ -121,19 +124,13 @@ pub(crate) enum FormatTrailingComments<'a> {
|
|||
|
||||
impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let comments = f.context().comments().clone();
|
||||
|
||||
let trailing_comments = match self {
|
||||
FormatTrailingComments::Node(node) => comments.trailing(*node),
|
||||
FormatTrailingComments::Comments(comments) => comments,
|
||||
};
|
||||
|
||||
fn write_trailing_comments(
|
||||
comments: &[SourceComment],
|
||||
f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
let mut has_trailing_own_line_comment = false;
|
||||
|
||||
for trailing in trailing_comments
|
||||
.iter()
|
||||
.filter(|comment| comment.is_unformatted())
|
||||
{
|
||||
for trailing in comments.iter().filter(|comment| comment.is_unformatted()) {
|
||||
has_trailing_own_line_comment |= trailing.line_position().is_own_line();
|
||||
|
||||
if has_trailing_own_line_comment {
|
||||
|
@ -177,6 +174,15 @@ impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
match self {
|
||||
FormatTrailingComments::Node(node) => {
|
||||
let comments = f.context().comments().clone();
|
||||
write_trailing_comments(comments.trailing(*node), f)
|
||||
}
|
||||
FormatTrailingComments::Comments(comments) => write_trailing_comments(comments, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats the dangling comments of `node`.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue