mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:53 +00:00
Add builders for common comment rendering (#3232)
This commit is contained in:
parent
a8a312e862
commit
51bca19c1d
13 changed files with 195 additions and 433 deletions
|
@ -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))])?;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue