Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird indentation for nesting mappings (dicts) and sequences (lists) #80

Open
waylan opened this issue Oct 26, 2017 · 4 comments
Open

Comments

@waylan
Copy link
Contributor

waylan commented Oct 26, 2017

I have the following sample (indentation represented by the middle-dot for demonstration purposes only) which by my eye is indented in the only sensible way:

---
items:
····- Item 1: item-1
····- Item 2:
········- Item 2.1: item-2.1
········- Item 2.2: item-2.2
····- Item 3:
····- Item 3.1: item-3.1
········- Item 3.2:
············- Item 3.2.1: item-3.2.1
············- Item 3.2.2: item-3.2.2

Yes, I get the following output which makes no sense:

sample.yml
  5:9: [error] wrong indentation: expected 10 but found 8 (indentation)
  8:9: [error] wrong indentation: expected 10 but found 8 (indentation)
  10:13: [error] wrong indentation: expected 14 but found 12 (indentation)

I've tried changing the indentation rules in various ways but never seem to get a reasonable result. Setting indent-sequences to any of true, whatever or consistent all result in the same output (yes, even whatever! What's up with that?!). Setting indent-sequences to false gives this:

sample.yml
  3:5: [error] wrong indentation: expected 0 but found 4 (indentation)
  5:9: [error] wrong indentation: expected 6 but found 8 (indentation)
  8:9: [error] wrong indentation: expected 6 but found 8 (indentation)
  10:13: [error] wrong indentation: expected 10 but found 12 (indentation)

... which still makes no sense. Actually, with indent-sequences: false you need to keep making changes until none of the lines are indented at all. Therefore, the above output is rather misleading.

In either case, why would the suggested indent be anything but a divisible of the spaces value (4 in this instance, although the same behavior presents itself with other values including consistent).

Going back to the default, you end up with this following to pass (again with indentation represented by the middle-dot for demonstration purposes):

---
items:
····- Item 1: item-1
····- Item 2:
··········- Item 2.1: item-2.1
··········- Item 2.2: item-2.2
····- Item 3:
··········- Item 3.1: item-3.1
··········- Item 3.2:
················- Item 3.2.1: item-3.2.1
················- Item 3.2.2: item-3.2.2

The first level of indent is 4 spaces, but all subsequent levels of indent are 6 spaces. I realize the documentation acknowledges that "some people perceive the - as part of the indentation", but I don't see how that fits here. Especially as you can't actually override it, which is the rational given in the documentation for providing the overrides.

I don't really care about the rational, I just want to enforce consistent (4 spaces for each level) in my YAML files and the current set of options don't allow that. If the current behavior was intentional, then an additional option need sot be provided. If the current behavior is a bug, I'm willing to take a crack at a fix. So is this a bug or not?

@adrienverge
Copy link
Owner

Hey @waylan,

I got what you mean. This behavior is intended, so this is not a bug. I think the problem lies in what you said at the end: I realize the documentation acknowledges that "some people perceive the - as part of the indentation".

I can agree the perception of this is quite subjective, but I'll try to explain why most people start counting indentation after the - token. For this, let's replace some of your items by other structures (let's say, long strings, arrays and dictionnaries), and let's use yamllint's way of indenting:

---
items:
    - Item 1: item-1
    - Item 2:
          - - Item 2.1.1
            - Item 2.1.2
            - Item 2.1.3
          - - Item 2.2.1
            - Item 2.2.2
            - Item 2.2.3
    - Item 3:
          - this
            is a
            multiline string
          - key-a: A
            key-b: B
            key-c: C

If we took your way of indenting, it would have given this:

---
items:
    - Item 1: item-1
    - Item 2:
        - - Item 2.1.1
            - Item 2.1.2
            - Item 2.1.3
        - - Item 2.2.1
            - Item 2.2.2
            - Item 2.2.3
    - Item 3:
        - this
        is a
        multiline string
        - key-a: A
        key-b: B
        key-c: C

... which is not the same structure (it is actually not even valid YAML).

I hope this helps, let me know if I wasn't clear!

@waylan
Copy link
Contributor Author

waylan commented Oct 26, 2017

First, while I understand how it works, I hate that indentation convention (I know many people use that for Markdown lists, which I also hate), I want to hit the tab key once for each level of indent and that's it. As it is now, I would need to type 2 tabs & 2 spaces for the second level of indent and 4 tabs for the third level of indent. The fact that every other level switches between a half-tab and a full tab is even more annoying.

In fact, the documentation suggests that the options are there for those who don't want that behavior (specifically it says: "when in a mapping, this indentation is not mandatory"). Yet, I can't get anything but that behavior, so something is broken. At the very least whatever should allow my scheme, but even that doesn't work.

To be clear, for multiple-line text fields, I understand it (even if I don't like it). For everything else that should not be how it works, IMO. If others disagree, fine, but please give me the option to do it my way.

@waylan
Copy link
Contributor Author

waylan commented Oct 26, 2017

As a compromise I would be okay with this

item 1:  
    - A block of text
      with multiple lines
      which is indented
      by 6 spaces.
    - item 1-2:
        - item 1-2-1: indented 8 spaces

Here, the secondary lines of a text block are indented the extra two spaces to account for the - on the first line. However, nested, non-text block items get the full 8 spaces of indent. Yes, this is what "consistent" means to me. This is also what I assumed was meant by "when in a mapping, this indentation is not mandatory" in the documentation. If that means something else, then I suggest an update to the docs.

@wilhelmer
Copy link

First, while I understand how it works, I hate that indentation convention (I know many people use that for Markdown lists, which I also hate), I want to hit the tab key once for each level of indent and that's it. As it is now, I would need to type 2 tabs & 2 spaces for the second level of indent and 4 tabs for the third level of indent. The fact that every other level switches between a half-tab and a full tab is even more annoying.

In fact, the documentation suggests that the options are there for those who don't want that behavior (specifically it says: "when in a mapping, this indentation is not mandatory"). Yet, I can't get anything but that behavior, so something is broken. At the very least whatever should allow my scheme, but even that doesn't work.

100% agree.

Almost 4 years later, this still keeps me from using yamllint. Every YML checker in the worlds says this is perfectly valid YML, except for yamllint:

foo:
    - bar:
        - baz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants