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:
@@ -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()
|
||||
]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user