]> 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:

Avoid unstable formatting when comment follows escaped newline. (#839). Fixes #767.
authorCarl Meyer <carljm@instagram.com>
Wed, 8 May 2019 13:53:20 +0000 (09:53 -0400)
committerGitHub <noreply@github.com>
Wed, 8 May 2019 13:53:20 +0000 (09:53 -0400)
black.py
tests/data/comment_after_escaped_newline.py [new file with mode: 0644]
tests/data/comments.py
tests/test_black.py

index 18d60c02d6b48cc918a5978a7e2124b1f6f160c3..1978fd52ec91969e226908ebb566d18f422a3ab7 100644 (file)
--- a/black.py
+++ b/black.py
@@ -2145,15 +2145,21 @@ def list_comments(prefix: str, *, is_endmarker: bool) -> List[ProtoComment]:
 
     consumed = 0
     nlines = 0
+    ignored_lines = 0
     for index, line in enumerate(prefix.split("\n")):
         consumed += len(line) + 1  # adding the length of the split '\n'
         line = line.lstrip()
         if not line:
             nlines += 1
         if not line.startswith("#"):
+            # Escaped newlines outside of a comment are not really newlines at
+            # all. We treat a single-line comment following an escaped newline
+            # as a simple trailing comment.
+            if line.endswith("\\"):
+                ignored_lines += 1
             continue
 
-        if index == 0 and not is_endmarker:
+        if index == ignored_lines and not is_endmarker:
             comment_type = token.COMMENT  # simple trailing comment
         else:
             comment_type = STANDALONE_COMMENT
diff --git a/tests/data/comment_after_escaped_newline.py b/tests/data/comment_after_escaped_newline.py
new file mode 100644 (file)
index 0000000..133f489
--- /dev/null
@@ -0,0 +1,18 @@
+def bob(): \
+         # pylint: disable=W9016
+    pass
+
+
+def bobtwo(): \
+    \
+  # some comment here
+    pass
+
+# output
+
+def bob():  # pylint: disable=W9016
+    pass
+
+
+def bobtwo():  # some comment here
+    pass
index 4ae3d2c2862100522b35874241c9ad97d0428826..a20e653dae1207d4a78342f60d235e63a916ac0a 100644 (file)
@@ -75,6 +75,8 @@ class Foo:
 
 @fast(really=True)
 async def wat():
+    # This comment, for some reason \
+    # contains a trailing backslash.
     async with X.open_async() as x:  # Some more comments
         result = await x.method1()
     # Comment after ending a block.
index 896ad0c37dd7e7238ad443a51962b3f9864fb2ac..53d175066a95e4e1e56d4f7ee2460fada0e9309b 100644 (file)
@@ -396,6 +396,14 @@ class BlackTestCase(unittest.TestCase):
         black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, black.FileMode())
 
+    @patch("black.dump_to_file", dump_to_stderr)
+    def test_comment_after_escaped_newline(self) -> None:
+        source, expected = read_data("comment_after_escaped_newline")
+        actual = fs(source)
+        self.assertFormatEqual(expected, actual)
+        black.assert_equivalent(source, actual)
+        black.assert_stable(source, actual, black.FileMode())
+
     @patch("black.dump_to_file", dump_to_stderr)
     def test_cantfit(self) -> None:
         source, expected = read_data("cantfit")