format StmtTryStar (#5418)

This commit is contained in:
David Szotten 2023-06-29 07:07:33 +01:00 committed by GitHub
parent a973019358
commit ca5e10b5ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 205 additions and 23 deletions

View file

@ -2,12 +2,33 @@ use crate::comments::trailing_comments;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::FormatRuleWithOptions;
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::node::AstNode;
use rustpython_parser::ast::ExceptHandlerExceptHandler;
#[derive(Copy, Clone, Default)]
pub enum ExceptHandlerKind {
#[default]
Regular,
Starred,
}
#[derive(Default)]
pub struct FormatExceptHandlerExceptHandler;
pub struct FormatExceptHandlerExceptHandler {
except_handler_kind: ExceptHandlerKind,
}
impl FormatRuleWithOptions<ExceptHandlerExceptHandler, PyFormatContext<'_>>
for FormatExceptHandlerExceptHandler
{
type Options = ExceptHandlerKind;
fn with_options(mut self, options: Self::Options) -> Self {
self.except_handler_kind = options;
self
}
}
impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHandler {
fn fmt_fields(
@ -25,7 +46,16 @@ impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHan
let comments_info = f.context().comments().clone();
let dangling_comments = comments_info.dangling_comments(item.as_any_node_ref());
write!(f, [text("except")])?;
write!(
f,
[
text("except"),
match self.except_handler_kind {
ExceptHandlerKind::Regular => None,
ExceptHandlerKind::Starred => Some(text("*")),
}
]
)?;
if let Some(type_) = type_ {
write!(