Rules: indentation: Fix `spaces: consitent` with broken flows

pull/4/head
Adrien Vergé 9 years ago
parent 5b62548ece
commit 9a7eec34b1

@ -684,7 +684,7 @@ class IndentationTestCase(RuleTestCase):
'a: {\n' 'a: {\n'
' x: 1,\n' ' x: 1,\n'
' y, z: 1\n' ' y, z: 1\n'
'}\n', conf, problem=(3, 4)) '}\n', conf, problem=(4, 3))
self.check('---\n' self.check('---\n'
'a: {\n' 'a: {\n'
' x: 1,\n' ' x: 1,\n'
@ -703,7 +703,7 @@ class IndentationTestCase(RuleTestCase):
'a: [\n' 'a: [\n'
' x,\n' ' x,\n'
' y, z\n' ' y, z\n'
']\n', conf, problem=(3, 4)) ']\n', conf, problem=(4, 3))
self.check('---\n' self.check('---\n'
'a: [\n' 'a: [\n'
' x,\n' ' x,\n'
@ -752,6 +752,17 @@ class IndentationTestCase(RuleTestCase):
' foo: 1,\n' ' foo: 1,\n'
' bar: 2\n' ' bar: 2\n'
' }\n', conf, problem1=(7, 7), problem2=(11, 3)) ' }\n', conf, problem1=(7, 7), problem2=(11, 3))
conf = 'indentation: {spaces: 2}'
self.check('---\n'
'a: {\n'
' x: 1,\n'
' y, z: 1\n'
'}\n', conf, problem=(3, 4))
self.check('---\n'
'a: [\n'
' x,\n'
' y, z\n'
']\n', conf, problem=(3, 4))
def test_cleared_flows(self): def test_cleared_flows(self):
# flow: # flow:

@ -287,11 +287,10 @@ def check(conf, token, prev, next, nextnext, context):
first_in_line = (is_visible and first_in_line = (is_visible and
token.start_mark.line + 1 > context['cur_line']) token.start_mark.line + 1 > context['cur_line'])
def detect_indent(next): def detect_indent(base_indent, next):
if type(context['spaces']) is not int: if type(context['spaces']) is not int:
context['spaces'] = (next.start_mark.column - context['spaces'] = next.start_mark.column - base_indent
context['stack'][-1].indent) return base_indent + context['spaces']
return context['spaces']
if first_in_line: if first_in_line:
found_indentation = token.start_mark.column found_indentation = token.start_mark.column
@ -303,7 +302,7 @@ def check(conf, token, prev, next, nextnext, context):
elif (context['stack'][-1].type == KEY and elif (context['stack'][-1].type == KEY and
context['stack'][-1].explicit_key and context['stack'][-1].explicit_key and
not isinstance(token, yaml.ValueToken)): not isinstance(token, yaml.ValueToken)):
expected += detect_indent(token) expected = detect_indent(expected, token)
if found_indentation != expected: if found_indentation != expected:
yield LintProblem(token.start_mark.line + 1, found_indentation + 1, yield LintProblem(token.start_mark.line + 1, found_indentation + 1,
@ -337,7 +336,7 @@ def check(conf, token, prev, next, nextnext, context):
# - ? # - ?
# a # a
# : 1 # : 1
indent = token.start_mark.column + detect_indent(next) indent = detect_indent(token.start_mark.column, next)
context['stack'].append(Parent(B_MAP, indent)) context['stack'].append(Parent(B_MAP, indent))
@ -349,7 +348,7 @@ def check(conf, token, prev, next, nextnext, context):
# - { # - {
# a: 1, b: 2 # a: 1, b: 2
# } # }
indent = context['cur_line_indent'] + detect_indent(next) indent = detect_indent(context['cur_line_indent'], next)
context['stack'].append(Parent(F_MAP, indent, context['stack'].append(Parent(F_MAP, indent,
line_indent=context['cur_line_indent'])) line_indent=context['cur_line_indent']))
@ -383,7 +382,7 @@ def check(conf, token, prev, next, nextnext, context):
# - # -
# key: # key:
# value # value
indent = token.start_mark.column + detect_indent(next) indent = detect_indent(token.start_mark.column, next)
context['stack'].append(Parent(B_ENT, indent)) context['stack'].append(Parent(B_ENT, indent))
@ -395,7 +394,7 @@ def check(conf, token, prev, next, nextnext, context):
# - [ # - [
# a, b # a, b
# ] # ]
indent = context['cur_line_indent'] + detect_indent(next) indent = detect_indent(context['cur_line_indent'], next)
context['stack'].append(Parent(F_SEQ, indent, context['stack'].append(Parent(F_SEQ, indent,
line_indent=context['cur_line_indent'])) line_indent=context['cur_line_indent']))
@ -433,7 +432,7 @@ def check(conf, token, prev, next, nextnext, context):
# ? k # ? k
# : # :
# value # value
indent = context['stack'][-1].indent + detect_indent(next) indent = detect_indent(context['stack'][-1].indent, next)
elif next.start_mark.line == prev.start_mark.line: elif next.start_mark.line == prev.start_mark.line:
# k: value # k: value
indent = next.start_mark.column indent = next.start_mark.column
@ -447,7 +446,7 @@ def check(conf, token, prev, next, nextnext, context):
if conf['indent-sequences'] is False: if conf['indent-sequences'] is False:
indent = context['stack'][-1].indent indent = context['stack'][-1].indent
elif conf['indent-sequences'] is True: elif conf['indent-sequences'] is True:
indent = context['stack'][-1].indent + detect_indent(next) indent = detect_indent(context['stack'][-1].indent, next)
else: # 'whatever' else: # 'whatever'
if next.start_mark.column == context['stack'][-1].indent: if next.start_mark.column == context['stack'][-1].indent:
# key: # key:
@ -458,12 +457,12 @@ def check(conf, token, prev, next, nextnext, context):
# key: # key:
# - e1 # - e1
# - e2 # - e2
indent = (context['stack'][-1].indent + indent = detect_indent(context['stack'][-1].indent,
detect_indent(next)) next)
else: else:
# k: # k:
# value # value
indent = context['stack'][-1].indent + detect_indent(next) indent = detect_indent(context['stack'][-1].indent, next)
context['stack'].append(Parent(VAL, indent)) context['stack'].append(Parent(VAL, indent))

Loading…
Cancel
Save