use ruff_formatter::prelude::*; use ruff_formatter::{write, Format}; use ruff_text_size::TextSize; use crate::context::ASTFormatContext; use crate::cst::Stmt; use crate::shared_traits::AsFormat; #[derive(Copy, Clone)] pub struct Block<'a> { body: &'a [Stmt], } impl Format> for Block<'_> { fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { for (i, stmt) in self.body.iter().enumerate() { if i > 0 { write!(f, [hard_line_break()])?; } write!(f, [stmt.format()])?; } Ok(()) } } #[inline] pub fn block(body: &[Stmt]) -> Block { Block { body } } pub(crate) const fn join_names(names: &[String]) -> JoinNames { JoinNames { names } } pub(crate) struct JoinNames<'a> { names: &'a [String], } impl Format for JoinNames<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { let mut join = f.join_with(text(", ")); for name in self.names { join.entry(&dynamic_text(name, TextSize::default())); } join.finish() } }