Skip to content

Commit

Permalink
Keep trailing new line in module docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Feb 1, 2024
1 parent 0e8d1b2 commit c63bc47
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 157 deletions.
6 changes: 5 additions & 1 deletion crates/ruff_python_formatter/src/other/string_literal.rs
Expand Up @@ -66,7 +66,11 @@ impl Format<PyFormatContext<'_>> for FormatStringLiteral<'_> {
);

if self.layout.is_docstring() {
docstring::format(&normalized, f)
let is_module = matches!(
f.context().node_level(),
crate::context::NodeLevel::TopLevel(_)
);
docstring::format(&normalized, f, is_module)
} else {
normalized.fmt(f)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/preview.rs
Expand Up @@ -76,7 +76,7 @@ pub(crate) const fn is_multiline_string_handling_enabled(context: &PyFormatConte
}

/// Returns `true` if the [`multiline_string_handling`](https://github.com/astral-sh/ruff/pull/9725) preview style is enabled.
/// Black does not format docstrings https://github.com/psf/black/issues/3493 so we keep this
/// Black does not [`format docstrings`](https://github.com/psf/black/issues/3493) so we keep this
/// preview for compatibility with Black.
pub(crate) const fn is_format_module_docstring_enabled(context: &PyFormatContext) -> bool {
context.is_preview()
Expand Down
10 changes: 9 additions & 1 deletion crates/ruff_python_formatter/src/string/docstring.rs
Expand Up @@ -102,7 +102,11 @@ use super::{NormalizedString, QuoteChar};
/// line c
/// """
/// ```
pub(crate) fn format(normalized: &NormalizedString, f: &mut PyFormatter) -> FormatResult<()> {
pub(crate) fn format(
normalized: &NormalizedString,
f: &mut PyFormatter,
is_module: bool,
) -> FormatResult<()> {
let docstring = &normalized.text;

// Black doesn't change the indentation of docstrings that contain an escaped newline
Expand Down Expand Up @@ -205,6 +209,10 @@ pub(crate) fn format(normalized: &NormalizedString, f: &mut PyFormatter) -> Form
space().fmt(f)?;
}

if is_module && trim_end.ends_with('\n') {
hard_line_break().fmt(f)?;
}

write!(f, [source_position(normalized.end()), normalized.quotes])
}

Expand Down

This file was deleted.

Expand Up @@ -189,13 +189,10 @@ def f():
```diff
--- Stable
+++ Preview
@@ -13,15 +13,14 @@
def callback(event ):
print('Python:Click')
@@ -15,13 +15,13 @@
- button.on_event(ButtonClick, callback)
-"""
+ button.on_event(ButtonClick, callback)"""
button.on_event(ButtonClick, callback)
"""
+
first_stmt_after_module_level_docstring = 1
Expand All @@ -208,7 +205,7 @@ def f():
def raw_docstring():
@@ -41,23 +40,22 @@
@@ -41,23 +41,22 @@
class RemoveNewlineBeforeClassDocstring:
Expand Down Expand Up @@ -241,7 +238,7 @@ def f():
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
cccccccc.ccccccccccccc(d).cccccccc + e
@@ -71,9 +69,9 @@
@@ -71,9 +70,9 @@
+ eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
)
Expand Down Expand Up @@ -291,7 +288,8 @@ A code black to format
def callback(event ):
print('Python:Click')
button.on_event(ButtonClick, callback)"""
button.on_event(ButtonClick, callback)
"""
first_stmt_after_module_level_docstring = 1
Expand Down
Expand Up @@ -313,24 +313,15 @@ class ComplexStatements:
```diff
--- Stable
+++ Preview
@@ -1,7 +1,6 @@
"""Tests for empty line rules in stub files, mostly inspired by typeshed.
The rules are a list of nested exceptions. See also
-https://github.com/psf/black/blob/c160e4b7ce30c661ac4f2dfa5038becf1b8c5c33/src/black/lines.py#L576-L744
-"""
+https://github.com/psf/black/blob/c160e4b7ce30c661ac4f2dfa5038becf1b8c5c33/src/black/lines.py#L576-L744"""
import sys
from typing import Self, TypeAlias, final
@@ -110,6 +109,7 @@
@@ -110,6 +110,7 @@
class InnerClass5:
def a(self): ...
+
field1 = 1
class InnerClass6:
@@ -119,6 +119,7 @@
@@ -119,6 +120,7 @@
class InnerClass7:
def a(self): ...
Expand Down

0 comments on commit c63bc47

Please sign in to comment.