From: Yilei "Dolee" Yang Date: Thu, 15 Dec 2022 01:56:14 +0000 (-0800) Subject: Fix a crash when a colon line is marked between `# fmt: off` and `# fmt: on` (#3439) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/a2821815af5f5a706c673279d6405e286d6e95b8?ds=sidebyside Fix a crash when a colon line is marked between `# fmt: off` and `# fmt: on` (#3439) --- diff --git a/CHANGES.md b/CHANGES.md index f604035..1a7c320 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ +- Fix a crash when a colon line is marked between `# fmt: off` and `# fmt: on` (#3439) + ### Preview style diff --git a/src/black/comments.py b/src/black/comments.py index dce83ab..e733dcc 100644 --- a/src/black/comments.py +++ b/src/black/comments.py @@ -232,7 +232,7 @@ def generate_ignored_nodes( # fix for fmt: on in children if children_contains_fmt_on(container, preview=preview): - for child in container.children: + for index, child in enumerate(container.children): if isinstance(child, Leaf) and is_fmt_on(child, preview=preview): if child.type in CLOSING_BRACKETS: # This means `# fmt: on` is placed at a different bracket level @@ -241,6 +241,16 @@ def generate_ignored_nodes( # The alternative is to fail the formatting. yield child return + if ( + child.type == token.INDENT + and index < len(container.children) - 1 + and children_contains_fmt_on( + container.children[index + 1], preview=preview + ) + ): + # This means `# fmt: on` is placed right after an indentation + # level, and we shouldn't swallow the previous INDENT token. + return if children_contains_fmt_on(child, preview=preview): return yield child diff --git a/tests/data/simple_cases/fmtonoff5.py b/tests/data/simple_cases/fmtonoff5.py index 71b1381..181151b 100644 --- a/tests/data/simple_cases/fmtonoff5.py +++ b/tests/data/simple_cases/fmtonoff5.py @@ -64,7 +64,7 @@ class A: print ( "This will be formatted" ) -# Regression test for https://github.com/psf/black/issues/2985 +# Regression test for https://github.com/psf/black/issues/2985. class Named(t.Protocol): # fmt: off @property @@ -75,6 +75,15 @@ class Factory(t.Protocol): # fmt: on +# Regression test for https://github.com/psf/black/issues/3436. +if x: + return x +# fmt: off +elif unformatted: +# fmt: on + will_be_formatted () + + # output @@ -144,7 +153,7 @@ class A: print("This will be formatted") -# Regression test for https://github.com/psf/black/issues/2985 +# Regression test for https://github.com/psf/black/issues/2985. class Named(t.Protocol): # fmt: off @property @@ -156,3 +165,12 @@ class Factory(t.Protocol): ... # fmt: on + + +# Regression test for https://github.com/psf/black/issues/3436. +if x: + return x +# fmt: off +elif unformatted: + # fmt: on + will_be_formatted()