Add empty line after import block (#6200)

## Summary

Ensures that, given:

```python
import os
x = 1
```

We format like:

```python
import os

x = 1
```
This commit is contained in:
Charlie Marsh 2023-07-31 12:01:45 -04:00 committed by GitHub
parent cb34e6d322
commit 7eb2ba47cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 19 deletions

View file

@ -58,17 +58,13 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
return Ok(());
};
// First entry has never any separator, doesn't matter which one we take;
// First entry has never any separator, doesn't matter which one we take.
joiner.entry(first, &first.format());
let mut last = first;
let mut is_last_function_or_class_definition = is_class_or_function_definition(first);
for statement in iter {
let is_current_function_or_class_definition =
is_class_or_function_definition(statement);
if is_last_function_or_class_definition || is_current_function_or_class_definition {
if is_class_or_function_definition(last) || is_class_or_function_definition(statement) {
match self.level {
SuiteLevel::TopLevel => {
joiner.entry_with_separator(
@ -81,6 +77,8 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
joiner.entry_with_separator(&empty_line(), &statement.format(), statement);
}
}
} else if is_import_definition(last) && !is_import_definition(statement) {
joiner.entry_with_separator(&empty_line(), &statement.format(), statement);
} else if is_compound_statement(last) {
// Handles the case where a body has trailing comments. The issue is that RustPython does not include
// the comments in the range of the suite. This means, the body ends right after the last statement in the body.
@ -124,7 +122,6 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
joiner.entry(statement, &statement.format());
}
is_last_function_or_class_definition = is_current_function_or_class_definition;
last = statement;
}
@ -143,6 +140,10 @@ const fn is_class_or_function_definition(stmt: &Stmt) -> bool {
)
}
const fn is_import_definition(stmt: &Stmt) -> bool {
matches!(stmt, Stmt::Import(_) | Stmt::ImportFrom(_))
}
impl FormatRuleWithOptions<Suite, PyFormatContext<'_>> for FormatSuite {
type Options = SuiteLevel;