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

Don't split on for-loop variable unpacks
authorŁukasz Langa <lukasz@langa.pl>
Fri, 16 Mar 2018 02:20:42 +0000 (19:20 -0700)
committerLukasz Langa <ambv@fb.com>
Fri, 16 Mar 2018 02:23:19 +0000 (19:23 -0700)
Fixes #23

README.md
black.py
tests/expression.py

index ebff26a047a57e3e07ae15f212ac74999caeef01..b3e69851965c409d0ec1c0ab24c712f597bde368 100644 (file)
--- a/README.md
+++ b/README.md
@@ -262,6 +262,9 @@ You can still try but prepare to be disappointed.
 
 * fixed invalid spacing of dots in relative imports (#6, #13)
 
+* fixed invalid splitting after comma on unpacked variables in for-loops
+  (#23)
+
 * fixed spurious space in parenthesized set expressions (#7)
 
 * fixed spurious space after opening parentheses and in default
index e2c427cb536c752b6e5d0e00c07ca2b2a2a6a23e..774d91dc2a7f64ec2dbc252a55e0fe051d5c429c 100644 (file)
--- a/black.py
+++ b/black.py
@@ -392,6 +392,8 @@ class Line:
     comments: Dict[LeafID, Leaf] = attrib(default=Factory(dict))
     bracket_tracker: BracketTracker = attrib(default=Factory(BracketTracker))
     inside_brackets: bool = attrib(default=False)
+    has_for: bool = attrib(default=False)
+    _for_loop_variable: bool = attrib(default=False, init=False)
 
     def append(self, leaf: Leaf, preformatted: bool = False) -> None:
         has_value = leaf.value.strip()
@@ -403,8 +405,10 @@ class Line:
             # imports, for which we only preserve newlines.
             leaf.prefix += whitespace(leaf)
         if self.inside_brackets or not preformatted:
+            self.maybe_decrement_after_for_loop_variable(leaf)
             self.bracket_tracker.mark(leaf)
             self.maybe_remove_trailing_comma(leaf)
+            self.maybe_increment_for_loop_variable(leaf)
             if self.maybe_adapt_standalone_comment(leaf):
                 return
 
@@ -508,6 +512,29 @@ class Line:
 
         return False
 
+    def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
+        """In a for loop, or comprehension, the variables are often unpacks.
+
+        To avoid splitting on the comma in this situation, we will increase
+        the depth of tokens between `for` and `in`.
+        """
+        if leaf.type == token.NAME and leaf.value == 'for':
+            self.has_for = True
+            self.bracket_tracker.depth += 1
+            self._for_loop_variable = True
+            return True
+
+        return False
+
+    def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
+        # See `maybe_increment_for_loop_variable` above for explanation.
+        if self._for_loop_variable and leaf.type == token.NAME and leaf.value == 'in':
+            self.bracket_tracker.depth -= 1
+            self._for_loop_variable = False
+            return True
+
+        return False
+
     def maybe_adapt_standalone_comment(self, comment: Leaf) -> bool:
         """Hack a standalone comment to act as a trailing comment for line splitting.
 
index 68707498aefe2d0f3310526c0fbac668da617091..59e4211c8886a46fe4f4e5434ce2a086dc47ba8f 100644 (file)
@@ -63,6 +63,7 @@ str or None if (1 if True else 2) else str or bytes or None
 [((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
 {i: 0 for i in (1, 2, 3)}
 {i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}
+{k: v for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension}
 Python3 > Python2 > COBOL
 Life is Life
 call()
@@ -188,6 +189,10 @@ str or None if (1 if True else 2) else str or bytes or None
 [((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
 {i: 0 for i in (1, 2, 3)}
 {i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}
+{
+    k: v
+    for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
+}
 Python3 > Python2 > COBOL
 Life is Life
 call()