Skip to content

Commit

Permalink
Mark format docstring as preview style
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Feb 1, 2024
1 parent bef24c2 commit 2cbafd2
Show file tree
Hide file tree
Showing 8 changed files with 1,419 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@
"docstring_code_line_width": "dynamic",
"indent_style": "space",
"indent_width": 4
},
{
"docstring_code": "enabled",
"preview": "enabled",
"indent_style": "space",
"indent_width": 4
}
]
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()
}
11 changes: 8 additions & 3 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 @@ -141,8 +142,12 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
}
}
SuiteKind::TopLevel => {
if let Some(docstring) = DocstringStmt::try_from_statement(first, self.kind) {
SuiteChildStatement::Docstring(docstring)
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,24 @@ b = 2
```diff
--- Black
+++ Ruff
@@ -1,6 +1,6 @@
"""I am a very helpful module docstring.
@@ -9,8 +9,7 @@
Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident,
-sunt in culpa qui officia deserunt mollit anim id est laborum.
-"""
+sunt in culpa qui officia deserunt mollit anim id est laborum."""
a = 1
-With trailing spaces:
+With trailing spaces:
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam,
```

## Ruff Output

```python
"""I am a very helpful module docstring.
With trailing spaces:
With trailing spaces:
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam,
Expand All @@ -72,8 +74,7 @@ nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
"""
sunt in culpa qui officia deserunt mollit anim id est laborum."""
a = 1
Expand Down
Loading

0 comments on commit 2cbafd2

Please sign in to comment.