vscode-yaml icon indicating copy to clipboard operation
vscode-yaml copied to clipboard

Add Config for Array Indentation Spacing

Open codytwinton opened this issue 6 years ago • 41 comments

I'm working with Jekyll for GitHub Pages, which uses the Ruby gem for parsing YAML. This gem prefers this spacing in Arrays:

---
simple_val: Testing 1
object_val:
  sub_object_val: Testing 2
array_val:
- Testing 3
- Testing 4

However, when auto-formatter is on, this gets converted to:

---
simple_val: Testing 1
object_val:
  sub_object_val: Testing 2
array_val:
  - Testing 3
  - Testing 4

Regardless of preference, it would be great to have an option to disable adding space indention for arrays.

codytwinton avatar Mar 27 '19 06:03 codytwinton

Hi, any plans to add this config?

sledoux avatar Aug 27 '19 19:08 sledoux

+1 on this one

CermakM avatar Mar 17 '20 15:03 CermakM

This all depends on prettier adding this configuration or seeing if they have some api that allows to change array configuration.

JPinkney avatar Mar 17 '20 15:03 JPinkney

This all depends on prettier adding this configuration or seeing if they have some api that allows to change array configuration.

And per https://github.com/prettier/prettier/issues/4723, Prettier is decided to stay at do indent in mapping.

zerda avatar Mar 18 '20 23:03 zerda

+1 for this, my dev teams preferred option is not to indent

telb99 avatar Apr 07 '20 15:04 telb99

+1 for this. :)

nlamirault avatar Jun 04 '20 11:06 nlamirault

Also need this option

shamil avatar Jun 18 '20 08:06 shamil

Another +1

alchemistake avatar Feb 10 '21 10:02 alchemistake

