Pin comment to single leaf in invisible parens (#872)
This commit is contained in:
parent
9394de150e
commit
8f380911e0
29
black.py
29
black.py
@ -1282,10 +1282,13 @@ def contains_inner_type_comments(self) -> bool:
|
|||||||
try:
|
try:
|
||||||
last_leaf = self.leaves[-1]
|
last_leaf = self.leaves[-1]
|
||||||
ignored_ids.add(id(last_leaf))
|
ignored_ids.add(id(last_leaf))
|
||||||
if last_leaf.type == token.COMMA:
|
if last_leaf.type == token.COMMA or (
|
||||||
# When trailing commas are inserted by Black for consistency, comments
|
last_leaf.type == token.RPAR and not last_leaf.value
|
||||||
# after the previous last element are not moved (they don't have to,
|
):
|
||||||
# rendering will still be correct). So we ignore trailing commas.
|
# When trailing commas or optional parens are inserted by Black for
|
||||||
|
# consistency, comments after the previous last element are not moved
|
||||||
|
# (they don't have to, rendering will still be correct). So we ignore
|
||||||
|
# trailing commas and invisible.
|
||||||
last_leaf = self.leaves[-2]
|
last_leaf = self.leaves[-2]
|
||||||
ignored_ids.add(id(last_leaf))
|
ignored_ids.add(id(last_leaf))
|
||||||
except IndexError:
|
except IndexError:
|
||||||
@ -1382,7 +1385,23 @@ def append_comment(self, comment: Leaf) -> bool:
|
|||||||
comment.prefix = ""
|
comment.prefix = ""
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.comments.setdefault(id(self.leaves[-1]), []).append(comment)
|
last_leaf = self.leaves[-1]
|
||||||
|
if (
|
||||||
|
last_leaf.type == token.RPAR
|
||||||
|
and not last_leaf.value
|
||||||
|
and last_leaf.parent
|
||||||
|
and len(list(last_leaf.parent.leaves())) <= 3
|
||||||
|
and not is_type_comment(comment)
|
||||||
|
):
|
||||||
|
# Comments on an optional parens wrapping a single leaf should belong to
|
||||||
|
# the wrapped node except if it's a type comment. Pinning the comment like
|
||||||
|
# this avoids unstable formatting caused by comment migration.
|
||||||
|
if len(self.leaves) < 2:
|
||||||
|
comment.type = STANDALONE_COMMENT
|
||||||
|
comment.prefix = ""
|
||||||
|
return False
|
||||||
|
last_leaf = self.leaves[-2]
|
||||||
|
self.comments.setdefault(id(last_leaf), []).append(comment)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def comments_after(self, leaf: Leaf) -> List[Leaf]:
|
def comments_after(self, leaf: Leaf) -> List[Leaf]:
|
||||||
|
@ -47,8 +47,8 @@
|
|||||||
0
|
0
|
||||||
)
|
)
|
||||||
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
|
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
|
||||||
1
|
1 # with a comment
|
||||||
) # with a comment
|
)
|
||||||
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = [
|
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = [
|
||||||
1,
|
1,
|
||||||
2,
|
2,
|
||||||
|
@ -84,3 +84,6 @@ def func(
|
|||||||
0.0789,
|
0.0789,
|
||||||
a[-1], # type: ignore
|
a[-1], # type: ignore
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # aaa
|
||||||
|
@ -23,6 +23,19 @@
|
|||||||
# DEFAULT_TYPE_ATTRIBUTES,
|
# DEFAULT_TYPE_ATTRIBUTES,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||||
|
|
||||||
|
result = (
|
||||||
|
1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||||
|
)
|
||||||
|
|
||||||
|
result = (
|
||||||
|
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # aaa
|
||||||
|
)
|
||||||
|
|
||||||
|
result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # aaa
|
||||||
|
|
||||||
# output
|
# output
|
||||||
|
|
||||||
from .config import (
|
from .config import (
|
||||||
@ -49,3 +62,12 @@
|
|||||||
# resolve_to_config_type,
|
# resolve_to_config_type,
|
||||||
# DEFAULT_TYPE_ATTRIBUTES,
|
# DEFAULT_TYPE_ATTRIBUTES,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||||
|
|
||||||
|
result = 1 # look ma, no comment migration xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||||
|
|
||||||
|
result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # aaa
|
||||||
|
|
||||||
|
result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # aaa
|
||||||
|
Loading…
Reference in New Issue
Block a user