Skip to content

Commit

Permalink
Respect whitelines higher than section_whitelines
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhoyer committed Jul 1, 2023
1 parent 170bf93 commit 614e4dc
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Expand Up @@ -258,7 +258,7 @@ section:
key: value
```
* Sections at the start and end of the document will have whitelines removed before and after respectively.

* If `whitelines` parameter is higher than `section_whitelines`, the rule will preserve the amount of whitespaces specified by `whitelines` parameter.

### Config Path

Expand Down
18 changes: 15 additions & 3 deletions src/yamlfix/adapters.py
Expand Up @@ -658,27 +658,39 @@ def _replace_whitelines(match: Match[str], n_whitelines: int) -> str:

def _fix_section_whitelines(self, source_code: str) -> str:
re_section = "\n*(^#.*\n)*\n*^[^ ].*:\n(\n|(^ .*))+\n*"

# Match the first --- or start of the string \A
# See: https://docs.python.org/3.9/library/re.html#regular-expression-syntax
re_beginning_section = f"(?P<b>(?:---\n|\\A){re_section})"
re_normal_section = f"(?P<s>{re_section})"
re_full = f"{re_beginning_section}|{re_normal_section}"
pattern = re.compile(re_full, flags=re.MULTILINE)
n_whitelines = self.config.whitelines
n_section_whitelines = self.config.section_whitelines

def _fix_before_section(match: Match[str]) -> str:
whitelines = n_section_whitelines
section = match.group("s")
if not section:
return match.group()
if n_whitelines > n_section_whitelines and section.startswith(
"\n" + n_whitelines * "\n"
):
whitelines = n_whitelines
while section[0] == "\n":
section = section[1:]
out = "\n" * (self.config.section_whitelines + 1) + section
return out
return "\n" * (whitelines + 1) + section

def _fix_after_section(match: Match[str]) -> str:
whitelines = n_section_whitelines
section = match.group("b") or match.group("s")
if n_whitelines > n_section_whitelines and section.endswith(
"\n\n" + n_whitelines * "\n"
):
whitelines = n_whitelines
while section[-1] == "\n":
section = section[:-1]
return section + "\n" * (self.config.section_whitelines + 1)
return section + "\n" * (whitelines + 1)

before_fixed = pattern.sub(repl=_fix_before_section, string=source_code)
after_fixed = pattern.sub(repl=_fix_after_section, string=before_fixed)
Expand Down
77 changes: 77 additions & 0 deletions tests/unit/test_adapter_yaml.py
Expand Up @@ -885,6 +885,7 @@ def test_whitelines_adjusted_to_value(self) -> None:
"""\
---
key: value
dict:
key: value
Expand All @@ -902,6 +903,82 @@ def test_whitelines_adjusted_to_value(self) -> None:

assert result == fixed_source

def test_whitelines_higher_than_secion_whitelines(self) -> None:
"""Checks that amount of whitelines are in line with the config values."""
source = dedent(
# pylint: disable=C0303
"""\
---
begin_section:
key: value
key1: value
key2: value
happy_path_section:
key1: value
key2: value
nested_dict:
nested_key: value
# Comment 1
# Comment 2
comment_section:
key: value
key3: value
key4: value
key5: value
close_section:
key: value
""" # noqa: W291
)
fixed_source = dedent(
"""\
---
begin_section:
key: value
key1: value
key2: value
happy_path_section:
key1: value
key2: value
nested_dict:
nested_key: value
# Comment 1
# Comment 2
comment_section:
key: value
key3: value
key4: value
key5: value
close_section:
key: value
"""
)
config = YamlfixConfig()
config.whitelines = 1
config.section_whitelines = 2

result = fix_code(source, config)

assert result == fixed_source

def test_enforcing_flow_style_together_with_adjustable_newlines(self) -> None:
"""Checks that transforming block style sequences to flow style together with
newlines adjusting produces correct result.
Expand Down

0 comments on commit 614e4dc

Please sign in to comment.