Also an issue when using kustomize edit command (See https://github.com/kubernetes-sigs/kustomize/issues/1395), as it re-indents the file with similar preferences as Jekyll (removing the two additional spaces added by this plugin for an array).

This means that currently, the VS Code formatting plugin and the kustomize edit can't easily be used together.

smyrman avatar Apr 20 '21 13:04 smyrman

This all depends on prettier adding this configuration or seeing if they have some api that allows to change array configuration.

And per prettier/prettier#4723, Prettier is decided to stay at do indent in mapping.

Maybe a workable option could be to allow changing the formatter plugin? E.g. yamlfmt (CLI wrapper for https://sourceforge.net/projects/ruamel-yaml/)? Don't know if the exact tool is a good suggestion or not.

smyrman avatar Apr 21 '21 11:04 smyrman

Also an issue when using kustomize edit command

Same for the YAML files edited and committed by Flux v2 image-automation-controller. They've pinpointed it to the behaviour of the underlying library (kyaml) that is also used by kustomize.

yamllint has a configurable setting for sequences indentation (indent-sequences: false), so using it mitigates the problem a bit. It's just a linter though so can only be used to catch the wrong formatting after the fact.

Prettier's decision to enforce a particular style of sequence indentation is questionable to be honest. Automated tools aside, there are plenty of YAML files using non-indented sequences, particularly in the Kubernetes ecosystem.

sparhomenko avatar Apr 21 '21 12:04 sparhomenko

Same issue here +1

ludusrusso avatar Apr 27 '21 12:04 ludusrusso

So, I gave it a try and despite Prettier community deciding against using this style, I hope they would agree to make this an option. Please support my PR https://github.com/prettier/prettier/pull/10927

Once/if the PR merges and releases we can just simply update prettier and enable the flag here. :slightly_smiling_face: 2. (https://github.com/redhat-developer/vscode-yaml/pull/519, https://github.com/redhat-developer/yaml-language-server/pull/471)

tumido avatar May 22 '21 16:05 tumido

+1 for this.

AlexZhenWang avatar Jun 21 '21 04:06 AlexZhenWang

@tumido great work, I'd love to see your PR being merged into prettier. But as @thorn0 said, prettier is an opinionated formatter that isn't intended to be flexible, avoiding support for too many options. Looks like your https://github.com/prettier/prettier/pull/10927 isn't likely to be merged 😞 I'd suggest redhat-developer/vscode-yaml to use it's own fork of prettier, or any other formatter that may suit all the needs of its users. As you've suggested already, brodybits/prettierx seems a great alternative

OctavioBR avatar Jun 27 '21 16:06 OctavioBR

Hi, has there been any progress on this? Or known workaround... I was trying to disable formatOnSave feature for YAML completely (as a workaround) but in my case I am editing a Markdown file with YAML snippets and that makes this more complex.

rvansa avatar Dec 17 '21 13:12 rvansa

Same as above, wonder if there is any update here?

RafalSkolasinski avatar Jan 06 '22 19:01 RafalSkolasinski

+1 for this. Most editors adds no indents for lists. Prettier breaks them without this option.

mislam01 avatar Feb 12 '22 05:02 mislam01

+1 please provide an option for me to config

winkee01 avatar Mar 02 '22 08:03 winkee01

The path forward is to extend prettierx and then use it instead of prettier?

https://github.com/brodybits/prettierx/issues/382

Normally, I might advocate for extending prettier via prettier-plugin-x, but prettier's philosophical tenet that their opinion cannot be configured seems incompatible with the needs of a yaml plugin for vscode.

joebowbeer avatar May 08 '22 00:05 joebowbeer

To be honest I am against making non-indented default. I am more than ok with them indented (prettier default). For example on Ansible ecosystem that is also the standard and there are good reasons for it related to visibility/readability.

ssbarnea avatar May 18 '22 09:05 ssbarnea

Ansible is using non-intented format for yamls in many places? Just for example on this snippet in docs here https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html we have

---
- name: Update web servers
  hosts: webservers
  remote_user: root

  tasks:
  - name: Ensure apache is at the latest version
    ansible.builtin.yum:
      name: httpd
      state: latest
  - name: Write the apache config file
    ansible.builtin.template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf

To be clear, I am not against indented lists. Just not in all context. It should be customizable and everyone will be happy :)

RafalSkolasinski avatar May 18 '22 09:05 RafalSkolasinski

@ssbarnea the selection of a default is a different question. The intent as stated in the title is to add config in order to support the style baked into kustomize and used by a rapidly growing community of Kubernetes users.

joebowbeer avatar May 18 '22 09:05 joebowbeer

Do we have any hope on resolving this any time soon? This makes the extension unusable for k8s-related development.

RafalSkolasinski avatar Jun 07 '22 12:06 RafalSkolasinski

might be worth making an option for which backend formatted to use? There are two camps here, and both are correct. There are the prettier people and the ansible people.

Maybe something like:

"yaml.formatting.engine": "prettier",

or

"yaml.formatting.engine": "pyyaml",

if I get some time maybe I might take a stab at doing this

blairham avatar Sep 23 '22 17:09 blairham

@blairham Ansible people group decided to become prettier, so basically the groups you are talking about are more likely named: pyyaml and everyone else ;)

While ansible-lint is making use of pyyaml, it does override its default config in order to make it compatible with prettier rules. Sadly the documentation does not specify this, but we are working on improving it.

Joke aside, I am not against adding a configuration option, but it should be named yaml.formatting.style, defaulting to prettier. The other option would be pyyaml, and that would be a style, not a formatter. Who will write it is another question because prettier does not allow customization and yaml extension will never add a dependency on a python library. Basically to implement the pyyaml styling, we need a ts/js library that can do it, one that is actively maintained.

ssbarnea avatar Sep 23 '22 19:09 ssbarnea

@ssbarnea I agree that style is better nomenclature for the option, however, in that case I would omit prettier and pyyaml from its values and use terms descriptive of the style options, rather than the default choices of some tools. Let's be very clear.

The important thing is to provide an option.

Does the prettierx approach have potential?

joebowbeer avatar Sep 23 '22 19:09 joebowbeer

+1 on this one

Ryu-Z avatar Oct 15 '22 05:10 Ryu-Z

now i find a new plugin:kubernetes-yaml-formatter https://github.com/longkai/kubernetes-yaml-formatter

Ryu-Z avatar Oct 15 '22 05:10 Ryu-Z

+1

Was this issue addressed in the end?

mzylak83 avatar Mar 22 '23 23:03 mzylak83