Skip to content

Commit

Permalink
Rules: indentation: Add support for cleared sequence entries
Browse files Browse the repository at this point in the history
The following construction is valid YAML, and its indentation should be
correctly handled:

    - this is
    -
      a
    -
      sequence:
        with cleared entries
  • Loading branch information
adrienverge committed Feb 1, 2016
1 parent 7cb7b4f commit cae1000
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
45 changes: 45 additions & 0 deletions tests/rules/test_indentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,51 @@ def test_explicit_block_mappings(self):
' value\n'
'...\n', conf, problem1=(4, 10), problem2=(6, 8))

def test_clear_sequence_item(self):
conf = 'indentation: {spaces: 2}'
self.check('---\n'
'-\n'
' string\n'
'-\n'
' map: ping\n'
'-\n'
' - sequence\n'
' -\n'
' nested\n'
' -\n'
' >\n'
' multi\n'
' line\n'
'...\n', conf)
self.check('---\n'
'-\n'
' string\n'
'-\n'
' string\n', conf, problem1=(3, 2), problem2=(5, 4))
self.check('---\n'
'-\n'
' map: ping\n'
'-\n'
' map: ping\n', conf, problem1=(3, 2), problem2=(5, 4))
self.check('---\n'
'-\n'
' - sequence\n'
'-\n'
' - sequence\n', conf, problem1=(3, 2), problem2=(5, 4))
self.check('---\n'
'-\n'
' -\n'
' nested\n'
' -\n'
' nested\n', conf, problem1=(4, 4), problem2=(6, 6))
self.check('---\n'
'-\n'
' -\n'
' >\n'
' multi\n'
' line\n'
'...\n', conf, problem=(4, 6))


class ScalarIndentationTestCase(RuleTestCase):
rule_id = 'indentation'
Expand Down
22 changes: 21 additions & 1 deletion yamllint/rules/indentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
'indent-sequences': (True, False, 'whatever'),
'check-multi-line-strings': bool}

ROOT, MAP, B_SEQ, F_SEQ, KEY, VAL = range(6)
ROOT, MAP, B_SEQ, F_SEQ, B_ENT, KEY, VAL = range(7)


class Parent(object):
Expand Down Expand Up @@ -273,6 +273,9 @@ def check(conf, token, prev, next, context):

# Step 2.b: Update state

if context['stack'][-1].type == B_ENT:
context['stack'].pop()

if isinstance(token, yaml.BlockMappingStartToken):
assert isinstance(next, yaml.KeyToken)
if next.start_mark.line == token.start_mark.line:
Expand Down Expand Up @@ -312,6 +315,23 @@ def check(conf, token, prev, next, context):

context['stack'].append(Parent(B_SEQ, indent))

elif (isinstance(token, yaml.BlockEntryToken) and
# in case of an empty entry
not isinstance(next, (yaml.BlockEntryToken, yaml.BlockEndToken))):
if next.start_mark.line == token.end_mark.line:
# - item 1
# - item 2
indent = next.start_mark.column
else:
# -
# item 1
# -
# key:
# value
indent = token.start_mark.column + conf['spaces']

context['stack'].append(Parent(B_ENT, indent))

elif isinstance(token, yaml.FlowSequenceStartToken):
if next.start_mark.line == token.start_mark.line:
# - [a, b]
Expand Down

0 comments on commit cae1000

Please sign in to comment.