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

Remove NBSP at the beginning of comments (#2092)
authorPierre Sassoulas <pierre.sassoulas@gmail.com>
Sun, 11 Apr 2021 21:41:22 +0000 (23:41 +0200)
committerGitHub <noreply@github.com>
Sun, 11 Apr 2021 21:41:22 +0000 (14:41 -0700)
Closes #2091

CHANGES.md
src/black/__init__.py
tests/data/comments7.py
tests/data/comments_non_breaking_space.py [new file with mode: 0644]
tests/test_format.py

index 6340f60ae59ec71a85e5347b4c0120613fdba56b..12e8d45327709b61b9cd9b8cc7eddc1be46582d0 100644 (file)
@@ -4,6 +4,8 @@
 
 #### _Black_
 
+- `Black` now cleans up leading non-breaking spaces in comments (#2092)
+
 - `Black` now respects `--skip-string-normalization` when normalizing multiline
   docstring quotes (#1637)
 
index 2bb42a470eadb617b3cb57c4ee4b9c0c76377d40..36716474e8c187adcb4acddf1a46d46c17603e3f 100644 (file)
@@ -2709,6 +2709,13 @@ def make_comment(content: str) -> str:
 
     if content[0] == "#":
         content = content[1:]
+    NON_BREAKING_SPACE = " "
+    if (
+        content
+        and content[0] == NON_BREAKING_SPACE
+        and not content.lstrip().startswith("type:")
+    ):
+        content = " " + content[1:]  # Replace NBSP by a simple space
     if content and content[0] not in " !:#'%":
         content = " " + content
     return "#" + content
index 0e2bd35bf55b35829f2d0427c91d57669ffe7a83..ca9d7c62b215515e57c21a0ef55dd8ad07174c36 100644 (file)
@@ -129,6 +129,8 @@ class C:
     ):
         ...
 
+square = Square(4) # type: Optional[Square]
+
 # output
 
 from .config import (
@@ -263,4 +265,7 @@ class C:
     def test_fails_invalid_post_data(
         self, pyramid_config, db_request, post_data, message
     ):
-        ...
\ No newline at end of file
+        ...
+
+
+square = Square(4)  # type: Optional[Square]
diff --git a/tests/data/comments_non_breaking_space.py b/tests/data/comments_non_breaking_space.py
new file mode 100644 (file)
index 0000000..e17c3f4
--- /dev/null
@@ -0,0 +1,44 @@
+from .config import (  ConfigTypeAttributes,    Int,    Path,    # String,
+    # DEFAULT_TYPE_ATTRIBUTES,
+)
+
+result = 1  # A simple comment
+result = (    1, ) # Another one
+
+result = 1    # type: ignore
+result = 1# This comment is talking about type: ignore
+square = Square(4) # type: Optional[Square]
+
+def function(a:int=42):
+    """ This docstring is already formatted
+       a
+       b
+    """
+    #    There's a NBSP + 3 spaces before
+    #    And 4 spaces on the next line
+    pass
+
+# output
+from .config import (
+    ConfigTypeAttributes,
+    Int,
+    Path,  # String,
+    # DEFAULT_TYPE_ATTRIBUTES,
+)
+
+result = 1  # A simple comment
+result = (1,)  # Another one
+
+result = 1  #  type: ignore
+result = 1  # This comment is talking about type: ignore
+square = Square(4)  #  type: Optional[Square]
+
+
+def function(a: int = 42):
+    """This docstring is already formatted
+    a
+    b
+    """
+    #    There's a NBSP + 3 spaces before
+    #    And 4 spaces on the next line
+    pass
index e0cb0b74195792a30f06b3ac63646bcb80ab64a1..eabec8c72191b634c4ae1ddbba9a443ead8c9de9 100644 (file)
@@ -27,6 +27,7 @@ SIMPLE_CASES = [
     "comments5",
     "comments6",
     "comments7",
+    "comments_non_breaking_space",
     "comment_after_escaped_newline",
     "composition",
     "composition_no_trailing_comma",