Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve multiline implicit concatenated strings in docstring positions #15126

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,20 @@
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"
)
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
)
```


Expand Down Expand Up @@ -495,7 +528,7 @@ r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"
...


@@ -193,16 +205,8 @@
@@ -193,24 +205,19 @@

r"aaaaaaaaa" r"bbbbbbbbbbbbbbbbbbbb"

Expand All @@ -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():
```


Expand Down Expand Up @@ -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'
)
```


Expand Down Expand Up @@ -807,7 +868,7 @@ r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb'
...


@@ -193,16 +205,8 @@
@@ -193,24 +205,19 @@

r'aaaaaaaaa' r'bbbbbbbbbbbbbbbbbbbb'

Expand All @@ -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