mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
Preserve multiline implicit concatenated strings in docstring positions (#15126)
Some checks are pending
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
Some checks are pending
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
This commit is contained in:
parent
6180f78da4
commit
1218bc65ed
3 changed files with 116 additions and 6 deletions
|
@ -195,3 +195,20 @@ r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
|
|||
f"""aaaa{
|
||||
10}aaaaa""" fr"""bbbbbbbbbbbbbbbbbbbb"""
|
||||
)
|
||||
|
||||
# In docstring positions
|
||||
def docstring():
|
||||
(
|
||||
r"aaaaaaaaa"
|
||||
"bbbbbbbbbbbbbbbbbbbb"
|
||||
)
|
||||
|
||||
def docstring_flat():
|
||||
(
|
||||
r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
|
||||
)
|
||||
|
||||
def docstring_flat_overlong():
|
||||
(
|
||||
r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
)
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
use ruff_formatter::FormatRuleWithOptions;
|
||||
use ruff_python_ast::{AnyNodeRef, ExprStringLiteral, StringLike};
|
||||
|
||||
use crate::builders::parenthesize_if_expands;
|
||||
use crate::expression::parentheses::{
|
||||
in_parentheses_only_group, NeedsParentheses, OptionalParentheses,
|
||||
};
|
||||
use crate::other::string_literal::StringLiteralKind;
|
||||
use crate::prelude::*;
|
||||
use crate::string::implicit::FormatImplicitConcatenatedStringFlat;
|
||||
use crate::string::implicit::{
|
||||
FormatImplicitConcatenatedStringExpanded, FormatImplicitConcatenatedStringFlat,
|
||||
ImplicitConcatenatedLayout,
|
||||
};
|
||||
use crate::string::{implicit::FormatImplicitConcatenatedString, StringLikeExtensions};
|
||||
use ruff_formatter::FormatRuleWithOptions;
|
||||
use ruff_python_ast::{AnyNodeRef, ExprStringLiteral, StringLike};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprStringLiteral {
|
||||
|
@ -38,6 +41,23 @@ impl FormatNodeRule<ExprStringLiteral> for FormatExprStringLiteral {
|
|||
format_flat.set_docstring(self.kind.is_docstring());
|
||||
return format_flat.fmt(f);
|
||||
}
|
||||
|
||||
// ```py
|
||||
// def test():
|
||||
// (
|
||||
// r"a"
|
||||
// "b"
|
||||
// )
|
||||
// ```
|
||||
if self.kind.is_docstring() {
|
||||
return parenthesize_if_expands(
|
||||
&FormatImplicitConcatenatedStringExpanded::new(
|
||||
item.into(),
|
||||
ImplicitConcatenatedLayout::Multipart,
|
||||
),
|
||||
)
|
||||
.fmt(f);
|
||||
}
|
||||
}
|
||||
|
||||
in_parentheses_only_group(&FormatImplicitConcatenatedString::new(item)).fmt(f)
|
||||
|
|
|
@ -202,6 +202,23 @@ r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
|
|||
f"""aaaa{
|
||||
10}aaaaa""" fr"""bbbbbbbbbbbbbbbbbbbb"""
|
||||
)
|
||||
|
||||
# In docstring positions
|
||||
def docstring():
|
||||
(
|
||||
r"aaaaaaaaa"
|
||||
"bbbbbbbbbbbbbbbbbbbb"
|
||||
)
|
||||
|
||||
def docstring_flat():
|
||||
(
|
||||
r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
|
||||
)
|
||||
|
||||
def docstring_flat_overlong():
|
||||
(
|
||||
r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
)
|
||||
```
|
||||
|
||||
## Outputs
|
||||
|
@ -429,6 +446,22 @@ r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
|
|||
10}aaaaa"""
|
||||
rf"""bbbbbbbbbbbbbbbbbbbb"""
|
||||
)
|
||||
|
||||
|
||||
# In docstring positions
|
||||
def docstring():
|
||||
r"aaaaaaaaa" "bbbbbbbbbbbbbbbbbbbb"
|
||||
|
||||
|
||||
def docstring_flat():
|
||||
r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
|
||||
|
||||
|
||||
def docstring_flat_overlong():
|
||||
(
|
||||
r"aaaaaaaaa"
|
||||
r"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
|
@ -495,7 +528,7 @@ r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
|
|||
...
|
||||
|
||||
|
||||
@@ -193,16 +205,8 @@
|
||||
@@ -193,24 +205,19 @@
|
||||
|
||||
r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
|
||||
|
||||
|
@ -514,6 +547,18 @@ r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
|
|||
- rf"""bbbbbbbbbbbbbbbbbbbb"""
|
||||
-)
|
||||
+(f"""aaaa{10}aaaaa""" rf"""bbbbbbbbbbbbbbbbbbbb""")
|
||||
|
||||
|
||||
# In docstring positions
|
||||
def docstring():
|
||||
- r"aaaaaaaaa" "bbbbbbbbbbbbbbbbbbbb"
|
||||
+ (
|
||||
+ r"aaaaaaaaa"
|
||||
+ "bbbbbbbbbbbbbbbbbbbb"
|
||||
+ )
|
||||
|
||||
|
||||
def docstring_flat():
|
||||
```
|
||||
|
||||
|
||||
|
@ -741,6 +786,22 @@ r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb'
|
|||
10}aaaaa"""
|
||||
rf"""bbbbbbbbbbbbbbbbbbbb"""
|
||||
)
|
||||
|
||||
|
||||
# In docstring positions
|
||||
def docstring():
|
||||
r'aaaaaaaaa' 'bbbbbbbbbbbbbbbbbbbb'
|
||||
|
||||
|
||||
def docstring_flat():
|
||||
r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb'
|
||||
|
||||
|
||||
def docstring_flat_overlong():
|
||||
(
|
||||
r'aaaaaaaaa'
|
||||
r'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
|
@ -807,7 +868,7 @@ r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb'
|
|||
...
|
||||
|
||||
|
||||
@@ -193,16 +205,8 @@
|
||||
@@ -193,24 +205,19 @@
|
||||
|
||||
r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb'
|
||||
|
||||
|
@ -826,4 +887,16 @@ r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb'
|
|||
- rf"""bbbbbbbbbbbbbbbbbbbb"""
|
||||
-)
|
||||
+(f"""aaaa{10}aaaaa""" rf"""bbbbbbbbbbbbbbbbbbbb""")
|
||||
|
||||
|
||||
# In docstring positions
|
||||
def docstring():
|
||||
- r'aaaaaaaaa' 'bbbbbbbbbbbbbbbbbbbb'
|
||||
+ (
|
||||
+ r'aaaaaaaaa'
|
||||
+ 'bbbbbbbbbbbbbbbbbbbb'
|
||||
+ )
|
||||
|
||||
|
||||
def docstring_flat():
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue