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

Support sticky standalone comments (comments preceding defs, classes, and decorators)
authorŁukasz Langa <lukasz@langa.pl>
Tue, 24 Apr 2018 18:50:31 +0000 (11:50 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Tue, 24 Apr 2018 18:50:31 +0000 (11:50 -0700)
Fixes #56
Fixes #154

README.md
black.py
tests/comments2.py
tests/comments5.py
tests/comments6.py [deleted file]
tests/empty_lines.py
tests/fmtonoff.py
tests/test_black.py

index 05bff36ffbd010e9010853502e057c65b82854f1..e2f91effd32fed6093fc4c0f79057ce6bcaa0061 100644 (file)
--- a/README.md
+++ b/README.md
@@ -230,11 +230,9 @@ are always reformatted to fit minimal space, this whitespace is lost.
 
 It will also insert proper spacing before and after function definitions.
 It's one line before and after inner functions and two lines before and
-after module-level functions.  *Black* will put those empty lines also
-between the function definition and any standalone comments that
-immediately precede the given function.  If you want to comment on the
-entire function, use a docstring or put a leading comment in the function
-body.
+after module-level functions.  *Black* will not put empty lines between
+function/class definitions and standalone comments that immediately precede
+the given function/class.
 
 
 ### Trailing commas
@@ -532,6 +530,9 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
 
 * fixed comment indentation when a standalone comment closes a block (#16, #32)
 
+* fixed standalone comments receiving extra empty lines if immediately preceding
+  a class, def, or decorator (#56, #154)
+
 * fixed `--diff` not showing entire path (#130)
 
 * fixed parsing of complex expressions after star and double stars in
index a03b9aad7607ffd4107b22b6382b6a00028d204c..11386d23f9df76ad9b57129ea2bb28eba4792f5d 100644 (file)
--- a/black.py
+++ b/black.py
@@ -1040,12 +1040,14 @@ class EmptyLineTracker:
                 # Don't insert empty lines before the first line in the file.
                 return 0, 0
 
-            if self.previous_line and self.previous_line.is_decorator:
-                # Don't insert empty lines between decorators.
+            if self.previous_line.is_decorator:
                 return 0, 0
 
-            if is_decorator and self.previous_line and self.previous_line.is_comment:
-                # Don't insert empty lines between decorator comments.
+            if (
+                self.previous_line.is_comment
+                and self.previous_line.depth == current_line.depth
+                and before == 0
+            ):
                 return 0, 0
 
             newlines = 2
index 73fff32a3b933d6629aee64650099177c35062ac..44a4711a6aa0a9a7f0fe40992b5c1175a1d42b46 100644 (file)
@@ -161,8 +161,6 @@ else:
     # add_compiler(compilers[(7.1, 64)])
 
 # Comment before function.
-
-
 def inline_comments_in_brackets_ruin_everything():
     if typedargslist:
         parameters.children = [
index 703922ddb75537192428b969d5018f0f9127072d..d83b6b8ff47cc6f731aee0f7a3369f3d6e7db960 100644 (file)
@@ -27,5 +27,45 @@ try:
 except OSError:
     print("problems")
 
+import sys
+
+
+# leading function comment
+def wat():
+    ...
+    # trailing function comment
+
+
+# SECTION COMMENT
+
+
+# leading 1
+@deco1
+# leading 2
+@deco2(with_args=True)
+# leading 3
+@deco3
+def decorated1():
+    ...
+
+
+# leading 1
+@deco1
+# leading 2
+@deco2(with_args=True)
+# leading function comment
+def decorated1():
+    ...
+
+
+# Note: crappy but inevitable.  The current design of EmptyLineTracker doesn't
+# allow this to work correctly.  The user will have to split those lines by
+# hand.
+some_instruction
+# This comment should be split from `some_instruction` by two lines but isn't.
+def g():
+    ...
+
+
 if __name__ == "__main__":
     main()
diff --git a/tests/comments6.py b/tests/comments6.py
deleted file mode 100644 (file)
index 0565015..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-@property
-# TODO: X
-@property
-# TODO: Y
-# TODO: Z
-@property
-def foo():
-    pass
index 0edeb014a8f91e4bed3d9864e29798264f22847d..5b7ce928a783ed27c4fd23446680a4989a8a3c0c 100644 (file)
@@ -1,3 +1,7 @@
+"""Docstring."""
+
+
+# leading comment
 def f():
     NO = ''
     SPACE = ' '
@@ -44,9 +48,11 @@ def f():
                 syms.dictsetmaker,
             }:
                 return NO
+
 ###############################################################################
 # SECTION BECAUSE SECTIONS
 ###############################################################################
+
 def g():
     NO = ''
     SPACE = ' '
@@ -89,6 +95,10 @@ def g():
 # output
 
 
+"""Docstring."""
+
+
+# leading comment
 def f():
     NO = ""
     SPACE = " "
index 16c392595c0cf32a4b963d44cb574ce7ee54d38f..a7b9bc744ed07148c3f7d445e22244b6ac90238c 100644 (file)
@@ -119,8 +119,6 @@ many_args=[1,2,3]
 def function_signature_stress_test(number:int,no_annotation=None,text:str='default',* ,debug:bool=False,**kwargs) -> str:
  return text[number:-1]
 # fmt: on
-
-
 def spaces(a=1, b=(), c=[], d={}, e=True, f=-1, g=1 if False else 2, h="", i=r""):
     offset = attr.ib(default=attr.Factory(lambda: _r.uniform(10000, 200000)))
     assert task._cancel_stack[:len(old_stack)] == old_stack
index 9c029df7eb7b13a891070c30879f4778e4cca121..dd3beed99abe0e5b6d7b3f37188fa41420f38493 100644 (file)
@@ -626,14 +626,6 @@ class BlackTestCase(unittest.TestCase):
             )
             self.assertEqual(result.exit_code, 1)
 
-    @patch("black.dump_to_file", dump_to_stderr)
-    def test_comment_in_decorator(self) -> None:
-        source, expected = read_data("comments6")
-        actual = fs(source)
-        self.assertFormatEqual(expected, actual)
-        black.assert_equivalent(source, actual)
-        black.assert_stable(source, actual, line_length=ll)
-
 
 if __name__ == "__main__":
     unittest.main()