Skip to content

Commit

Permalink
Fix indentation change to comments when using --fix (#4273)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavcrosby authored Aug 2, 2024
1 parent 38c31e0 commit 2dd2336
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
env:
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests.
PYTEST_REQPASS: 887
PYTEST_REQPASS: 888
steps:
- uses: actions/checkout@v4
with:
Expand Down
5 changes: 5 additions & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,11 @@ def _post_process_yaml(

full_line_comments.append((i, stripped))
elif full_line_comments:
# match ident of previous non-blank line
if not lines[i - 1].lstrip():
prev = lines[i - 2]
space_length = len(prev) - len(prev.lstrip())

# end of full line comments so adjust to match indent of this line
spaces = " " * space_length
for index, comment in full_line_comments:
Expand Down
6 changes: 6 additions & 0 deletions test/test_yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ def load_yaml_formatting_fixtures(fixture_filename: str) -> tuple[str, str, str]
pytest.param("---\nfoo: YES\n", "---\nfoo: true\n", (1, 1), id="9"),
pytest.param("---\nfoo: YES\n", "---\nfoo: YES\n", (1, 2), id="10"),
pytest.param("---\nfoo: YES\n", "---\nfoo: YES\n", None, id="11"),
pytest.param(
"---\n # quoted-strings:\n # quote-type: double\n # required: only-when-needed\n\nignore:\n - secrets.yml\n",
"---\n # quoted-strings:\n # quote-type: double\n # required: only-when-needed\n\nignore:\n - secrets.yml\n",
None,
id="12",
),
),
)
def test_fmt(before: str, after: str, version: tuple[int, int] | None) -> None:
Expand Down

0 comments on commit 2dd2336

Please sign in to comment.