mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 04:19:18 +00:00
Implement basic module formatting (#4784)
* Add Format for Stmt * Implement basic module formatting This implements formatting each statement in a module with a hard line break in between, so that we can start formatting statements. Basic testing is done by the snapshots
This commit is contained in:
parent
28aad95414
commit
63d892f1e4
54 changed files with 2926 additions and 1171 deletions
|
@ -1,3 +1,8 @@
|
|||
use crate::context::PyFormatContext;
|
||||
use crate::{AsFormat, IntoFormat, PyFormatter};
|
||||
use ruff_formatter::{Format, FormatOwnedWithRule, FormatRefWithRule, FormatResult, FormatRule};
|
||||
use rustpython_parser::ast::Stmt;
|
||||
|
||||
pub(crate) mod stmt_ann_assign;
|
||||
pub(crate) mod stmt_assert;
|
||||
pub(crate) mod stmt_assign;
|
||||
|
@ -25,3 +30,54 @@ pub(crate) mod stmt_try;
|
|||
pub(crate) mod stmt_try_star;
|
||||
pub(crate) mod stmt_while;
|
||||
pub(crate) mod stmt_with;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmt;
|
||||
|
||||
impl FormatRule<Stmt, PyFormatContext<'_>> for FormatStmt {
|
||||
fn fmt(&self, item: &Stmt, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match item {
|
||||
Stmt::FunctionDef(x) => x.format().fmt(f),
|
||||
Stmt::AsyncFunctionDef(x) => x.format().fmt(f),
|
||||
Stmt::ClassDef(x) => x.format().fmt(f),
|
||||
Stmt::Return(x) => x.format().fmt(f),
|
||||
Stmt::Delete(x) => x.format().fmt(f),
|
||||
Stmt::Assign(x) => x.format().fmt(f),
|
||||
Stmt::AugAssign(x) => x.format().fmt(f),
|
||||
Stmt::AnnAssign(x) => x.format().fmt(f),
|
||||
Stmt::For(x) => x.format().fmt(f),
|
||||
Stmt::AsyncFor(x) => x.format().fmt(f),
|
||||
Stmt::While(x) => x.format().fmt(f),
|
||||
Stmt::If(x) => x.format().fmt(f),
|
||||
Stmt::With(x) => x.format().fmt(f),
|
||||
Stmt::AsyncWith(x) => x.format().fmt(f),
|
||||
Stmt::Match(x) => x.format().fmt(f),
|
||||
Stmt::Raise(x) => x.format().fmt(f),
|
||||
Stmt::Try(x) => x.format().fmt(f),
|
||||
Stmt::TryStar(x) => x.format().fmt(f),
|
||||
Stmt::Assert(x) => x.format().fmt(f),
|
||||
Stmt::Import(x) => x.format().fmt(f),
|
||||
Stmt::ImportFrom(x) => x.format().fmt(f),
|
||||
Stmt::Global(x) => x.format().fmt(f),
|
||||
Stmt::Nonlocal(x) => x.format().fmt(f),
|
||||
Stmt::Expr(x) => x.format().fmt(f),
|
||||
Stmt::Pass(x) => x.format().fmt(f),
|
||||
Stmt::Break(x) => x.format().fmt(f),
|
||||
Stmt::Continue(x) => x.format().fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for Stmt {
|
||||
type Format<'a> = FormatRefWithRule<'a, Stmt, FormatStmt, PyFormatContext<'ast>>;
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatRefWithRule::new(self, FormatStmt::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for Stmt {
|
||||
type Format = FormatOwnedWithRule<Stmt, FormatStmt, PyFormatContext<'ast>>;
|
||||
fn into_format(self) -> Self::Format {
|
||||
FormatOwnedWithRule::new(self, FormatStmt::default())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue