google / google/yamlfmt

Feature request: Add `preserve_comment_indents` option to keep manual comment column alignment

Open
#290 2 comments 6 reactions 0 assignees View on GitHub
yaml_v3_problem
Dominant language
Go
Stars
1.8k
Forks
75
PR merge metrics
No merged PRs in 30d

Description

### yamlfmt version
```
yamlfmt 0.20.0 (Homebrew)
```

### Is your feature request related to a problem? Please describe.

Yes. Currently `yamlfmt` always normalizes the spacing before line comments according to the `pad_line_comments` setting.
When developers manually align comments in columns (a very common practice in configuration files, Ansible roles, Kubernetes manifests, documentation-style YAML, etc.), this alignment is completely destroyed.

Example:

```yaml
# Before formatting (nicely aligned at column 50)
database_host: 127.0.0.1 # primary server
database_port: 5432 # default port
cache_enabled: true # enable caching
debug_mode: false # disabled in production
```

```yaml
# After yamlfmt – alignment is lost
database_host: 127.0.0.1 # primary server
database_port: 5432 # default port
cache_enabled: true # enable caching
debug_mode: false # disabled in production
```

Even setting `pad_line_comments` to a high value does not preserve large manual indents – the formatter still collapses them.

### Describe the solution you’d like

Add a new boolean option in the `basic` formatter:

```yaml
preserve_comment_indents: true|false # default: false
```

When `true`: Indentation before `#` in line comments is preserved exactly as in the original file.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.