Format StmtTry (#5222)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
David Szotten 2023-06-28 11:02:15 +01:00 committed by GitHub
parent 9e2fd0c620
commit 1979103ec0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 583 additions and 123 deletions

View file

@ -1,5 +1,9 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::comments::trailing_comments;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::node::AstNode;
use rustpython_parser::ast::ExceptHandlerExceptHandler;
#[derive(Default)]
@ -11,6 +15,43 @@ impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHan
item: &ExceptHandlerExceptHandler,
f: &mut PyFormatter,
) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let ExceptHandlerExceptHandler {
range: _,
type_,
name,
body,
} = item;
let comments_info = f.context().comments().clone();
let dangling_comments = comments_info.dangling_comments(item.as_any_node_ref());
write!(f, [text("except")])?;
if let Some(type_) = type_ {
write!(
f,
[space(), type_.format().with_options(Parenthesize::IfBreaks)]
)?;
if let Some(name) = name {
write!(f, [space(), text("as"), space(), name.format()])?;
}
}
write!(
f,
[
text(":"),
trailing_comments(dangling_comments),
block_indent(&body.format())
]
)
}
fn fmt_dangling_comments(
&self,
_node: &ExceptHandlerExceptHandler,
_f: &mut PyFormatter,
) -> FormatResult<()> {
// dangling comments are formatted as part of fmt_fields
Ok(())
}
}