Reduce comments.clone calls (#7144)

This commit is contained in:
Micha Reiser 2023-09-05 11:32:56 +02:00 committed by GitHub
parent 40ee4909b5
commit 175b3702c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -32,17 +32,11 @@ pub(crate) enum FormatLeadingComments<'a> {
impl Format<PyFormatContext<'_>> for FormatLeadingComments<'_> { impl Format<PyFormatContext<'_>> for FormatLeadingComments<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> { fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let comments = f.context().comments().clone(); fn write_leading_comments(
comments: &[SourceComment],
let leading_comments = match self { f: &mut PyFormatter,
FormatLeadingComments::Node(node) => comments.leading(*node), ) -> FormatResult<()> {
FormatLeadingComments::Comments(comments) => comments, for comment in comments.iter().filter(|comment| comment.is_unformatted()) {
};
for comment in leading_comments
.iter()
.filter(|comment| comment.is_unformatted())
{
let lines_after_comment = lines_after(comment.end(), f.context().source()); let lines_after_comment = lines_after(comment.end(), f.context().source());
write!( write!(
f, f,
@ -54,6 +48,15 @@ impl Format<PyFormatContext<'_>> for FormatLeadingComments<'_> {
Ok(()) 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 /// 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<'_> { impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> { fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let comments = f.context().comments().clone(); fn write_trailing_comments(
comments: &[SourceComment],
let trailing_comments = match self { f: &mut PyFormatter,
FormatTrailingComments::Node(node) => comments.trailing(*node), ) -> FormatResult<()> {
FormatTrailingComments::Comments(comments) => comments,
};
let mut has_trailing_own_line_comment = false; let mut has_trailing_own_line_comment = false;
for trailing in trailing_comments for trailing in comments.iter().filter(|comment| comment.is_unformatted()) {
.iter()
.filter(|comment| comment.is_unformatted())
{
has_trailing_own_line_comment |= trailing.line_position().is_own_line(); has_trailing_own_line_comment |= trailing.line_position().is_own_line();
if has_trailing_own_line_comment { if has_trailing_own_line_comment {
@ -177,6 +174,15 @@ impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
Ok(()) 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`. /// Formats the dangling comments of `node`.