google / yamlfmt

An extensible command line tool or library to format yaml files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feat: supporting max line length

daisuke834 opened this issue · comments

Many projects has its own rule specifying a maximum line length of yaml file (e.g. 80), and, for example, yamllint supports a feature to configure it via this feature.
There are cases that a line has to be split into multiple lines li this in order to comply with the rule

      KEY: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

, but yamlfmt automatically formats it to

      KEY: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

, then the line violates the rule, and yamllint fails.
It would be helpful if yamlfmt can support a feature to specify the maximum line length in config file.

Thank you for the suggestion!

This one might be a new can of worms... I'm not sure if there's going to be a way to do this in a context-free way, and currently I rely on yaml.v3 for all the contextful parsing and encoding. I don't think I'll be able to come up with anything quickly, but I will try to think about a way to accomplish this.

Thanks for your consideration!

A quick fix for some use cases might be to ignore strings ending in a backslash. These are usually explicitly wrapped. yamlfmt should not unwrap them.

I've just seen that this is already the case in multi-line strings like

---
blah:
  foo: 
    - |-
      this is a \
          single line in a \
          multi-line string

This will not be unwrapped.

However this

---
blah:
  foo: 
    - this is a \
          single line in an actual \
              single-line string

... will. Unfortunately, unwrapping this line does not automatically remove the backslashes.

Hmmmm that's gonna be tricky. The yaml.v3 library is doing that part. Based on the spec I'm assuming they shouldn't be doing that automatically. That would be hard to fix in a context-free hack, since a lone \ wouldn't necessarily imply a new line (although I could try making the assumption that \ (with a space) would represent a new line... Not a huge fan of that though.

I've started the process of making a proper fork of yaml.v3, and once I've done that I can properly tackle this issue.

Opted to close this issue since it will be part of 0.7.0 later today. It may not be perfect, since I started with just exposing the setting in the library. If there's any use cases you find that aren't covered, please open a new issue with specific instructions and I can start to look into refining this in the library itself.

For the "busy" people:

formatter:
  type: basic
  indent: 4
  max_line_length: 999