Skip to content

Commit

Permalink
Merge pull request #56 from lyz-code/fix/comments-in-lists
Browse files Browse the repository at this point in the history
Fix/comments in lists
  • Loading branch information
lyz-code authored Feb 5, 2021
2 parents 4173c15 + 2191e71 commit a040758
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- id: yamlfix
name: yamlfix
entry: yamlfix
require_serial: true
language: python
language_version: python3
types_or: [cython, pyi, python]
args: []
minimum_pre_commit_version: 2.9.0
22 changes: 20 additions & 2 deletions docs/editor_integration.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
For a smother experience, you can run `yamlfix` each time you save your file
in your editor.
For a smoother experience, you can run `yamlfix` automatically each time each
time you save your file in your editor or when you run `git commit`.

# Vim

Expand All @@ -12,3 +12,21 @@ plugin](https://github.com/dense-analysis/ale).
post](https://lyz-code.github.io/blue-book/linux/vim/vim_plugins/#ale).

`ale` is configured to run `yamlfix` automatically by default.

# [pre-commit](https://pre-commit.com/)

You can run `yamlfix` before we do a commit using the
[pre-commit](https://pre-commit.com/) framework. If you don't know how to use
it, follow [these
guidelines](https://lyz-code.github.io/blue-book/devops/ci/#configuring-pre-commit).

You'll need to add the following lines to your project's
`.pre-commit-config.yaml` file.

```yaml
repos:
- repo: https://github.com/lyz-code/yamlfix/
rev: master
hooks:
- id: yamlfix
```
6 changes: 5 additions & 1 deletion src/yamlfix/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ def _fix_top_level_lists(source_code: str) -> str:
# Remove the indentation from the line
fixed_source_lines.append(re.sub(rf"^{indent}(.*)", r"\1", line))
elif is_top_level_list:
fixed_source_lines.append(re.sub(rf"^{indent}(.*)", r"\1", line))
# ruyaml doesn't change the indentation of comments
if re.match(r"\s*#.*", line):
fixed_source_lines.append(line)
else:
fixed_source_lines.append(re.sub(rf"^{indent}(.*)", r"\1", line))
else:
return source_code

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ def test_fix_code_respects_parent_lists_with_comments() -> None:
assert result == source


def test_fix_code_preserves_indented_comments() -> None:
"""Don't remove indentation from comments in the code."""
source = dedent(
"""\
---
- program:
# Keep comments!"""
)

result = fix_code(source)

assert result == source


def test_fix_code_removes_extra_apostrophes() -> None:
"""Remove not needed apostrophes."""
source = dedent(
Expand Down

0 comments on commit a040758

Please sign in to comment.