google / yamlfmt

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: allow to retain some empty lines

doublethink13 opened this issue Β· comments

Imagine I have this file:

---
# https://taskfile.dev/styleguide/

version: "3"

env:
  PYTHON_VERSION: "3.11.2"

tasks:
  setup-python-interpreter:
    cmds:
      - pyenv install --skip-existing {{.PYTHON_VERSION}}
      - pyenv local {{.PYTHON_VERSION}}
    desc: uses pyenv to locally install python
  
  setup-venv:
    cmds:
      - python -m venv venv
    desc: sets up python venv

As far as I understand, I can do one of two things:

  • Either set retain_line_breaks to true, which means I could end up with extra lines even after formatting, for example:
---
# https://taskfile.dev/styleguide/





version: "3"

env:
  PYTHON_VERSION: "3.11.2"

tasks:
  setup-python-interpreter:
    cmds:
      - pyenv install --skip-existing {{.PYTHON_VERSION}}
      - pyenv local {{.PYTHON_VERSION}}
    desc: uses pyenv to locally install python



  setup-venv:
    cmds:
      - python -m venv venv
    desc: sets up python venv
  • Or I could set retain_line_breaks to false, which means I only end up with:
---
# https://taskfile.dev/styleguide/
version: "3"
env:
  PYTHON_VERSION: "3.11.2"
tasks:
  setup-python-interpreter:
    cmds:
      - pyenv install --skip-existing {{.PYTHON_VERSION}}
      - pyenv local {{.PYTHON_VERSION}}
    desc: uses pyenv to locally install python
  setup-venv:
    cmds:
      - python -m venv venv
    desc: sets up python venv

I would like to be able to set retain_line_breaks to a number, for example, 1, so that I can have a clear separation of one line, but never more, like the Taskfile style guide suggests, and I personally agree.

If this is something relevant I could try to have a look if you are accepting contributors πŸ™‚

Thanks for the feature request!

This might be doable; in internal/hotfix is where the hack for retaining line breaks is. It might work by just adding something that chomps continuous line breaks down to some minimum, so if there's 3 line breaks in a row then it only replaces it with one magic string.

If you are interested in contributing this I would be happy to accept it! If the PR contains the following things:

  • The functionality like I mentioned above
  • A new configuration parameter for this (can't mess with retain_line_breaks itself as that would be a breaking change; would need to be an additional option users can set)
  • An integration test, which would involve making a new folder testing this functionality with the new configuration option. The integration tests are new so you would be the first person other than me to use them, you can let me know if you run into any problems.

That would be great if you are willing to contribute this, and you can let me know if you run into any problems.

Thanks for the reply and the information! I'll try to tackle this and I'll let you know πŸ™‚

Hey @doublethink13 I am going to implement this as part of v0.11.0 since I got a couple direct requests for it. Hopefully I don't steal your thunder if you had something going.

hey @braydonk

sure go ahead πŸ˜‰

i'm very sorry, life got in the way and eventually i forgot about this πŸ™

That's no problem, it happens. I should be getting this together before next week!