Skip to content

Commit

Permalink
Preview Style: Format module level docstring (#9725)
Browse files Browse the repository at this point in the history
Co-authored-by: Micha Reiser <micha@reiser.io>
  • Loading branch information
Glyphack and MichaReiser committed Feb 5, 2024
1 parent 80fc02e commit b47f85e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'single'
'single' # this string is treated as a docstring
"double"
r'r single'
r"r double"
Expand Down
7 changes: 7 additions & 0 deletions crates/ruff_python_formatter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,10 @@ pub(crate) const fn is_hex_codes_in_unicode_sequences_enabled(context: &PyFormat
pub(crate) const fn is_multiline_string_handling_enabled(context: &PyFormatContext) -> bool {
context.is_preview()
}

/// 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
/// preview for compatibility with Black.
pub(crate) const fn is_format_module_docstring_enabled(context: &PyFormatContext) -> bool {
context.is_preview()
}
15 changes: 13 additions & 2 deletions crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use crate::expression::expr_string_literal::ExprStringLiteralKind;
use crate::prelude::*;
use crate::preview::{
is_blank_line_after_nested_stub_class_enabled, is_dummy_implementations_enabled,
is_module_docstring_newlines_enabled, is_no_blank_line_before_class_docstring_enabled,
is_format_module_docstring_enabled, is_module_docstring_newlines_enabled,
is_no_blank_line_before_class_docstring_enabled,
};
use crate::statement::stmt_expr::FormatStmtExpr;
use crate::verbatim::{
Expand Down Expand Up @@ -140,7 +141,17 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
SuiteChildStatement::Other(first)
}
}
SuiteKind::TopLevel => SuiteChildStatement::Other(first),
SuiteKind::TopLevel => {
if is_format_module_docstring_enabled(f.context()) {
if let Some(docstring) = DocstringStmt::try_from_statement(first, self.kind) {
SuiteChildStatement::Docstring(docstring)
} else {
SuiteChildStatement::Other(first)
}
} else {
SuiteChildStatement::Other(first)
}
}
};

let first_comments = comments.leading_dangling_trailing(first);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/quote_styl
---
## Input
```python
'single'
'single' # this string is treated as a docstring
"double"
r'r single'
r"r double"
Expand Down Expand Up @@ -73,7 +73,7 @@ source_type = Python
```

```python
'single'
'single' # this string is treated as a docstring
'double'
r'r single'
r'r double'
Expand Down Expand Up @@ -135,7 +135,8 @@ def docstring_single():
--- Stable
+++ Preview
@@ -1,4 +1,5 @@
'single'
-'single' # this string is treated as a docstring
+"single" # this string is treated as a docstring
+
'double'
r'r single'
Expand All @@ -159,7 +160,7 @@ source_type = Python
```

```python
"single"
"single" # this string is treated as a docstring
"double"
r"r single"
r"r double"
Expand Down Expand Up @@ -221,7 +222,7 @@ def docstring_single():
--- Stable
+++ Preview
@@ -1,4 +1,5 @@
"single"
"single" # this string is treated as a docstring
+
"double"
r"r single"
Expand All @@ -245,7 +246,7 @@ source_type = Python
```

```python
'single'
'single' # this string is treated as a docstring
"double"
r'r single'
r"r double"
Expand Down Expand Up @@ -307,7 +308,8 @@ def docstring_single():
--- Stable
+++ Preview
@@ -1,4 +1,5 @@
'single'
-'single' # this string is treated as a docstring
+"single" # this string is treated as a docstring
+
"double"
r'r single'
Expand Down

0 comments on commit b47f85e

Please sign in to comment.