From 3c7f56f5825d5d6c1971bf76d453752a722c5e58 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Mon, 27 Oct 2025 11:34:29 +0100 Subject: [PATCH] Restore `indent.py` (#21094) --- .../test/fixtures/ruff/fmt_on_off/indent.py | 55 +++++ .../format@fmt_on_off__indent.py.snap | 226 ++++++++++++++++++ 2 files changed, 281 insertions(+) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/indent.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/indent.py index e69de29bb2..c0cb6c1849 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/indent.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off/indent.py @@ -0,0 +1,55 @@ +def test(): + # fmt: off + a_very_small_indent + ( +not_fixed + ) + + if True: + pass + more + # fmt: on + + formatted + + def test(): + a_small_indent + # fmt: off +# fix under-indented comments + (or_the_inner_expression + +expressions + ) + + if True: + pass + # fmt: on + + +# fmt: off +def test(): + pass + + # It is necessary to indent comments because the following fmt: on comment because it otherwise becomes a trailing comment + # of the `test` function if the "proper" indentation is larger than 2 spaces. + # fmt: on + +disabled + formatting; + +# fmt: on + +formatted; + +def test(): + pass + # fmt: off + """A multiline strings + that should not get formatted""" + + "A single quoted multiline \ + string" + + disabled + formatting; + +# fmt: on + +formatted; diff --git a/crates/ruff_python_formatter/tests/snapshots/format@fmt_on_off__indent.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@fmt_on_off__indent.py.snap index 3a68d9ceae..b1c58a7f63 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@fmt_on_off__indent.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@fmt_on_off__indent.py.snap @@ -4,6 +4,61 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/fmt_on_off --- ## Input ```python +def test(): + # fmt: off + a_very_small_indent + ( +not_fixed + ) + + if True: + pass + more + # fmt: on + + formatted + + def test(): + a_small_indent + # fmt: off +# fix under-indented comments + (or_the_inner_expression + +expressions + ) + + if True: + pass + # fmt: on + + +# fmt: off +def test(): + pass + + # It is necessary to indent comments because the following fmt: on comment because it otherwise becomes a trailing comment + # of the `test` function if the "proper" indentation is larger than 2 spaces. + # fmt: on + +disabled + formatting; + +# fmt: on + +formatted; + +def test(): + pass + # fmt: off + """A multiline strings + that should not get formatted""" + + "A single quoted multiline \ + string" + + disabled + formatting; + +# fmt: on + +formatted; ``` ## Outputs @@ -23,6 +78,63 @@ source_type = Python ``` ```python +def test(): + # fmt: off + a_very_small_indent + ( +not_fixed + ) + + if True: + pass + more + # fmt: on + + formatted + + def test(): + a_small_indent + # fmt: off + # fix under-indented comments + (or_the_inner_expression + +expressions + ) + + if True: + pass + # fmt: on + + +# fmt: off +def test(): + pass + + # It is necessary to indent comments because the following fmt: on comment because it otherwise becomes a trailing comment + # of the `test` function if the "proper" indentation is larger than 2 spaces. + # fmt: on + +disabled + formatting; + +# fmt: on + +formatted + + +def test(): + pass + # fmt: off + """A multiline strings + that should not get formatted""" + + "A single quoted multiline \ + string" + + disabled + formatting; + + +# fmt: on + +formatted ``` @@ -42,6 +154,63 @@ source_type = Python ``` ```python +def test(): + # fmt: off + a_very_small_indent + ( +not_fixed + ) + + if True: + pass + more + # fmt: on + + formatted + + def test(): + a_small_indent + # fmt: off + # fix under-indented comments + (or_the_inner_expression + +expressions + ) + + if True: + pass + # fmt: on + + +# fmt: off +def test(): + pass + + # It is necessary to indent comments because the following fmt: on comment because it otherwise becomes a trailing comment + # of the `test` function if the "proper" indentation is larger than 2 spaces. + # fmt: on + +disabled + formatting; + +# fmt: on + +formatted + + +def test(): + pass + # fmt: off + """A multiline strings + that should not get formatted""" + + "A single quoted multiline \ + string" + + disabled + formatting; + + +# fmt: on + +formatted ``` @@ -61,4 +230,61 @@ source_type = Python ``` ```python +def test(): + # fmt: off + a_very_small_indent + ( +not_fixed + ) + + if True: + pass + more + # fmt: on + + formatted + + def test(): + a_small_indent + # fmt: off + # fix under-indented comments + (or_the_inner_expression + +expressions + ) + + if True: + pass + # fmt: on + + +# fmt: off +def test(): + pass + + # It is necessary to indent comments because the following fmt: on comment because it otherwise becomes a trailing comment + # of the `test` function if the "proper" indentation is larger than 2 spaces. + # fmt: on + +disabled + formatting; + +# fmt: on + +formatted + + +def test(): + pass + # fmt: off + """A multiline strings + that should not get formatted""" + + "A single quoted multiline \ + string" + + disabled + formatting; + + +# fmt: on + +formatted ```