mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:43 +00:00
Add StmtKind::Try
; fix trailing newlines (#3074)
This commit is contained in:
parent
b657468346
commit
6e02405bd6
6 changed files with 124 additions and 9 deletions
46
crates/ruff_python_formatter/src/format/excepthandler.rs
Normal file
46
crates/ruff_python_formatter/src/format/excepthandler.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use ruff_formatter::prelude::*;
|
||||
use ruff_formatter::write;
|
||||
use ruff_text_size::TextSize;
|
||||
|
||||
use crate::context::ASTFormatContext;
|
||||
use crate::cst::{Excepthandler, ExcepthandlerKind};
|
||||
use crate::format::builders::block;
|
||||
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 ExcepthandlerKind::ExceptHandler { type_, name, body } = &self.item.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, TextSize::default()),
|
||||
]
|
||||
)?;
|
||||
}
|
||||
}
|
||||
write!(f, [text(":")])?;
|
||||
write!(f, [block_indent(&block(body))])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue