mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
format StmtTryStar (#5418)
This commit is contained in:
parent
a973019358
commit
ca5e10b5ea
5 changed files with 205 additions and 23 deletions
|
@ -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!(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue