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

Lists of dictionaries are not indented correctly #49

Open
wwentland opened this issue May 15, 2017 · 3 comments
Open

Lists of dictionaries are not indented correctly #49

wwentland opened this issue May 15, 2017 · 3 comments

Comments

@wwentland
Copy link

I'm seeing the following indentation behaviour when using yaml-mode:

foo:
  - bar: baz
    - foo: 1

I yaml-mode to indent like:

--- expected
foo:
  - bar: baz
  - foo: 1

This seems to be related to yaml-nested-sequence-re and the way it is being used in yaml-compute-indentation as either of the following changes fixes/changes this particular behaviour:

  1. Change yaml-nested-sequence-re to only match lines that end with :

The following regular expression results in the "correct" behaviour in this particular case.

(defconst yaml-nested-sequence-re
  (concat "^\\(?:\\(?: *- +\\)+\\|\\(:? *-$\\)\\)"
          "\\(?:" yaml-bare-scalar-re " *:\\)?$")
  "Regexp matching a line containing one or more nested YAML sequences.")
  1. Remove nested sequence check from yaml-compute-indentation

Unsurprisingly removing the yaml-nested-sequence-re check from yaml-compute-indentation to yield the following also results in the correct indentation of the snippet above.

(defun yaml-compute-indentation ()
  "Calculate the maximum sensible indentation for the current line."
  (save-excursion
    (beginning-of-line)
    (if (looking-at yaml-document-delimiter-re) 0
      (forward-line -1)
      (while (and (looking-at yaml-blank-line-re)
                  (> (point) (point-min)))
        (forward-line -1))
      (+ (current-indentation)
         (if (looking-at yaml-nested-map-re) yaml-indent-offset 0)
         (if (looking-at yaml-block-literal-re) yaml-indent-offset 0)))))

Paired with #24 it appears as if the indentation framework needs to be more context sensitive like python-mode.

@wasamasa
Copy link
Collaborator

Um, have you tried hitting the TAB key repeatedly? It should cycle between the following positions (with | denoting cursor):

foo:
  - bar: baz
| | |

This way you start with the right position, change to the middle, then the left one. For the record, rst-mode behaves the same. python-mode is in so far different as it uses the same indentation order, but detects special situations where it would make no sense to have the right one (like after a return line which enforces the left position). Your change eliminates the right position, only leaving the left and middle one, hence why I cannot accept your proposed suggestion. If anything, change the indentation code to cycle in a different order through the positions (like medium, right, left). Bonus points if you can detect special cases like with python-mode.

@wwentland
Copy link
Author

wwentland commented May 15, 2017

@wasamasa Thanks for looking into this. I am aware that the changes outlined above are not the correct way to do this, which is I why I didn't open a PR against this repo. I simply wanted to provide a bit of context.

What would be a good approach to change the order in which the positions are being suggested?

@wasamasa
Copy link
Collaborator

wasamasa commented May 15, 2017

¯\_(ツ)_/¯

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

Successfully merging a pull request may close this issue.

2 participants