Skip to content

Commit

Permalink
Implement no_blank_line_before_class_docstring preview style
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Dec 15, 2023
1 parent 189e947 commit e95adb9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 148 deletions.
9 changes: 9 additions & 0 deletions crates/ruff_python_formatter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ pub(crate) const fn is_prefer_splitting_right_hand_side_of_assignments_enabled(
) -> bool {
context.is_preview()
}

/// Returns `true` if the [`no_blank_line_before_class_docstring`] preview style is enabled.
///
/// [`no_blank_line_before_class_docstring`]: https://github.com/astral-sh/ruff/issues/8888
pub(crate) const fn is_no_blank_line_before_class_docstring_enabled(
context: &PyFormatContext,
) -> bool {
context.is_preview()
}
11 changes: 11 additions & 0 deletions crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::comments::{
use crate::context::{NodeLevel, TopLevelStatementPosition, WithIndentLevel, WithNodeLevel};
use crate::expression::expr_string_literal::ExprStringLiteralKind;
use crate::prelude::*;
use crate::preview::is_no_blank_line_before_class_docstring_enabled;
use crate::statement::stmt_expr::FormatStmtExpr;
use crate::verbatim::{
suppressed_node, write_suppressed_statements_starting_with_leading_comment,
Expand Down Expand Up @@ -108,14 +109,24 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
if !comments.has_leading(first)
&& lines_before(first.start(), source) > 1
&& !source_type.is_stub()
&& !is_no_blank_line_before_class_docstring_enabled(f.context())
{
// Allow up to one empty line before a class docstring, e.g., this is
// stable formatting:
//
// ```python
// class Test:
//
// """Docstring"""
// ```
//
// But, in preview mode, we don't want to allow any empty lines before a
// class docstring, e.g., this is preview formatting:
//
// ```python
// class Test:
// """Docstring"""
// ```
empty_line().fmt(f)?;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,21 @@ class UpperCaseR:
```diff
--- Black
+++ Ruff
@@ -1,4 +1,5 @@
class C:
+
r"""Raw"""
@@ -7,8 +8,9 @@
@@ -7,7 +7,7 @@
class SingleQuotes:
- r'''Raw'''
+ r"""Raw"""
+
class UpperCaseR:
R"""Raw"""
```

## Ruff Output

```python
class C:
r"""Raw"""
Expand All @@ -59,7 +50,6 @@ def f():
class SingleQuotes:
r"""Raw"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ def reference_docstring_newlines():
class RemoveNewlineBeforeClassDocstring:
"""Black's `Preview.no_blank_line_before_class_docstring`"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,30 @@ class QuerySet(AltersData):
class Test(
@@ -159,20 +158,17 @@
@@ -94,7 +93,6 @@
class Test:
-
"""Docstring"""
@@ -111,14 +109,12 @@
class Test:
-
"""Docstring"""
x = 1
class Test:
-
"""Docstring"""
# comment
@@ -159,20 +155,17 @@
@dataclass
# Copied from transformers.models.clip.modeling_clip.CLIPOutput with CLIP->AltCLIP
Expand Down

0 comments on commit e95adb9

Please sign in to comment.