Commit 23fec8b0 authored by Lawrence Chan's avatar Lawrence Chan Committed by Łukasz Langa
Browse files

Fix fmt on/off when multiple exist in leaf prefix (#1086)

The old behavior would detect the existence of a `# fmt: on` in a leaf
node's comment prefix and immediately mark the node as formatting-on,
even if a subsequent `# fmt: off` in the same comment prefix would turn
it back off. This change modifies that logic to track the state through
the entire prefix and take the final state.

Note that this does not fully solve on/off behavior, since any _comment_
lines between the off/on are still formatted. We may need to add
virtual leaf nodes to truly solve that. I will leave that for a separate
commit/PR.

Fixes #1005
parent f99fad1b
Loading
Loading
Loading
Loading
+5 −0
Changes for black.py: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3056,8 +3056,13 @@ 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:
            return

        yield container
+12 −4
Changes for tests/data/fmtonoff.py: 12 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -108,9 +108,13 @@ def on_and_off_broken():
    # fmt: on
    # fmt: off
    this=should.not_be.formatted()
    but=it  is  formatted
    and_=indeed . it  is  not  formatted
    because . the . handling . inside . generate_ignored_nodes()
    doesnt . consider . ordering . within . one . prefix
    now . considers . multiple . fmt . directives . within . one . prefix
    # fmt: on
    # fmt: off
        # ...but comments still get reformatted even though they should not be
    # fmt: on
def long_lines():
    if True:
        typedargslist.extend(
@@ -319,9 +323,13 @@ def on_and_off_broken():
    # fmt: on
    # fmt: off
    this=should.not_be.formatted()
    but = it is formatted
    and_=indeed . it  is  not  formatted
    because . the . handling . inside . generate_ignored_nodes()
    doesnt.consider.ordering.within.one.prefix
    now . considers . multiple . fmt . directives . within . one . prefix
    # fmt: on
    # fmt: off
    # ...but comments still get reformatted even though they should not be
    # fmt: on


def long_lines():
+35 −0
Changes for tests/data/fmtonoff3.py: 35 added lines, 0 removed lines.
Original line number Diff line number Diff line
# fmt: off
x = [
    1, 2,
    3, 4,
]
# fmt: on

# fmt: off
x = [
    1, 2,
    3, 4,
]
# fmt: on

x = [
    1, 2, 3, 4
]

# output

# fmt: off
x = [
    1, 2,
    3, 4,
]
# fmt: on

# fmt: off
x = [
    1, 2,
    3, 4,
]
# fmt: on

x = [1, 2, 3, 4]
+8 −0
Changes for tests/test_black.py: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -614,6 +614,14 @@ def test_fmtonoff2(self) -> None:
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, black.FileMode())

    @patch("black.dump_to_file", dump_to_stderr)
    def test_fmtonoff3(self) -> None:
        source, expected = read_data("fmtonoff3")
        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")