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.
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
"""
container: Optional[LN] = container_of(leaf)
while container is not None and container.type != token.ENDMARKER:
"""
container: Optional[LN] = container_of(leaf)
while container is not None and container.type != token.ENDMARKER:
for comment in list_comments(container.prefix, is_endmarker=False):
if comment.value in FMT_ON:
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
# fmt: on
# fmt: off
this=should.not_be.formatted()
# fmt: on
# fmt: off
this=should.not_be.formatted()
+ and_=indeed . it is not formatted
because . the . handling . inside . generate_ignored_nodes()
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(
def long_lines():
if True:
typedargslist.extend(
"""Another known limitation."""
# fmt: on
# fmt: off
"""Another known limitation."""
# fmt: on
# fmt: off
- this = should.not_be.formatted()
- but = it is formatted
- because.the.handling.inside.generate_ignored_nodes()
- doesnt.consider.ordering.within.one.prefix
+ this=should.not_be.formatted()
+ and_=indeed . it is not formatted
+ because . the . handling . inside . generate_ignored_nodes()
+ 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
--- /dev/null
+# 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]
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())
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")
@patch("black.dump_to_file", dump_to_stderr)
def test_remove_empty_parentheses_after_class(self) -> None:
source, expected = read_data("class_blank_parentheses")