diff --git a/polymath_code_standard/yaml_format.py b/polymath_code_standard/yaml_format.py index 0eec21a..f063aaf 100644 --- a/polymath_code_standard/yaml_format.py +++ b/polymath_code_standard/yaml_format.py @@ -323,7 +323,10 @@ def format_yaml(source: str, explicit_start: bool = True) -> str: if not source: return source matrices = detect_matrices(source) - fixed = fix_code(source, _yamlfix_config(explicit_start=explicit_start)) + source_has_explicit_start = source.startswith('---\n') + fixed = fix_code(source, _yamlfix_config(explicit_start=True)) + if not explicit_start and not source_has_explicit_start and fixed.startswith('---\n'): + fixed = fixed[4:] if matrices: fixed = apply_matrices(fixed, matrices) return _strip_trailing_whitespace(fixed) diff --git a/tests/test_yaml_format.py b/tests/test_yaml_format.py index 5d80989..3b11f5a 100644 --- a/tests/test_yaml_format.py +++ b/tests/test_yaml_format.py @@ -104,6 +104,15 @@ def test_adds_document_start(self): result = format_yaml('key: value\n') assert result.startswith('---\n') + def test_no_explicit_start_preserves_root_sequence_indentation(self): + source = textwrap.dedent("""\ + - name: first + enabled: true + - name: second + enabled: false + """) + assert format_yaml(source, explicit_start=False) == source + def test_true_normalised(self): result = format_yaml('enabled: True\n') assert 'enabled: true' in result