mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 13:33:50 +00:00
Format function and class definitions into a single line if its body is an ellipsis (#6592)
This commit is contained in:
parent
bb5fbb1b5c
commit
2a8d24dd4b
15 changed files with 445 additions and 60 deletions
|
@ -0,0 +1,193 @@
|
|||
---
|
||||
source: crates/ruff_python_formatter/tests/fixtures.rs
|
||||
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/ellipsis.pyi
|
||||
---
|
||||
## Input
|
||||
```py
|
||||
"""Compound statements with no body should be written on one line."""
|
||||
|
||||
if True:
|
||||
...
|
||||
elif True:
|
||||
...
|
||||
else:
|
||||
...
|
||||
|
||||
if True:
|
||||
# comment
|
||||
...
|
||||
elif True:
|
||||
# comment
|
||||
...
|
||||
else:
|
||||
# comment
|
||||
...
|
||||
|
||||
if True:
|
||||
... # comment
|
||||
elif True:
|
||||
... # comment
|
||||
else:
|
||||
... # comment
|
||||
|
||||
for i in []:
|
||||
...
|
||||
else:
|
||||
...
|
||||
|
||||
for i in []:
|
||||
# comment
|
||||
...
|
||||
else:
|
||||
# comment
|
||||
...
|
||||
|
||||
for i in []:
|
||||
... # comment
|
||||
else:
|
||||
... # comment
|
||||
|
||||
while True:
|
||||
...
|
||||
else:
|
||||
...
|
||||
|
||||
while True:
|
||||
# comment
|
||||
...
|
||||
else:
|
||||
# comment
|
||||
...
|
||||
|
||||
while True:
|
||||
... # comment
|
||||
else:
|
||||
... # comment
|
||||
|
||||
with True:
|
||||
...
|
||||
|
||||
with True:
|
||||
# comment
|
||||
...
|
||||
|
||||
with True:
|
||||
... # comment
|
||||
|
||||
match x:
|
||||
case 1:
|
||||
...
|
||||
case 2:
|
||||
# comment
|
||||
...
|
||||
case 3:
|
||||
... # comment
|
||||
|
||||
try:
|
||||
...
|
||||
except:
|
||||
...
|
||||
finally:
|
||||
...
|
||||
|
||||
try:
|
||||
# comment
|
||||
...
|
||||
except:
|
||||
# comment
|
||||
...
|
||||
finally:
|
||||
# comment
|
||||
...
|
||||
|
||||
try:
|
||||
... # comment
|
||||
except:
|
||||
... # comment
|
||||
finally:
|
||||
... # comment```
|
||||
|
||||
## Output
|
||||
```py
|
||||
"""Compound statements with no body should be written on one line."""
|
||||
|
||||
if True: ...
|
||||
elif True: ...
|
||||
else: ...
|
||||
|
||||
if True:
|
||||
# comment
|
||||
...
|
||||
elif True:
|
||||
# comment
|
||||
...
|
||||
else:
|
||||
# comment
|
||||
...
|
||||
|
||||
if True: ... # comment
|
||||
elif True: ... # comment
|
||||
else: ... # comment
|
||||
|
||||
for i in []: ...
|
||||
else: ...
|
||||
|
||||
for i in []:
|
||||
# comment
|
||||
...
|
||||
else:
|
||||
# comment
|
||||
...
|
||||
|
||||
for i in []: ... # comment
|
||||
else: ... # comment
|
||||
|
||||
while True: ...
|
||||
else: ...
|
||||
|
||||
while True:
|
||||
# comment
|
||||
...
|
||||
else:
|
||||
# comment
|
||||
...
|
||||
|
||||
while True: ... # comment
|
||||
else: ... # comment
|
||||
|
||||
with True: ...
|
||||
|
||||
with True:
|
||||
# comment
|
||||
...
|
||||
|
||||
with True: ... # comment
|
||||
|
||||
match x:
|
||||
case "NOT_YET_IMPLEMENTED_PatternMatchValue": ...
|
||||
case "NOT_YET_IMPLEMENTED_PatternMatchValue":
|
||||
# comment
|
||||
...
|
||||
case "NOT_YET_IMPLEMENTED_PatternMatchValue": ... # comment
|
||||
|
||||
try: ...
|
||||
except: ...
|
||||
finally: ...
|
||||
|
||||
try:
|
||||
# comment
|
||||
...
|
||||
except:
|
||||
# comment
|
||||
...
|
||||
finally:
|
||||
# comment
|
||||
...
|
||||
|
||||
try: ... # comment
|
||||
except: ... # comment
|
||||
finally: ... # comment
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -82,6 +82,19 @@ class Test2(A):
|
|||
def b(): ...
|
||||
# comment
|
||||
def c(): ...
|
||||
|
||||
class EllipsisWithComment:
|
||||
... # comment
|
||||
|
||||
def function_with_comment():
|
||||
... # comment
|
||||
|
||||
class EllispsisWithMultipleTrailing: # trailing class comment
|
||||
... # trailing ellipsis comment
|
||||
|
||||
class EllipsisWithLeadingComment:
|
||||
# leading
|
||||
...
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -97,18 +110,13 @@ class B:
|
|||
def foo():
|
||||
pass
|
||||
|
||||
class Del(expr_context):
|
||||
...
|
||||
class Load(expr_context):
|
||||
...
|
||||
class Del(expr_context): ...
|
||||
class Load(expr_context): ...
|
||||
|
||||
# Some comment.
|
||||
class Other(expr_context):
|
||||
...
|
||||
class Store(expr_context):
|
||||
...
|
||||
class Foo(Bar):
|
||||
...
|
||||
class Other(expr_context): ...
|
||||
class Store(expr_context): ...
|
||||
class Foo(Bar): ...
|
||||
|
||||
class Baz(Qux):
|
||||
def __init__(self):
|
||||
|
@ -123,29 +131,33 @@ class Quuz(Qux):
|
|||
def __init__(self):
|
||||
pass
|
||||
|
||||
def bar():
|
||||
...
|
||||
def baz():
|
||||
...
|
||||
def bar(): ...
|
||||
def baz(): ...
|
||||
def quux():
|
||||
"""Some docstring."""
|
||||
|
||||
def quuz():
|
||||
"""Some docstring."""
|
||||
|
||||
def a():
|
||||
...
|
||||
def a(): ...
|
||||
|
||||
class Test:
|
||||
...
|
||||
class Test2(A):
|
||||
...
|
||||
class Test: ...
|
||||
class Test2(A): ...
|
||||
|
||||
def b():
|
||||
...
|
||||
def b(): ...
|
||||
|
||||
# comment
|
||||
def c():
|
||||
def c(): ...
|
||||
|
||||
class EllipsisWithComment: ... # comment
|
||||
|
||||
def function_with_comment(): ... # comment
|
||||
|
||||
class EllispsisWithMultipleTrailing: # trailing class comment
|
||||
... # trailing ellipsis comment
|
||||
|
||||
class EllipsisWithLeadingComment:
|
||||
# leading
|
||||
...
|
||||
```
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue