Unverified Commit 8f380911 authored by Zsolt Dollenstein's avatar Zsolt Dollenstein Committed by GitHub
Browse files

Pin comment to single leaf in invisible parens (#872)

parent 9394de15
Loading
Loading
Loading
Loading
+24 −5
Changes for black.py: 24 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -1282,10 +1282,13 @@ def contains_inner_type_comments(self) -> bool:
        try:
            last_leaf = self.leaves[-1]
            ignored_ids.add(id(last_leaf))
            if last_leaf.type == token.COMMA:
                # When trailing commas 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.
            if last_leaf.type == token.COMMA or (
                last_leaf.type == token.RPAR and not last_leaf.value
            ):
                # 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]
                ignored_ids.add(id(last_leaf))
        except IndexError:
@@ -1382,7 +1385,23 @@ def append_comment(self, comment: Leaf) -> bool:
            comment.prefix = ""
            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

    def comments_after(self, leaf: Leaf) -> List[Leaf]:
+2 −2
Changes for tests/data/cantfit.py: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -47,8 +47,8 @@
    0
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
    1
)  # with a comment
    1  # with a comment
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = [
    1,
    2,
+3 −0
Changes for tests/data/comments6.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -84,3 +84,6 @@ def func(
        0.0789,
        a[-1],  # type: ignore
    )


result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # aaa
+22 −0
Changes for tests/data/comments7.py: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -23,6 +23,19 @@
    #  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

from .config import (
@@ -49,3 +62,12 @@
    #  resolve_to_config_type,
    #  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