Skip to content

Commit

Permalink
Format empty lines in stub files closer to black's preview style
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Sep 8, 2023
1 parent 22bb6ce commit 52a7ff9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Tests for empty line rules in stub files, mostly inspired by typeshed.
The rules follow no discernible pattern. See also
The rules are a list of nested exceptions. See also
https://github.com/psf/black/blob/c160e4b7ce30c661ac4f2dfa5038becf1b8c5c33/src/black/lines.py#L576-L744
"""

Expand Down
32 changes: 12 additions & 20 deletions crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,6 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
///
/// These rules are ported from black (preview mode at time of writing) using the stubs test case:
/// <https://github.com/psf/black/blob/c160e4b7ce30c661ac4f2dfa5038becf1b8c5c33/src/black/lines.py#L576-L744>
/// The reasons for these rules here and in [`stub_suite_empty_line`] are not commented as i did
/// not follow them
fn stub_file_empty_lines(
kind: SuiteKind,
preceding: &Stmt,
Expand All @@ -342,25 +340,20 @@ fn stub_file_empty_lines(
f: &mut PyFormatter,
) -> FormatResult<()> {
let source = f.context().source();

// Preserve the empty line if the definitions are separated by a comment
let empty_line_condition = preceding_comments.has_trailing()
|| following_comments.has_leading()
|| !stub_suite_can_omit_empty_line(preceding, following, f);
match kind {
SuiteKind::TopLevel => {
// Preserve the empty line if the definitions are separated by a comment
if preceding_comments.has_trailing()
|| following_comments.has_leading()
|| stub_suite_empty_line(preceding, following, f)
{
if empty_line_condition {
empty_line().fmt(f)
} else {
hard_line_break().fmt(f)
}
}
SuiteKind::Class | SuiteKind::Other | SuiteKind::Function => {
if (preceding_comments.has_trailing()
|| following_comments.has_leading()
|| stub_suite_empty_line(preceding, following, f))
&& lines_after_ignoring_trivia(preceding.end(), source) > 1
{
if empty_line_condition && lines_after_ignoring_trivia(preceding.end(), source) > 1 {
empty_line().fmt(f)
} else {
hard_line_break().fmt(f)
Expand All @@ -369,9 +362,9 @@ fn stub_file_empty_lines(
}
}

/// Add an empty line except when one of the three reason to skip them is given
fn stub_suite_empty_line(preceding: &Stmt, following: &Stmt, f: &mut PyFormatter) -> bool {
// Two subsequent classes that both have an ellipsis only body
/// Only a function to compute it lazily
fn stub_suite_can_omit_empty_line(preceding: &Stmt, following: &Stmt, f: &mut PyFormatter) -> bool {
// Two subsequent class definitions that both have an ellipsis only body
// ```python
// class A: ...
// class B: ...
Expand Down Expand Up @@ -402,7 +395,7 @@ fn stub_suite_empty_line(preceding: &Stmt, following: &Stmt, f: &mut PyFormatter
.as_class_def_stmt()
.is_some_and(|class| !class.decorator_list.is_empty());

// Two subsequent functions where at least the preceding has an ellipsis only body
// A function definition following a stub function definition
// ```python
// def test(): ...
// def b(): a
Expand All @@ -412,10 +405,9 @@ fn stub_suite_empty_line(preceding: &Stmt, following: &Stmt, f: &mut PyFormatter
.is_some_and(|function| contains_only_an_ellipsis(&function.body, f.context().comments()))
&& following.is_function_def_stmt();

// Add an empty line except when one of the three reason to skip them is given
!(class_sequences_with_ellipsis_only
class_sequences_with_ellipsis_only
|| class_decorator_instead_of_empty_line
|| function_with_ellipsis)
|| function_with_ellipsis
}

/// Returns `true` if a function or class body contains only an ellipsis with no comments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/stub_files
## Input
```py
"""Tests for empty line rules in stub files, mostly inspired by typeshed.
The rules follow no discernible pattern. See also
The rules are a list of nested exceptions. See also
https://github.com/psf/black/blob/c160e4b7ce30c661ac4f2dfa5038becf1b8c5c33/src/black/lines.py#L576-L744
"""
Expand Down Expand Up @@ -158,7 +158,7 @@ class ComplexStatements:
## Output
```py
"""Tests for empty line rules in stub files, mostly inspired by typeshed.
The rules follow no discernible pattern. See also
The rules are a list of nested exceptions. See also
https://github.com/psf/black/blob/c160e4b7ce30c661ac4f2dfa5038becf1b8c5c33/src/black/lines.py#L576-L744
"""
Expand Down

0 comments on commit 52a7ff9

Please sign in to comment.