Add empty lines before nested functions and classes (#6206)
## Summary
This PR ensures that if a function or class is the first statement in a
nested suite that _isn't_ a function or class body, we insert a leading
newline.
For example, given:
```python
def f():
if True:
def register_type():
pass
```
We _want_ to preserve the newline, whereas today, we remove it.
Note that this only applies when the function or class doesn't have any
leading comments.
Closes https://github.com/astral-sh/ruff/issues/6066.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::statement::suite::SuiteLevel;
|
||||
use crate::statement::suite::SuiteKind;
|
||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::prelude::hard_line_break;
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
@@ -13,7 +13,7 @@ impl FormatNodeRule<ModModule> for FormatModModule {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
body.format().with_options(SuiteLevel::TopLevel),
|
||||
body.format().with_options(SuiteKind::TopLevel),
|
||||
// Trailing newline at the end of the file
|
||||
hard_line_break()
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user