mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 04:55:09 +00:00
Allow up to two empty lines after top-level imports (#6777)
## Summary For imports, we enforce that there's _at least_ one empty line after an import (assuming the next statement is _not_ an import), but allow up to two at the module level. Closes https://github.com/astral-sh/ruff/issues/6760. ## Test Plan `cargo test`
This commit is contained in:
parent
558b56f8a8
commit
cc278c24e2
3 changed files with 176 additions and 1 deletions
|
@ -192,7 +192,19 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
|||
}
|
||||
}
|
||||
} else if is_import_definition(preceding) && !is_import_definition(following) {
|
||||
empty_line().fmt(f)?;
|
||||
// Enforce _at least_ one empty line after an import statement (but allow up to
|
||||
// two at the top-level).
|
||||
match self.kind {
|
||||
SuiteKind::TopLevel => {
|
||||
match lines_after_ignoring_trivia(preceding.end(), source) {
|
||||
0 | 1 | 2 => empty_line().fmt(f)?,
|
||||
_ => write!(f, [empty_line(), empty_line()])?,
|
||||
}
|
||||
}
|
||||
SuiteKind::Function | SuiteKind::Class | SuiteKind::Other => {
|
||||
empty_line().fmt(f)?;
|
||||
}
|
||||
}
|
||||
} else if is_compound_statement(preceding) {
|
||||
// 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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue