]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Pin comment to single leaf in invisible parens (#872)
authorZsolt Dollenstein <zsol.zsol@gmail.com>
Sat, 15 Jun 2019 06:29:09 +0000 (07:29 +0100)
committerGitHub <noreply@github.com>
Sat, 15 Jun 2019 06:29:09 +0000 (07:29 +0100)
black.py
tests/data/cantfit.py
tests/data/comments6.py
tests/data/comments7.py

index 8318674a722f41fa70e4dfe95febe839dd93c87e..ce27653af14d6451aa23edea57419644a234e8a0 100644 (file)
--- a/black.py
+++ b/black.py
@@ -1282,10 +1282,13 @@ class Line:
         try:
             last_leaf = self.leaves[-1]
             ignored_ids.add(id(last_leaf))
         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:
                 last_leaf = self.leaves[-2]
                 ignored_ids.add(id(last_leaf))
         except IndexError:
@@ -1382,7 +1385,23 @@ class Line:
             comment.prefix = ""
             return False
 
             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]:
         return True
 
     def comments_after(self, leaf: Leaf) -> List[Leaf]:
index 0ab1575ffbcd0e6999e074e01b84ed6020b0efc3..317a38fc6e63484db41fd6809ff59964b14d4331 100644 (file)
@@ -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 = (
     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,
 this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = [
     1,
     2,
index ce17382fb2f928acfcb613327eb90c1166621193..a2cd018adcb77cea5859184cfc3220a0abb45c3f 100644 (file)
@@ -84,3 +84,6 @@ def func(
         0.0789,
         a[-1],  # type: ignore
     )
         0.0789,
         a[-1],  # type: ignore
     )
+
+
+result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # aaa
index 4159d847071382e4fff78054674ca3f362dc25c9..f69863eed47283f4f869e69717332af275fe20a4 100644 (file)
@@ -23,6 +23,19 @@ from .config import (
     #  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
 
 from .config import (
 # output
 
 from .config import (
@@ -49,3 +62,12 @@ from .config import (
     #  resolve_to_config_type,
     #  DEFAULT_TYPE_ATTRIBUTES,
 )
     #  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