ruff/crates/ruff_python_formatter/src/format/excepthandler.rs
Micha Reiser 86ced3516b
Introduce SourceCodeSlice to reduce the size of FormatElement (#4622)
Introduce `SourceCodeSlice` to reduce the size of `FormatElement`
2023-05-24 15:04:52 +00:00

41 lines
1.2 KiB
Rust

use ruff_formatter::prelude::*;
use ruff_formatter::write;
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> {
item: &'a Excepthandler,
}
impl AsFormat<ASTFormatContext<'_>> for Excepthandler {
type Format<'a> = FormatExcepthandler<'a>;
fn format(&self) -> Self::Format<'_> {
FormatExcepthandler { item: self }
}
}
impl Format<ASTFormatContext<'_>> for FormatExcepthandler<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let excepthandler = self.item;
let ExcepthandlerKind::ExceptHandler { type_, name, body } = &excepthandler.node;
write!(f, [text("except")])?;
if let Some(type_) = &type_ {
write!(f, [space(), type_.format()])?;
if let Some(name) = &name {
write!(f, [space(), text("as"), space(), dynamic_text(name, None)])?;
}
}
write!(f, [text(":")])?;
write!(f, [end_of_line_comments(excepthandler)])?;
write!(f, [block_indent(&block(body))])?;
Ok(())
}
}