mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 19:34:23 +00:00
Insert empty line between suite and alternative branch after def/class (#12294)
When there is a function or class definition at the end of a suite followed by the beginning of an alternative block, we have to insert a single empty line between them. In the if-else-statement example below, we insert an empty line after the `foo` in the if-block, but none after the else-block `foo`, since in the latter case the enclosing suite already adds empty lines. ```python if sys.version_info >= (3, 10): def foo(): return "new" else: def foo(): return "old" class Bar: pass ``` To do so, we track whether the current suite is the last one in the current statement with a new option on the suite kind. Fixes #12199 --------- Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
ecd4b4d943
commit
9a817a2922
25 changed files with 699 additions and 149 deletions
|
@ -380,21 +380,14 @@ pub(crate) struct FormatClauseBody<'a> {
|
|||
trailing_comments: &'a [SourceComment],
|
||||
}
|
||||
|
||||
impl<'a> FormatClauseBody<'a> {
|
||||
#[must_use]
|
||||
pub(crate) fn with_kind(mut self, kind: SuiteKind) -> Self {
|
||||
self.kind = kind;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn clause_body<'a>(
|
||||
body: &'a Suite,
|
||||
kind: SuiteKind,
|
||||
trailing_comments: &'a [SourceComment],
|
||||
) -> FormatClauseBody<'a> {
|
||||
FormatClauseBody {
|
||||
body,
|
||||
kind: SuiteKind::default(),
|
||||
kind,
|
||||
trailing_comments,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue