mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-01 12:25:10 +00:00
Hug multiline-strings preview style (#9243)
This commit is contained in:
parent
6be73322da
commit
ac02d3aedd
16 changed files with 590 additions and 215 deletions
|
|
@ -187,7 +187,7 @@ this_will_also_become_one_line = ( # comment
|
|||
```diff
|
||||
--- Black
|
||||
+++ Ruff
|
||||
@@ -1,95 +1,138 @@
|
||||
@@ -1,46 +1,69 @@
|
||||
-"""cow
|
||||
+(
|
||||
+ """cow
|
||||
|
|
@ -271,41 +271,21 @@ this_will_also_become_one_line = ( # comment
|
|||
+ ),
|
||||
)
|
||||
textwrap.dedent("""A one-line triple-quoted string.""")
|
||||
-textwrap.dedent("""A two-line triple-quoted string
|
||||
-since it goes to the next line.""")
|
||||
-textwrap.dedent("""A three-line triple-quoted string
|
||||
+textwrap.dedent(
|
||||
+ """A two-line triple-quoted string
|
||||
+since it goes to the next line."""
|
||||
+)
|
||||
+textwrap.dedent(
|
||||
+ """A three-line triple-quoted string
|
||||
that not only goes to the next line
|
||||
-but also goes one line beyond.""")
|
||||
-textwrap.dedent("""\
|
||||
+but also goes one line beyond."""
|
||||
+)
|
||||
+textwrap.dedent(
|
||||
+ """\
|
||||
A triple-quoted string
|
||||
actually leveraging the textwrap.dedent functionality
|
||||
textwrap.dedent("""A two-line triple-quoted string
|
||||
@@ -54,18 +77,24 @@
|
||||
that ends in a trailing newline,
|
||||
representing e.g. file contents.
|
||||
-""")
|
||||
""")
|
||||
-path.write_text(textwrap.dedent("""\
|
||||
+"""
|
||||
+)
|
||||
+path.write_text(
|
||||
+ textwrap.dedent(
|
||||
+ """\
|
||||
+ textwrap.dedent("""\
|
||||
A triple-quoted string
|
||||
actually leveraging the textwrap.dedent functionality
|
||||
that ends in a trailing newline,
|
||||
representing e.g. file contents.
|
||||
-"""))
|
||||
-path.write_text(textwrap.dedent("""\
|
||||
+"""
|
||||
+ )
|
||||
+""")
|
||||
+)
|
||||
+path.write_text(
|
||||
+ textwrap.dedent(
|
||||
|
|
@ -319,29 +299,9 @@ this_will_also_become_one_line = ( # comment
|
|||
+ )
|
||||
+)
|
||||
# Another use case
|
||||
-data = yaml.load("""\
|
||||
+data = yaml.load(
|
||||
+ """\
|
||||
data = yaml.load("""\
|
||||
a: 1
|
||||
b: 2
|
||||
-""")
|
||||
+"""
|
||||
+)
|
||||
data = yaml.load(
|
||||
"""\
|
||||
a: 1
|
||||
b: 2
|
||||
""",
|
||||
)
|
||||
-data = yaml.load("""\
|
||||
+data = yaml.load(
|
||||
+ """\
|
||||
a: 1
|
||||
b: 2
|
||||
-""")
|
||||
+"""
|
||||
+)
|
||||
|
||||
@@ -85,11 +114,13 @@
|
||||
MULTILINE = """
|
||||
foo
|
||||
""".replace("\n", "")
|
||||
|
|
@ -356,7 +316,7 @@ this_will_also_become_one_line = ( # comment
|
|||
parser.usage += """
|
||||
Custom extra help summary.
|
||||
|
||||
@@ -156,16 +199,24 @@
|
||||
@@ -156,16 +187,24 @@
|
||||
10 LOAD_CONST 0 (None)
|
||||
12 RETURN_VALUE
|
||||
""" % (_C.__init__.__code__.co_firstlineno + 1,)
|
||||
|
|
@ -387,36 +347,7 @@ this_will_also_become_one_line = ( # comment
|
|||
[
|
||||
"""cow
|
||||
moos""",
|
||||
@@ -177,28 +228,32 @@
|
||||
|
||||
|
||||
def dastardly_default_value(
|
||||
- cow: String = json.loads("""this
|
||||
+ cow: String = json.loads(
|
||||
+ """this
|
||||
is
|
||||
quite
|
||||
the
|
||||
dastadardly
|
||||
-value!"""),
|
||||
+value!"""
|
||||
+ ),
|
||||
**kwargs,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
-print(f"""
|
||||
+print(
|
||||
+ f"""
|
||||
This {animal}
|
||||
moos and barks
|
||||
{animal} say
|
||||
-""")
|
||||
+"""
|
||||
+)
|
||||
msg = f"""The arguments {bad_arguments} were passed in.
|
||||
Please use `--build-option` instead,
|
||||
@@ -198,7 +237,7 @@
|
||||
`--global-option` is reserved to flags like `--verbose` or `--quiet`.
|
||||
"""
|
||||
|
||||
|
|
@ -425,7 +356,7 @@ this_will_also_become_one_line = ( # comment
|
|||
|
||||
this_will_stay_on_three_lines = (
|
||||
"a" # comment
|
||||
@@ -206,4 +261,6 @@
|
||||
@@ -206,4 +245,6 @@
|
||||
"c"
|
||||
)
|
||||
|
||||
|
|
@ -506,32 +437,24 @@ call(
|
|||
),
|
||||
)
|
||||
textwrap.dedent("""A one-line triple-quoted string.""")
|
||||
textwrap.dedent(
|
||||
"""A two-line triple-quoted string
|
||||
since it goes to the next line."""
|
||||
)
|
||||
textwrap.dedent(
|
||||
"""A three-line triple-quoted string
|
||||
textwrap.dedent("""A two-line triple-quoted string
|
||||
since it goes to the next line.""")
|
||||
textwrap.dedent("""A three-line triple-quoted string
|
||||
that not only goes to the next line
|
||||
but also goes one line beyond."""
|
||||
)
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
but also goes one line beyond.""")
|
||||
textwrap.dedent("""\
|
||||
A triple-quoted string
|
||||
actually leveraging the textwrap.dedent functionality
|
||||
that ends in a trailing newline,
|
||||
representing e.g. file contents.
|
||||
"""
|
||||
)
|
||||
""")
|
||||
path.write_text(
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
textwrap.dedent("""\
|
||||
A triple-quoted string
|
||||
actually leveraging the textwrap.dedent functionality
|
||||
that ends in a trailing newline,
|
||||
representing e.g. file contents.
|
||||
"""
|
||||
)
|
||||
""")
|
||||
)
|
||||
path.write_text(
|
||||
textwrap.dedent(
|
||||
|
|
@ -544,24 +467,20 @@ path.write_text(
|
|||
)
|
||||
)
|
||||
# Another use case
|
||||
data = yaml.load(
|
||||
"""\
|
||||
data = yaml.load("""\
|
||||
a: 1
|
||||
b: 2
|
||||
"""
|
||||
)
|
||||
""")
|
||||
data = yaml.load(
|
||||
"""\
|
||||
a: 1
|
||||
b: 2
|
||||
""",
|
||||
)
|
||||
data = yaml.load(
|
||||
"""\
|
||||
data = yaml.load("""\
|
||||
a: 1
|
||||
b: 2
|
||||
"""
|
||||
)
|
||||
""")
|
||||
|
||||
MULTILINE = """
|
||||
foo
|
||||
|
|
@ -668,26 +587,22 @@ barks""",
|
|||
|
||||
|
||||
def dastardly_default_value(
|
||||
cow: String = json.loads(
|
||||
"""this
|
||||
cow: String = json.loads("""this
|
||||
is
|
||||
quite
|
||||
the
|
||||
dastadardly
|
||||
value!"""
|
||||
),
|
||||
value!"""),
|
||||
**kwargs,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
print(
|
||||
f"""
|
||||
print(f"""
|
||||
This {animal}
|
||||
moos and barks
|
||||
{animal} say
|
||||
"""
|
||||
)
|
||||
""")
|
||||
msg = f"""The arguments {bad_arguments} were passed in.
|
||||
Please use `--build-option` instead,
|
||||
`--global-option` is reserved to flags like `--verbose` or `--quiet`.
|
||||
|
|
|
|||
|
|
@ -8209,6 +8209,42 @@ def markdown_skipped_rst_directive():
|
|||
```
|
||||
|
||||
|
||||
#### Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -480,10 +480,8 @@
|
||||
Do cool stuff::
|
||||
|
||||
if True:
|
||||
- cool_stuff(
|
||||
- '''
|
||||
- hiya'''
|
||||
- )
|
||||
+ cool_stuff('''
|
||||
+ hiya''')
|
||||
|
||||
Done.
|
||||
"""
|
||||
@@ -958,13 +956,11 @@
|
||||
Do cool stuff.
|
||||
|
||||
``````
|
||||
- do_something(
|
||||
- '''
|
||||
+ do_something('''
|
||||
```
|
||||
did i trick you?
|
||||
```
|
||||
- '''
|
||||
- )
|
||||
+ ''')
|
||||
``````
|
||||
|
||||
Done.
|
||||
```
|
||||
|
||||
|
||||
### Output 6
|
||||
```
|
||||
indent-style = space
|
||||
|
|
@ -9577,6 +9613,42 @@ def markdown_skipped_rst_directive():
|
|||
```
|
||||
|
||||
|
||||
#### Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -480,10 +480,8 @@
|
||||
Do cool stuff::
|
||||
|
||||
if True:
|
||||
- cool_stuff(
|
||||
- '''
|
||||
- hiya'''
|
||||
- )
|
||||
+ cool_stuff('''
|
||||
+ hiya''')
|
||||
|
||||
Done.
|
||||
"""
|
||||
@@ -958,13 +956,11 @@
|
||||
Do cool stuff.
|
||||
|
||||
``````
|
||||
- do_something(
|
||||
- '''
|
||||
+ do_something('''
|
||||
```
|
||||
did i trick you?
|
||||
```
|
||||
- '''
|
||||
- )
|
||||
+ ''')
|
||||
``````
|
||||
|
||||
Done.
|
||||
```
|
||||
|
||||
|
||||
### Output 7
|
||||
```
|
||||
indent-style = tab
|
||||
|
|
@ -10954,6 +11026,42 @@ def markdown_skipped_rst_directive():
|
|||
```
|
||||
|
||||
|
||||
#### Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -489,10 +489,8 @@
|
||||
Do cool stuff::
|
||||
|
||||
if True:
|
||||
- cool_stuff(
|
||||
- '''
|
||||
- hiya'''
|
||||
- )
|
||||
+ cool_stuff('''
|
||||
+ hiya''')
|
||||
|
||||
Done.
|
||||
"""
|
||||
@@ -967,13 +965,11 @@
|
||||
Do cool stuff.
|
||||
|
||||
``````
|
||||
- do_something(
|
||||
- '''
|
||||
+ do_something('''
|
||||
```
|
||||
did i trick you?
|
||||
```
|
||||
- '''
|
||||
- )
|
||||
+ ''')
|
||||
``````
|
||||
|
||||
Done.
|
||||
```
|
||||
|
||||
|
||||
### Output 8
|
||||
```
|
||||
indent-style = tab
|
||||
|
|
@ -12322,6 +12430,42 @@ def markdown_skipped_rst_directive():
|
|||
```
|
||||
|
||||
|
||||
#### Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -480,10 +480,8 @@
|
||||
Do cool stuff::
|
||||
|
||||
if True:
|
||||
- cool_stuff(
|
||||
- '''
|
||||
- hiya'''
|
||||
- )
|
||||
+ cool_stuff('''
|
||||
+ hiya''')
|
||||
|
||||
Done.
|
||||
"""
|
||||
@@ -958,13 +956,11 @@
|
||||
Do cool stuff.
|
||||
|
||||
``````
|
||||
- do_something(
|
||||
- '''
|
||||
+ do_something('''
|
||||
```
|
||||
did i trick you?
|
||||
```
|
||||
- '''
|
||||
- )
|
||||
+ ''')
|
||||
``````
|
||||
|
||||
Done.
|
||||
```
|
||||
|
||||
|
||||
### Output 9
|
||||
```
|
||||
indent-style = space
|
||||
|
|
@ -13699,6 +13843,42 @@ def markdown_skipped_rst_directive():
|
|||
```
|
||||
|
||||
|
||||
#### Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -489,10 +489,8 @@
|
||||
Do cool stuff::
|
||||
|
||||
if True:
|
||||
- cool_stuff(
|
||||
- '''
|
||||
- hiya'''
|
||||
- )
|
||||
+ cool_stuff('''
|
||||
+ hiya''')
|
||||
|
||||
Done.
|
||||
"""
|
||||
@@ -967,13 +965,11 @@
|
||||
Do cool stuff.
|
||||
|
||||
``````
|
||||
- do_something(
|
||||
- '''
|
||||
+ do_something('''
|
||||
```
|
||||
did i trick you?
|
||||
```
|
||||
- '''
|
||||
- )
|
||||
+ ''')
|
||||
``````
|
||||
|
||||
Done.
|
||||
```
|
||||
|
||||
|
||||
### Output 10
|
||||
```
|
||||
indent-style = space
|
||||
|
|
@ -15067,4 +15247,40 @@ def markdown_skipped_rst_directive():
|
|||
```
|
||||
|
||||
|
||||
#### Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -480,10 +480,8 @@
|
||||
Do cool stuff::
|
||||
|
||||
if True:
|
||||
- cool_stuff(
|
||||
- '''
|
||||
- hiya'''
|
||||
- )
|
||||
+ cool_stuff('''
|
||||
+ hiya''')
|
||||
|
||||
Done.
|
||||
"""
|
||||
@@ -958,13 +956,11 @@
|
||||
Do cool stuff.
|
||||
|
||||
``````
|
||||
- do_something(
|
||||
- '''
|
||||
+ do_something('''
|
||||
```
|
||||
did i trick you?
|
||||
```
|
||||
- '''
|
||||
- )
|
||||
+ ''')
|
||||
``````
|
||||
|
||||
Done.
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,136 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/multiline_string_deviations.py
|
||||
---
|
||||
## Input
|
||||
```python
|
||||
# This file documents the deviations for formatting multiline strings with black.
|
||||
|
||||
# Black hugs the parentheses for `%` usages -> convert to fstring.
|
||||
# Can get unreadable if the arguments split
|
||||
# This could be solved by using `best_fitting` to try to format the arguments on a single
|
||||
# line. Let's consider adding this later.
|
||||
# ```python
|
||||
# call(
|
||||
# 3,
|
||||
# "dogsay",
|
||||
# textwrap.dedent(
|
||||
# """dove
|
||||
# coo""" % "cowabunga",
|
||||
# more,
|
||||
# and_more,
|
||||
# "aaaaaaa",
|
||||
# "bbbbbbbbb",
|
||||
# "cccccccc",
|
||||
# ),
|
||||
# )
|
||||
# ```
|
||||
call(3, "dogsay", textwrap.dedent("""dove
|
||||
coo""" % "cowabunga"))
|
||||
|
||||
# Black applies the hugging recursively. We don't (consistent with the hugging style).
|
||||
path.write_text(textwrap.dedent("""\
|
||||
A triple-quoted string
|
||||
actually leveraging the textwrap.dedent functionality
|
||||
that ends in a trailing newline,
|
||||
representing e.g. file contents.
|
||||
"""))
|
||||
|
||||
|
||||
|
||||
# Black avoids parenthesizing the following lambda. We could potentially support
|
||||
# this by changing `Lambda::needs_parentheses` to return `BestFit` but it causes
|
||||
# issues when the lambda has comments.
|
||||
# Let's keep this as a known deviation for now.
|
||||
generated_readme = lambda project_name: """
|
||||
{}
|
||||
|
||||
<Add content here!>
|
||||
""".strip().format(project_name)
|
||||
```
|
||||
|
||||
## Output
|
||||
```python
|
||||
# This file documents the deviations for formatting multiline strings with black.
|
||||
|
||||
# Black hugs the parentheses for `%` usages -> convert to fstring.
|
||||
# Can get unreadable if the arguments split
|
||||
# This could be solved by using `best_fitting` to try to format the arguments on a single
|
||||
# line. Let's consider adding this later.
|
||||
# ```python
|
||||
# call(
|
||||
# 3,
|
||||
# "dogsay",
|
||||
# textwrap.dedent(
|
||||
# """dove
|
||||
# coo""" % "cowabunga",
|
||||
# more,
|
||||
# and_more,
|
||||
# "aaaaaaa",
|
||||
# "bbbbbbbbb",
|
||||
# "cccccccc",
|
||||
# ),
|
||||
# )
|
||||
# ```
|
||||
call(
|
||||
3,
|
||||
"dogsay",
|
||||
textwrap.dedent(
|
||||
"""dove
|
||||
coo"""
|
||||
% "cowabunga"
|
||||
),
|
||||
)
|
||||
|
||||
# Black applies the hugging recursively. We don't (consistent with the hugging style).
|
||||
path.write_text(
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
A triple-quoted string
|
||||
actually leveraging the textwrap.dedent functionality
|
||||
that ends in a trailing newline,
|
||||
representing e.g. file contents.
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# Black avoids parenthesizing the following lambda. We could potentially support
|
||||
# this by changing `Lambda::needs_parentheses` to return `BestFit` but it causes
|
||||
# issues when the lambda has comments.
|
||||
# Let's keep this as a known deviation for now.
|
||||
generated_readme = (
|
||||
lambda project_name: """
|
||||
{}
|
||||
|
||||
<Add content here!>
|
||||
""".strip().format(project_name)
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
## Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -31,14 +31,12 @@
|
||||
|
||||
# Black applies the hugging recursively. We don't (consistent with the hugging style).
|
||||
path.write_text(
|
||||
- textwrap.dedent(
|
||||
- """\
|
||||
+ textwrap.dedent("""\
|
||||
A triple-quoted string
|
||||
actually leveraging the textwrap.dedent functionality
|
||||
that ends in a trailing newline,
|
||||
representing e.g. file contents.
|
||||
-"""
|
||||
- )
|
||||
+""")
|
||||
)
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue