Skip to content

Commit ff37d7a

Browse files
authored
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
1 parent c65f47d commit ff37d7a

File tree

43 files changed

+1168
-1516
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1168
-1516
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
use crate::AsFormat;
2-
use crate::{FormatNodeRule, PyFormatter};
1+
use crate::statement::suite::SuiteLevel;
2+
use crate::{AsFormat, FormatNodeRule, PyFormatter};
33
use ruff_formatter::prelude::hard_line_break;
44
use ruff_formatter::{write, Buffer, FormatResult};
5-
65
use rustpython_parser::ast::ModModule;
76

87
#[derive(Default)]
98
pub struct FormatModModule;
109

1110
impl FormatNodeRule<ModModule> for FormatModModule {
1211
fn fmt_fields(&self, item: &ModModule, f: &mut PyFormatter) -> FormatResult<()> {
13-
for stmt in &item.body {
14-
write!(f, [stmt.format(), hard_line_break()])?;
15-
}
16-
Ok(())
12+
write!(
13+
f,
14+
[
15+
item.body.format().with_options(SuiteLevel::TopLevel),
16+
// Trailing newline at the end of the file
17+
hard_line_break()
18+
]
19+
)
1720
}
1821
}

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__attribute_access_on_number_literals_py.snap

+6-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ y = 100(no)
3535
```diff
3636
--- Black
3737
+++ Ruff
38-
@@ -1,22 +1,20 @@
38+
@@ -1,21 +1,21 @@
3939
-x = (123456789).bit_count()
4040
+x = 123456789 .bit_count()
4141
x = (123456).__abs__()
@@ -53,8 +53,6 @@ y = 100(no)
5353
-x = 0o777.real
5454
-x = (0.000000006).hex()
5555
-x = -100.0000j
56-
-
57-
-if (10).real:
5856
+x = .1.is_integer()
5957
+x = 1. .imag
6058
+x = 1E+1.imag
@@ -69,11 +67,12 @@ y = 100(no)
6967
+x = 0O777 .real
7068
+x = 0.000000006 .hex()
7169
+x = -100.0000J
70+
71+
-if (10).real:
7272
+if 10 .real:
7373
...
74-
-
74+
7575
y = 100[no]
76-
y = 100(no)
7776
```
7877

7978
## Ruff Output
@@ -95,8 +94,10 @@ x = 0B1011 .conjugate()
9594
x = 0O777 .real
9695
x = 0.000000006 .hex()
9796
x = -100.0000J
97+
9898
if 10 .real:
9999
...
100+
100101
y = 100[no]
101102
y = 100(no)
102103
```

crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__black_test__class_blank_parentheses_py.snap

+21-13
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,35 @@ class NormalClass (
3636
```diff
3737
--- Black
3838
+++ Ruff
39-
@@ -1,30 +1,21 @@
39+
@@ -1,16 +1,16 @@
4040
-class SimpleClassWithBlankParentheses:
4141
+class SimpleClassWithBlankParentheses():
4242
pass
43-
-
44-
-
43+
44+
4545
-class ClassWithSpaceParentheses:
4646
+class ClassWithSpaceParentheses ( ):
4747
first_test_data = 90
4848
second_test_data = 100
4949
-
5050
def test_func(self):
5151
return None
52-
-
53-
-
52+
53+
5454
class ClassWithEmptyFunc(object):
5555
+
5656
def func_with_blank_parentheses():
5757
return 5
58-
-
59-
-
60-
def public_func_with_blank_parentheses():
61-
return None
62-
-
63-
-
58+
59+
@@ -20,11 +20,12 @@
60+
61+
6462
def class_under_the_func_with_blank_parentheses():
6563
- class InsideFunc:
6664
+ class InsideFunc():
6765
pass
68-
-
69-
-
66+
67+
7068
-class NormalClass:
7169
+class NormalClass (
7270
+):
@@ -80,20 +78,30 @@ class NormalClass (
8078
```py
8179
class SimpleClassWithBlankParentheses():
8280
pass
81+
82+
8383
class ClassWithSpaceParentheses ( ):
8484
first_test_data = 90
8585
second_test_data = 100
8686
def test_func(self):
8787
return None
88+
89+
8890
class ClassWithEmptyFunc(object):
8991
9092
def func_with_blank_parentheses():
9193
return 5
94+
95+
9296
def public_func_with_blank_parentheses():
9397
return None
98+
99+
94100
def class_under_the_func_with_blank_parentheses():
95101
class InsideFunc():
96102
pass
103+
104+
97105
class NormalClass (
98106
):
99107
def func_for_testing(self, first, second):

0 commit comments

Comments
 (0)