Compare commits

...

1 Commits

Author SHA1 Message Date
Charlie Marsh
6783d8a53b Don't expand groups for trailing end-of-line comments 2023-08-09 20:17:31 -04:00
18 changed files with 65 additions and 204 deletions

View File

@@ -161,10 +161,11 @@ impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
} else {
write!(
f,
[
line_suffix(&format_args![space(), space(), format_comment(trailing)]),
expand_parent()
]
[line_suffix(&format_args![
space(),
space(),
format_comment(trailing)
])]
)?;
}
@@ -210,13 +211,17 @@ impl Format<PyFormatContext<'_>> for FormatDanglingComments<'_> {
write!(f, [space(), space()])?;
}
write!(
f,
[
format_comment(comment),
empty_lines(lines_after(comment.slice().end(), f.context().source()))
]
)?;
if comment.line_position.is_end_of_line() {
write!(f, [format_comment(comment)])?;
} else {
write!(
f,
[
format_comment(comment),
empty_lines(lines_after(comment.slice().end(), f.context().source()))
]
)?;
}
comment.mark_formatted();

View File

@@ -65,7 +65,7 @@ match match(
```diff
--- Black
+++ Ruff
@@ -1,35 +1,34 @@
@@ -1,16 +1,11 @@
match something:
- case b():
+ case NOT_YET_IMPLEMENTED_Pattern:
@@ -85,29 +85,9 @@ match match(
+ case NOT_YET_IMPLEMENTED_Pattern:
pass
-match(arg) # comment
+match(
+ arg # comment
+)
match()
match()
-case(arg) # comment
+case(
+ arg # comment
+)
case()
case()
-re.match(something) # fast
+re.match(
+ something # fast
+)
match(arg) # comment
@@ -29,7 +24,5 @@
re.match(something) # fast
re.match()
match match():
- case case(
@@ -130,26 +110,20 @@ match something:
case NOT_YET_IMPLEMENTED_Pattern:
pass
match(
arg # comment
)
match(arg) # comment
match()
match()
case(
arg # comment
)
case(arg) # comment
case()
case()
re.match(
something # fast
)
re.match(something) # fast
re.match()
match match():
case NOT_YET_IMPLEMENTED_Pattern:

View File

@@ -191,21 +191,7 @@ instruction()#comment with bad spacing
)
# Please keep __all__ alphabetized within each category.
@@ -60,8 +60,12 @@
# Comment before function.
def inline_comments_in_brackets_ruin_everything():
if typedargslist:
- parameters.children = [children[0], body, children[-1]] # (1 # )1
parameters.children = [
+ children[0], # (1
+ body,
+ children[-1], # )1
+ ]
+ parameters.children = [
children[0],
body,
children[-1], # type: ignore
@@ -72,7 +76,11 @@
@@ -72,7 +72,11 @@
body,
parameters.children[-1], # )2
]
@@ -218,41 +204,17 @@ instruction()#comment with bad spacing
if (
self._proc is not None
# has the child process finished?
@@ -115,7 +123,9 @@
@@ -114,9 +118,7 @@
# yup
arg3=True,
)
lcomp = [
- lcomp = [
- element for element in collection if element is not None # yup # yup # right
+ element # yup
+ for element in collection # yup
+ if element is not None # right
]
- ]
+ lcomp = [element for element in collection if element is not None] # yup # yup # right
lcomp2 = [
# hello
@@ -143,7 +153,10 @@
# let's return
return Node(
syms.simple_stmt,
- [Node(statement, result), Leaf(token.NEWLINE, "\n")], # FIXME: \r\n?
+ [
+ Node(statement, result),
+ Leaf(token.NEWLINE, "\n"), # FIXME: \r\n?
+ ],
)
@@ -158,7 +171,10 @@
class Test:
def _init_host(self, parsed) -> None:
- if parsed.hostname is None or not parsed.hostname.strip(): # type: ignore
+ if (
+ parsed.hostname is None # type: ignore
+ or not parsed.hostname.strip()
+ ):
pass
element
```
## Ruff Output
@@ -320,11 +282,7 @@ else:
# Comment before function.
def inline_comments_in_brackets_ruin_everything():
if typedargslist:
parameters.children = [
children[0], # (1
body,
children[-1], # )1
]
parameters.children = [children[0], body, children[-1]] # (1 # )1
parameters.children = [
children[0],
body,
@@ -382,11 +340,7 @@ short
# yup
arg3=True,
)
lcomp = [
element # yup
for element in collection # yup
if element is not None # right
]
lcomp = [element for element in collection if element is not None] # yup # yup # right
lcomp2 = [
# hello
element
@@ -413,10 +367,7 @@ short
# let's return
return Node(
syms.simple_stmt,
[
Node(statement, result),
Leaf(token.NEWLINE, "\n"), # FIXME: \r\n?
],
[Node(statement, result), Leaf(token.NEWLINE, "\n")], # FIXME: \r\n?
)
@@ -431,10 +382,7 @@ CONFIG_FILES = (
class Test:
def _init_host(self, parsed) -> None:
if (
parsed.hostname is None # type: ignore
or not parsed.hostname.strip()
):
if parsed.hostname is None or not parsed.hostname.strip(): # type: ignore
pass

View File

@@ -21,26 +21,24 @@ else:
```diff
--- Black
+++ Ruff
@@ -1,7 +1,7 @@
@@ -1,9 +1,5 @@
a, b, c = 3, 4, 5
if (
a == 3
-if (
- a == 3
- and b != 9 # fmt: skip
+ and b != 9 # fmt: skip
and c is not None
):
- and c is not None
-):
+if a == 3 and b != 9 and c is not None: # fmt: skip
print("I'm good!")
else:
print("I'm bad")
```
## Ruff Output
```py
a, b, c = 3, 4, 5
if (
a == 3
and b != 9 # fmt: skip
and c is not None
):
if a == 3 and b != 9 and c is not None: # fmt: skip
print("I'm good!")
else:
print("I'm bad")

View File

@@ -100,20 +100,7 @@ def foo() -> tuple[int, int, int,]:
```diff
--- Black
+++ Ruff
@@ -26,7 +26,11 @@
return 2 * a
-def double(a: int) -> int: # Hello
+def double(
+ a: int
+) -> (
+ int # Hello
+):
return 2 * a
@@ -54,7 +58,9 @@
@@ -54,7 +54,9 @@
a: int,
b: int,
c: int,
@@ -157,11 +144,7 @@ def double(a: int) -> int: # Hello
return 2 * a
def double(
a: int
) -> (
int # Hello
):
def double(a: int) -> int: # Hello
return 2 * a

View File

@@ -255,11 +255,7 @@ c1 = (
# Fits, either style
d11 = x.e().e().e() #
d12 = x.e().e().e() #
d13 = (
x.e() #
.e()
.e()
)
d13 = x.e().e().e() #
# Doesn't fit, default
d2 = x.e().esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkfsdddd() #

View File

@@ -506,12 +506,8 @@ x = (
- ( #
)
)
x = (
() - () #
)
x = (
() - () #
)
x = () - () #
x = () - () #
```

View File

@@ -195,11 +195,7 @@ result = (
# TODO(konstin): Black has this special case for comment placement where everything stays in one line
f("aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa")
f(
session,
b=1,
**dict(), # oddly placed end-of-line comment
)
f(session, b=1, **dict()) # oddly placed end-of-line comment
f(
session,
b=1,

View File

@@ -138,10 +138,7 @@ a not in b
== b
)
(
a # comment
== b
)
(a == b) # comment
a < b > c == d

View File

@@ -78,14 +78,9 @@ x={ # dangling end of line comment
{**d}
{
**a, # leading
**b, # middle # trailing
}
{**a, **b} # leading # middle # trailing
{
**b # middle with single item
}
{**b} # middle with single item
{
# before

View File

@@ -128,10 +128,7 @@ aaaaaaaaaaaaaaaaaaaaa = {
]
}
{
a: a # a
for c in e # for # c # in # e
}
{a: a for c in e} # a # for # c # in # e
{
# above a

View File

@@ -158,9 +158,7 @@ lambda x: lambda y: lambda z: (
# Trailing
a = (
lambda: 1 # Dangling
)
a = lambda: 1 # Dangling
# Regression test: lambda empty arguments ranges were too long, leading to unstable
# formatting

View File

@@ -92,10 +92,7 @@ aaaaaaaaaaaaaaaaaaaaa = [
]
]
[
a # a
for c in e # for # c # in # e
]
[a for c in e] # a # for # c # in # e
[
# above a

View File

@@ -72,10 +72,7 @@ selected_choices = {
]
}
{
a # a
for c in e # for # c # in # e
}
{a for c in e} # a # for # c # in # e
{
# above a

View File

@@ -111,8 +111,7 @@ a1 = "a"[
a2 = "a"[
# a
# b
: # c
# d
: # c# d
]
# Check all places where comments can exist
@@ -151,9 +150,7 @@ c4 = "c"[
]
# End of line comments
d1 = "d"[ # comment
:
]
d1 = "d"[:] # comment
d2 = "d"[ # comment
1:
]

View File

@@ -635,9 +635,7 @@ def f(arg1=1, *, kwonlyarg1, kwonlyarg2=2):
# Regression test for https://github.com/astral-sh/ruff/issues/5176#issuecomment-1598171989
def foo(
b=3 + 2, # comment
):
def foo(b=3 + 2): # comment
...
@@ -1039,9 +1037,7 @@ def handleMatch( # type: ignore[override] # https://github.com/python/mypy/issu
...
def double(
a: int, # Hello
) -> int:
def double(a: int) -> int: # Hello
return 2 * a

View File

@@ -61,11 +61,7 @@ while some_condition(unformatted, args) and anotherCondition or aThirdCondition:
print("Do something")
while (
some_condition(unformatted, args) # trailing some condition
and anotherCondition
or aThirdCondition # trailing third condition
): # comment
while some_condition(unformatted, args) and anotherCondition or aThirdCondition: # trailing some condition # trailing third condition # comment
print("Do something")
```

View File

@@ -142,10 +142,7 @@ with (
# trailing
with (
a, # a # comma
b, # c
): # colon
with a, b: # a # comma # c # colon
...
@@ -218,11 +215,9 @@ with (
...
with (
(
a
# trailing own line comment
) as b # trailing as same line comment # trailing b same line comment
):
a
# trailing own line comment
) as b: # trailing as same line comment # trailing b same line comment
...
with (