Skip to content

Commit

Permalink
Implement module formatting using JoinNodesBuilder
Browse files Browse the repository at this point in the history
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
  • Loading branch information
konstin committed Jun 5, 2023
1 parent c65f47d commit 446f987
Show file tree
Hide file tree
Showing 43 changed files with 1,170 additions and 1,517 deletions.
20 changes: 12 additions & 8 deletions crates/ruff_python_formatter/src/module/mod_module.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use crate::AsFormat;
use crate::{FormatNodeRule, PyFormatter};
use crate::builders::{JoinNodesBuilder, PyFormatterExtensions};
use crate::context::NodeLevel;
use crate::statement::suite::SuiteLevel;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::hard_line_break;
use ruff_formatter::{write, Buffer, FormatResult};

use ruff_formatter::{write, Buffer, Format, FormatResult};
use rustpython_parser::ast::ModModule;

#[derive(Default)]
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),
hard_line_break()
]
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ y = 100(no)
```diff
--- Black
+++ Ruff
@@ -1,22 +1,20 @@
@@ -1,21 +1,21 @@
-x = (123456789).bit_count()
+x = 123456789 .bit_count()
x = (123456).__abs__()
Expand All @@ -53,8 +53,6 @@ y = 100(no)
-x = 0o777.real
-x = (0.000000006).hex()
-x = -100.0000j
-
-if (10).real:
+x = .1.is_integer()
+x = 1. .imag
+x = 1E+1.imag
Expand All @@ -69,11 +67,12 @@ y = 100(no)
+x = 0O777 .real
+x = 0.000000006 .hex()
+x = -100.0000J
-if (10).real:
+if 10 .real:
...
-
y = 100[no]
y = 100(no)
```

## Ruff Output
Expand All @@ -95,8 +94,10 @@ x = 0B1011 .conjugate()
x = 0O777 .real
x = 0.000000006 .hex()
x = -100.0000J
if 10 .real:
...
y = 100[no]
y = 100(no)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,35 @@ class NormalClass (
```diff
--- Black
+++ Ruff
@@ -1,30 +1,21 @@
@@ -1,16 +1,16 @@
-class SimpleClassWithBlankParentheses:
+class SimpleClassWithBlankParentheses():
pass
-
-
-class ClassWithSpaceParentheses:
+class ClassWithSpaceParentheses ( ):
first_test_data = 90
second_test_data = 100
-
def test_func(self):
return None
-
-
class ClassWithEmptyFunc(object):
+
def func_with_blank_parentheses():
return 5
-
-
def public_func_with_blank_parentheses():
return None
-
-
@@ -20,11 +20,12 @@
def class_under_the_func_with_blank_parentheses():
- class InsideFunc:
+ class InsideFunc():
pass
-
-
-class NormalClass:
+class NormalClass (
+):
Expand All @@ -80,20 +78,30 @@ class NormalClass (
```py
class SimpleClassWithBlankParentheses():
pass
class ClassWithSpaceParentheses ( ):
first_test_data = 90
second_test_data = 100
def test_func(self):
return None
class ClassWithEmptyFunc(object):
def func_with_blank_parentheses():
return 5
def public_func_with_blank_parentheses():
return None
def class_under_the_func_with_blank_parentheses():
class InsideFunc():
pass
class NormalClass (
):
def func_for_testing(self, first, second):
Expand Down
Loading

0 comments on commit 446f987

Please sign in to comment.