From 5c443bfc1f35a6b9887106877bacdc92bc8cbc10 Mon Sep 17 00:00:00 2001 From: Roman Khatsiev Date: Mon, 30 Mar 2020 11:41:23 +0300 Subject: [PATCH 1/3] initial fix --- black.py | 28 ++++++++++++++++++++++++++-- tests/data/fmtonoff4.py | 31 +++++++++++++++++++++++++++++++ tests/test_black.py | 8 ++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tests/data/fmtonoff4.py diff --git a/black.py b/black.py index 31859d1a499..2d8f9127a06 100644 --- a/black.py +++ b/black.py @@ -3118,9 +3118,33 @@ def generate_ignored_nodes(leaf: Leaf) -> Iterator[LN]: if is_fmt_on: return - yield container + # fix for fmt: on in children + if contains_fmt_on_at_column(container, leaf.column): + for child in container.children: + if not contains_fmt_on_at_column(child, leaf.column): + yield child + return + else: + yield container + container = container.next_sibling + + +def contains_fmt_on_at_column(container, column): + for child in container.children: + if (isinstance(child, Node) and first_leaf_column(child) == column + or isinstance(child, Leaf) and child.column == column): + for comment in list_comments(child.prefix, is_endmarker=False): + if comment.value in FMT_ON: + return True - container = container.next_sibling + return False + + +def first_leaf_column(node): + for child in node.children: + if isinstance(child, Leaf): + return child.column + return None def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool: diff --git a/tests/data/fmtonoff4.py b/tests/data/fmtonoff4.py new file mode 100644 index 00000000000..54673c06b2d --- /dev/null +++ b/tests/data/fmtonoff4.py @@ -0,0 +1,31 @@ +# fmt: off +@test([ + 1, 2, + 3, 4, +]) +# fmt: on +def f(): pass + +@test([ + 1, 2, + 3, 4, +]) +def f(): pass + +# output + +# fmt: off +@test([ + 1, 2, + 3, 4, +]) +# fmt: on +def f(): + pass + + +@test( + [1, 2, 3, 4,] +) +def f(): + pass diff --git a/tests/test_black.py b/tests/test_black.py index acbaade0a50..790416c3e3e 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -632,6 +632,14 @@ def test_fmtonoff3(self) -> None: black.assert_equivalent(source, actual) black.assert_stable(source, actual, black.FileMode()) + @patch("black.dump_to_file", dump_to_stderr) + def test_fmtonoff4(self) -> None: + source, expected = read_data("fmtonoff4") + 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_remove_empty_parentheses_after_class(self) -> None: source, expected = read_data("class_blank_parentheses") From 241b37479eda1289d598ee463ab8339ad6083eb0 Mon Sep 17 00:00:00 2001 From: Roman Khatsiev Date: Sun, 5 Apr 2020 21:57:42 +0300 Subject: [PATCH 2/3] fix broken tests --- black.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/black.py b/black.py index 2d8f9127a06..90acba9a24a 100644 --- a/black.py +++ b/black.py @@ -3109,33 +3109,40 @@ def generate_ignored_nodes(leaf: Leaf) -> Iterator[LN]: """ container: Optional[LN] = container_of(leaf) while container is not None and container.type != token.ENDMARKER: - is_fmt_on = False - for comment in list_comments(container.prefix, is_endmarker=False): - if comment.value in FMT_ON: - is_fmt_on = True - elif comment.value in FMT_OFF: - is_fmt_on = False - if is_fmt_on: + if fmt_on(container): return # fix for fmt: on in children if contains_fmt_on_at_column(container, leaf.column): for child in container.children: - if not contains_fmt_on_at_column(child, leaf.column): - yield child - return + if contains_fmt_on_at_column(child, leaf.column): + return + yield child else: yield container container = container.next_sibling +def fmt_on(container): + is_fmt_on = False + for comment in list_comments(container.prefix, is_endmarker=False): + if comment.value in FMT_ON: + is_fmt_on = True + elif comment.value in FMT_OFF: + is_fmt_on = False + return is_fmt_on + + def contains_fmt_on_at_column(container, column): for child in container.children: - if (isinstance(child, Node) and first_leaf_column(child) == column - or isinstance(child, Leaf) and child.column == column): - for comment in list_comments(child.prefix, is_endmarker=False): - if comment.value in FMT_ON: - return True + if ( + isinstance(child, Node) + and first_leaf_column(child) == column + or isinstance(child, Leaf) + and child.column == column + ): + if fmt_on(child): + return True return False From f89ad0e0c841910d1b3e7cc9661312d372b560c1 Mon Sep 17 00:00:00 2001 From: Roman Khatsiev Date: Sun, 5 Apr 2020 22:40:15 +0300 Subject: [PATCH 3/3] fix linter errors --- black.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/black.py b/black.py index 90acba9a24a..539bf9bb9d7 100644 --- a/black.py +++ b/black.py @@ -3123,7 +3123,7 @@ def generate_ignored_nodes(leaf: Leaf) -> Iterator[LN]: container = container.next_sibling -def fmt_on(container): +def fmt_on(container: LN) -> bool: is_fmt_on = False for comment in list_comments(container.prefix, is_endmarker=False): if comment.value in FMT_ON: @@ -3133,7 +3133,7 @@ def fmt_on(container): return is_fmt_on -def contains_fmt_on_at_column(container, column): +def contains_fmt_on_at_column(container: LN, column: int) -> bool: for child in container.children: if ( isinstance(child, Node) @@ -3147,7 +3147,7 @@ def contains_fmt_on_at_column(container, column): return False -def first_leaf_column(node): +def first_leaf_column(node: Node) -> Optional[int]: for child in node.children: if isinstance(child, Leaf): return child.column