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

fix bracket match bug (#470)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Thu, 23 Aug 2018 11:52:07 +0000 (04:52 -0700)
committerZsolt Dollenstein <zsol.zsol@gmail.com>
Thu, 23 Aug 2018 11:52:07 +0000 (12:52 +0100)
* fix bracket match bug

* add missing test file

black.py
tests/data/bracketmatch.py [new file with mode: 0644]
tests/test_black.py

index 85cb45bded5cd7794d6d69bc2f4adea22aa10dda..0f166c61494bee270191c68d2ae3accfc90aa0a2 100644 (file)
--- a/black.py
+++ b/black.py
@@ -877,8 +877,8 @@ class BracketTracker:
     bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
     delimiters: Dict[LeafID, Priority] = Factory(dict)
     previous: Optional[Leaf] = None
-    _for_loop_variable: int = 0
-    _lambda_arguments: int = 0
+    _for_loop_depths: List[int] = Factory(list)
+    _lambda_argument_depths: List[int] = Factory(list)
 
     def mark(self, leaf: Leaf) -> None:
         """Mark `leaf` with bracket-related metadata. Keep track of delimiters.
@@ -951,16 +951,21 @@ class BracketTracker:
         """
         if leaf.type == token.NAME and leaf.value == "for":
             self.depth += 1
-            self._for_loop_variable += 1
+            self._for_loop_depths.append(self.depth)
             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":
+        if (
+            self._for_loop_depths
+            and self._for_loop_depths[-1] == self.depth
+            and leaf.type == token.NAME
+            and leaf.value == "in"
+        ):
             self.depth -= 1
-            self._for_loop_variable -= 1
+            self._for_loop_depths.pop()
             return True
 
         return False
@@ -973,16 +978,20 @@ class BracketTracker:
         """
         if leaf.type == token.NAME and leaf.value == "lambda":
             self.depth += 1
-            self._lambda_arguments += 1
+            self._lambda_argument_depths.append(self.depth)
             return True
 
         return False
 
     def maybe_decrement_after_lambda_arguments(self, leaf: Leaf) -> bool:
         """See `maybe_increment_lambda_arguments` above for explanation."""
-        if self._lambda_arguments and leaf.type == token.COLON:
+        if (
+            self._lambda_argument_depths
+            and self._lambda_argument_depths[-1] == self.depth
+            and leaf.type == token.COLON
+        ):
             self.depth -= 1
-            self._lambda_arguments -= 1
+            self._lambda_argument_depths.pop()
             return True
 
         return False
diff --git a/tests/data/bracketmatch.py b/tests/data/bracketmatch.py
new file mode 100644 (file)
index 0000000..0aaa2d8
--- /dev/null
@@ -0,0 +1,15 @@
+for ((x in {}) or {})['a'] in x:
+    pass
+pem_spam = lambda l, spam = {
+    "x": 3
+}: not spam.get(l.strip())
+lambda x=lambda y={1: 3}: y['x':lambda y: {1: 2}]: x
+
+
+# output
+
+
+for ((x in {}) or {})["a"] in x:
+    pass
+pem_spam = lambda l, spam={"x": 3}: not spam.get(l.strip())
+lambda x=lambda y={1: 3}: y["x" : lambda y: {1: 2}]: x
index 9c798caa1c3169b6792f8d663ff41a97df345e10..311f63563b9666765712a78bec994222fe8465df 100644 (file)
@@ -453,6 +453,14 @@ class BlackTestCase(unittest.TestCase):
         black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, line_length=ll)
 
+    @patch("black.dump_to_file", dump_to_stderr)
+    def test_bracket_match(self) -> None:
+        source, expected = read_data("bracketmatch")
+        actual = fs(source)
+        self.assertFormatEqual(expected, actual)
+        black.assert_equivalent(source, actual)
+        black.assert_stable(source, actual, line_length=ll)
+
     def test_report_verbose(self) -> None:
         report = black.Report(verbose=True)
         out_lines = []