Skip to content

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

Description

@d-enk

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:

# 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
# 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:

preserve_comment_indents: true|false   # default: false

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    yaml_v3_problemA bug in the underlying yaml library. These issues are vastly harder to fix.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions