diff --git a/CHANGES.md b/CHANGES.md index 128d84b8ff3..c727c286857 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,7 @@ +- Fix `fmt: skip` skipping the line after instead of the line it's on (#4855) - Remove unnecessary parentheses from the left-hand side of assignments while preserving magic trailing commas and intentional multiline formatting (#4865) - Fix `fix_fmt_skip_in_one_liners` crashing on `with` statements (#4853) diff --git a/src/black/comments.py b/src/black/comments.py index de1831e4d7c..ff83a0079c4 100644 --- a/src/black/comments.py +++ b/src/black/comments.py @@ -605,6 +605,10 @@ def _generate_ignored_nodes_from_fmt_skip( comments = list_comments(leaf.prefix, is_endmarker=False, mode=mode) if not comments or comment.value != comments[0].value: return + + if Preview.fix_fmt_skip_in_one_liners in mode and not prev_sibling and parent: + prev_sibling = parent.prev_sibling + if prev_sibling is not None: leaf.prefix = leaf.prefix[comment.consumed :] @@ -646,10 +650,20 @@ def _generate_ignored_nodes_from_fmt_skip( leaf_nodes = list(current_node.prev_sibling.leaves()) current_node = leaf_nodes[-1] if leaf_nodes else current_node + if ( + current_node.type in CLOSING_BRACKETS + and current_node.parent + and current_node.parent.type == syms.atom + ): + current_node = current_node.parent + if current_node.type in (token.NEWLINE, token.INDENT): current_node.prefix = "" break + if current_node.type == token.DEDENT: + break + # Special case for with expressions # Without this, we can stuck inside the asexpr_test's children's children if ( diff --git a/tests/data/cases/fmtskip10.py b/tests/data/cases/fmtskip10.py index 98bb843c63f..f4f4981eb26 100644 --- a/tests/data/cases/fmtskip10.py +++ b/tests/data/cases/fmtskip10.py @@ -11,3 +11,12 @@ def f(x: int): return x # fmt: skip while j < 10: j += 1 # fmt: skip b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip + +v = ( + foo_dict # fmt: skip + .setdefault("a", {}) + .setdefault("b", {}) + .setdefault("c", {}) + .setdefault("d", {}) + .setdefault("e", {}) +) diff --git a/tests/data/cases/fmtskip13.py b/tests/data/cases/fmtskip13.py new file mode 100644 index 00000000000..f3abcba9219 --- /dev/null +++ b/tests/data/cases/fmtskip13.py @@ -0,0 +1,64 @@ +# flags: --preview + +t = ( + {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip + {"foo": "bar"}, +) + +t = ( + { + "foo": "very long string", + "bar": "another very long string", + "baz": "we should run out of space by now", + }, # fmt: skip + {"foo": "bar"}, +) + + +t = ( + {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip + {"foo": "bar",}, +) + +t = ( + { + "foo": "very long string", + "bar": "another very long string", + "baz": "we should run out of space by now", + }, # fmt: skip + {"foo": "bar",}, +) + +# output +t = ( + {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip + {"foo": "bar"}, +) + +t = ( + { + "foo": "very long string", + "bar": "another very long string", + "baz": "we should run out of space by now", + }, # fmt: skip + {"foo": "bar"}, +) + + +t = ( + {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip + { + "foo": "bar", + }, +) + +t = ( + { + "foo": "very long string", + "bar": "another very long string", + "baz": "we should run out of space by now", + }, # fmt: skip + { + "foo": "bar", + }, +)