mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 10:22:24 +00:00
Formatter: Run generate.py for ElifElseClauses (#5864)
**Summary** This removes the diff for the next user of `generate.py`. It's effectively a refactoring. **Test Plan** No functional changes
This commit is contained in:
parent
0c7c81aa31
commit
5d41c832ad
4 changed files with 58 additions and 53 deletions
|
@ -617,46 +617,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtIf {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FormatRule<ast::ElifElseClause, PyFormatContext<'_>>
|
|
||||||
for crate::statement::stmt_if::FormatElifElseClause
|
|
||||||
{
|
|
||||||
#[inline]
|
|
||||||
fn fmt(
|
|
||||||
&self,
|
|
||||||
node: &ast::ElifElseClause,
|
|
||||||
f: &mut Formatter<PyFormatContext<'_>>,
|
|
||||||
) -> FormatResult<()> {
|
|
||||||
FormatNodeRule::<ast::ElifElseClause>::fmt(self, node, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ElifElseClause {
|
|
||||||
type Format<'a> = FormatRefWithRule<
|
|
||||||
'a,
|
|
||||||
ast::ElifElseClause,
|
|
||||||
crate::statement::stmt_if::FormatElifElseClause,
|
|
||||||
PyFormatContext<'ast>,
|
|
||||||
>;
|
|
||||||
fn format(&self) -> Self::Format<'_> {
|
|
||||||
FormatRefWithRule::new(
|
|
||||||
self,
|
|
||||||
crate::statement::stmt_if::FormatElifElseClause::default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ElifElseClause {
|
|
||||||
type Format = FormatOwnedWithRule<
|
|
||||||
ast::ElifElseClause,
|
|
||||||
crate::statement::stmt_if::FormatElifElseClause,
|
|
||||||
PyFormatContext<'ast>,
|
|
||||||
>;
|
|
||||||
fn into_format(self) -> Self::Format {
|
|
||||||
FormatOwnedWithRule::new(
|
|
||||||
self,
|
|
||||||
crate::statement::stmt_if::FormatElifElseClause::default(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FormatRule<ast::StmtWith, PyFormatContext<'_>>
|
impl FormatRule<ast::StmtWith, PyFormatContext<'_>>
|
||||||
for crate::statement::stmt_with::FormatStmtWith
|
for crate::statement::stmt_with::FormatStmtWith
|
||||||
{
|
{
|
||||||
|
@ -2974,3 +2934,43 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::Decorator {
|
||||||
FormatOwnedWithRule::new(self, crate::other::decorator::FormatDecorator::default())
|
FormatOwnedWithRule::new(self, crate::other::decorator::FormatDecorator::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FormatRule<ast::ElifElseClause, PyFormatContext<'_>>
|
||||||
|
for crate::other::elif_else_clause::FormatElifElseClause
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn fmt(
|
||||||
|
&self,
|
||||||
|
node: &ast::ElifElseClause,
|
||||||
|
f: &mut Formatter<PyFormatContext<'_>>,
|
||||||
|
) -> FormatResult<()> {
|
||||||
|
FormatNodeRule::<ast::ElifElseClause>::fmt(self, node, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ElifElseClause {
|
||||||
|
type Format<'a> = FormatRefWithRule<
|
||||||
|
'a,
|
||||||
|
ast::ElifElseClause,
|
||||||
|
crate::other::elif_else_clause::FormatElifElseClause,
|
||||||
|
PyFormatContext<'ast>,
|
||||||
|
>;
|
||||||
|
fn format(&self) -> Self::Format<'_> {
|
||||||
|
FormatRefWithRule::new(
|
||||||
|
self,
|
||||||
|
crate::other::elif_else_clause::FormatElifElseClause::default(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ElifElseClause {
|
||||||
|
type Format = FormatOwnedWithRule<
|
||||||
|
ast::ElifElseClause,
|
||||||
|
crate::other::elif_else_clause::FormatElifElseClause,
|
||||||
|
PyFormatContext<'ast>,
|
||||||
|
>;
|
||||||
|
fn into_format(self) -> Self::Format {
|
||||||
|
FormatOwnedWithRule::new(
|
||||||
|
self,
|
||||||
|
crate::other::elif_else_clause::FormatElifElseClause::default(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
16
crates/ruff_python_formatter/src/other/elif_else_clause.rs
Normal file
16
crates/ruff_python_formatter/src/other/elif_else_clause.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
use crate::statement::stmt_if::format_elif_else_clause;
|
||||||
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
|
use ruff_formatter::FormatResult;
|
||||||
|
use rustpython_parser::ast::ElifElseClause;
|
||||||
|
|
||||||
|
/// Note that this implementation misses the leading newlines before the leading comments because
|
||||||
|
/// it does not have access to the last node of the previous branch. The `StmtIf` therefore doesn't
|
||||||
|
/// call this but `format_elif_else_clause` directly.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct FormatElifElseClause;
|
||||||
|
|
||||||
|
impl FormatNodeRule<ElifElseClause> for FormatElifElseClause {
|
||||||
|
fn fmt_fields(&self, item: &ElifElseClause, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
|
format_elif_else_clause(item, f, None)
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ pub(crate) mod arg_with_default;
|
||||||
pub(crate) mod arguments;
|
pub(crate) mod arguments;
|
||||||
pub(crate) mod comprehension;
|
pub(crate) mod comprehension;
|
||||||
pub(crate) mod decorator;
|
pub(crate) mod decorator;
|
||||||
|
pub(crate) mod elif_else_clause;
|
||||||
pub(crate) mod except_handler_except_handler;
|
pub(crate) mod except_handler_except_handler;
|
||||||
pub(crate) mod identifier;
|
pub(crate) mod identifier;
|
||||||
pub(crate) mod keyword;
|
pub(crate) mod keyword;
|
||||||
|
|
|
@ -49,21 +49,9 @@ impl FormatNodeRule<StmtIf> for FormatStmtIf {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Note that this implementation misses the leading newlines before the leading comments because
|
|
||||||
/// it does not have access to the last node of the previous branch. The `StmtIf` therefore doesn't
|
|
||||||
/// call this but `format_elif_else_clause` directly.
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct FormatElifElseClause;
|
|
||||||
|
|
||||||
impl FormatNodeRule<ElifElseClause> for FormatElifElseClause {
|
|
||||||
fn fmt_fields(&self, item: &ElifElseClause, f: &mut PyFormatter) -> FormatResult<()> {
|
|
||||||
format_elif_else_clause(item, f, None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extracted so we can implement `FormatElifElseClause` but also pass in `last_node` from
|
/// Extracted so we can implement `FormatElifElseClause` but also pass in `last_node` from
|
||||||
/// `FormatStmtIf`
|
/// `FormatStmtIf`
|
||||||
fn format_elif_else_clause(
|
pub(crate) fn format_elif_else_clause(
|
||||||
item: &ElifElseClause,
|
item: &ElifElseClause,
|
||||||
f: &mut PyFormatter,
|
f: &mut PyFormatter,
|
||||||
last_node: Option<AnyNodeRef>,
|
last_node: Option<AnyNodeRef>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue