Add builders for common comment rendering (#3232)

This commit is contained in:
Charlie Marsh 2023-02-25 23:16:24 -05:00 committed by GitHub
parent a8a312e862
commit 51bca19c1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 195 additions and 433 deletions

View file

@ -2,10 +2,10 @@ use ruff_formatter::prelude::*;
use ruff_formatter::write;
use ruff_text_size::TextSize;
use crate::builders::literal;
use crate::context::ASTFormatContext;
use crate::cst::{Excepthandler, ExcepthandlerKind};
use crate::format::builders::block;
use crate::format::comments::end_of_line_comments;
use crate::shared_traits::AsFormat;
pub struct FormatExcepthandler<'a> {
@ -41,21 +41,7 @@ impl Format<ASTFormatContext<'_>> for FormatExcepthandler<'_> {
}
}
write!(f, [text(":")])?;
// Format any end-of-line comments.
let mut first = true;
for range in excepthandler.trivia.iter().filter_map(|trivia| {
if trivia.relationship.is_trailing() {
trivia.kind.end_of_line_comment()
} else {
None
}
}) {
if std::mem::take(&mut first) {
write!(f, [line_suffix(&text(" "))])?;
}
write!(f, [line_suffix(&literal(range))])?;
}
write!(f, [end_of_line_comments(excepthandler)])?;
write!(f, [block_indent(&block(body))])?;