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

View File

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

View File

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

View File

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

View File

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

View File

@@ -255,11 +255,7 @@ c1 = (
# Fits, either style # Fits, either style
d11 = x.e().e().e() # d11 = x.e().e().e() #
d12 = x.e().e().e() # d12 = x.e().e().e() #
d13 = ( d13 = x.e().e().e() #
x.e() #
.e()
.e()
)
# Doesn't fit, default # Doesn't fit, default
d2 = x.e().esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkfsdddd() # 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 # 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("aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa")
f( f(session, b=1, **dict()) # oddly placed end-of-line comment
session,
b=1,
**dict(), # oddly placed end-of-line comment
)
f( f(
session, session,
b=1, b=1,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -111,8 +111,7 @@ a1 = "a"[
a2 = "a"[ a2 = "a"[
# a # a
# b # b
: # c : # c# d
# d
] ]
# Check all places where comments can exist # Check all places where comments can exist
@@ -151,9 +150,7 @@ c4 = "c"[
] ]
# End of line comments # End of line comments
d1 = "d"[ # comment d1 = "d"[:] # comment
:
]
d2 = "d"[ # comment d2 = "d"[ # comment
1: 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 # Regression test for https://github.com/astral-sh/ruff/issues/5176#issuecomment-1598171989
def foo( def foo(b=3 + 2): # comment
b=3 + 2, # comment
):
... ...
@@ -1039,9 +1037,7 @@ def handleMatch( # type: ignore[override] # https://github.com/python/mypy/issu
... ...
def double( def double(a: int) -> int: # Hello
a: int, # Hello
) -> int:
return 2 * a return 2 * a

View File

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

View File

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