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:
Charlie Marsh 2023-08-01 11:30:59 -04:00 committed by GitHub
parent b68f76f0d9
commit 928ab63a64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 43 deletions

View file

@ -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()
]