Implement module formatting using JoinNodesBuilder (#4808)

* Implement module formatting using JoinNodesBuilder

This uses JoinNodesBuilder to implement module formatting for #4800

See the snapshots for the changed behaviour. See one PR up for a CLI that i used to verify the trailing new line behaviour
This commit is contained in:
konstin 2023-06-05 10:35:05 +02:00 committed by GitHub
parent c65f47d7c4
commit ff37d7af23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 1176 additions and 1524 deletions

View file

@ -1,8 +1,7 @@
use crate::AsFormat;
use crate::{FormatNodeRule, PyFormatter};
use crate::statement::suite::SuiteLevel;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::hard_line_break;
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ModModule;
#[derive(Default)]
@ -10,9 +9,13 @@ pub struct FormatModModule;
impl FormatNodeRule<ModModule> for FormatModModule {
fn fmt_fields(&self, item: &ModModule, f: &mut PyFormatter) -> FormatResult<()> {
for stmt in &item.body {
write!(f, [stmt.format(), hard_line_break()])?;
}
Ok(())
write!(
f,
[
item.body.format().with_options(SuiteLevel::TopLevel),
// Trailing newline at the end of the file
hard_line_break()
]
)
}
}