mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Format StmtExpr (#4788)
This commit is contained in:
parent
4cd4b37e74
commit
a401989b7a
4 changed files with 77 additions and 19 deletions
|
@ -1,3 +1,7 @@
|
|||
use crate::prelude::*;
|
||||
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatRule};
|
||||
use rustpython_parser::ast::Expr;
|
||||
|
||||
pub(crate) mod expr_attribute;
|
||||
pub(crate) mod expr_await;
|
||||
pub(crate) mod expr_bin_op;
|
||||
|
@ -25,3 +29,54 @@ pub(crate) mod expr_tuple;
|
|||
pub(crate) mod expr_unary_op;
|
||||
pub(crate) mod expr_yield;
|
||||
pub(crate) mod expr_yield_from;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExpr;
|
||||
|
||||
impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
|
||||
fn fmt(&self, item: &Expr, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match item {
|
||||
Expr::BoolOp(expr) => expr.format().fmt(f),
|
||||
Expr::NamedExpr(expr) => expr.format().fmt(f),
|
||||
Expr::BinOp(expr) => expr.format().fmt(f),
|
||||
Expr::UnaryOp(expr) => expr.format().fmt(f),
|
||||
Expr::Lambda(expr) => expr.format().fmt(f),
|
||||
Expr::IfExp(expr) => expr.format().fmt(f),
|
||||
Expr::Dict(expr) => expr.format().fmt(f),
|
||||
Expr::Set(expr) => expr.format().fmt(f),
|
||||
Expr::ListComp(expr) => expr.format().fmt(f),
|
||||
Expr::SetComp(expr) => expr.format().fmt(f),
|
||||
Expr::DictComp(expr) => expr.format().fmt(f),
|
||||
Expr::GeneratorExp(expr) => expr.format().fmt(f),
|
||||
Expr::Await(expr) => expr.format().fmt(f),
|
||||
Expr::Yield(expr) => expr.format().fmt(f),
|
||||
Expr::YieldFrom(expr) => expr.format().fmt(f),
|
||||
Expr::Compare(expr) => expr.format().fmt(f),
|
||||
Expr::Call(expr) => expr.format().fmt(f),
|
||||
Expr::FormattedValue(expr) => expr.format().fmt(f),
|
||||
Expr::JoinedStr(expr) => expr.format().fmt(f),
|
||||
Expr::Constant(expr) => expr.format().fmt(f),
|
||||
Expr::Attribute(expr) => expr.format().fmt(f),
|
||||
Expr::Subscript(expr) => expr.format().fmt(f),
|
||||
Expr::Starred(expr) => expr.format().fmt(f),
|
||||
Expr::Name(expr) => expr.format().fmt(f),
|
||||
Expr::List(expr) => expr.format().fmt(f),
|
||||
Expr::Tuple(expr) => expr.format().fmt(f),
|
||||
Expr::Slice(expr) => expr.format().fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for Expr {
|
||||
type Format<'a> = FormatRefWithRule<'a, Expr, FormatExpr, PyFormatContext<'ast>>;
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatRefWithRule::new(self, FormatExpr::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for Expr {
|
||||
type Format = FormatOwnedWithRule<Expr, FormatExpr, PyFormatContext<'ast>>;
|
||||
fn into_format(self) -> Self::Format {
|
||||
FormatOwnedWithRule::new(self, FormatExpr::default())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue