From: Zsolt Dollenstein Date: Sat, 15 Jun 2019 06:29:09 +0000 (+0100) Subject: Pin comment to single leaf in invisible parens (#872) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/8f380911e0cd8057bd9a1f0c418d81195ce858a3?hp=9394de150ebf0adc426523f46dc08e8b2b2b0b63 Pin comment to single leaf in invisible parens (#872) --- diff --git a/black.py b/black.py index 8318674..ce27653 100644 --- a/black.py +++ b/black.py @@ -1282,10 +1282,13 @@ class Line: 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 @@ class Line: 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]: diff --git a/tests/data/cantfit.py b/tests/data/cantfit.py index 0ab1575..317a38f 100644 --- a/tests/data/cantfit.py +++ b/tests/data/cantfit.py @@ -47,8 +47,8 @@ this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_li 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, diff --git a/tests/data/comments6.py b/tests/data/comments6.py index ce17382..a2cd018 100644 --- a/tests/data/comments6.py +++ b/tests/data/comments6.py @@ -84,3 +84,6 @@ def func( 0.0789, a[-1], # type: ignore ) + + +result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # aaa diff --git a/tests/data/comments7.py b/tests/data/comments7.py index 4159d84..f69863e 100644 --- a/tests/data/comments7.py +++ b/tests/data/comments7.py @@ -23,6 +23,19 @@ from .config import ( # 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 @@ from .config import ( # 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