Rules: indentation: Rewrite algorithm

pull/4/head
Adrien Vergé 9 years ago
parent 3989a09d32
commit e81b73c111

@ -41,7 +41,7 @@ class ColonTestCase(RuleTestCase):
' - o: {k1: v1}\n'
' - p: kdjf\n'
' - q: val0\n'
' q2:\n'
' - q2:\n'
' - val1\n'
'...\n', conf)
self.check('---\n'
@ -66,7 +66,7 @@ class ColonTestCase(RuleTestCase):
' val\n'
' - o: {k1: v1}\n'
' - o: {k1: v1}\n'
' q2:\n'
' - q2:\n'
' - val1\n'
'...\n', conf)
self.check('---\n'

@ -118,7 +118,7 @@ class IndentationTestCase(RuleTestCase):
self.check('---\n'
' object:\n'
' val: 1\n'
'...\n', conf, problem=(2, 2))
'...\n', conf, problem1=(2, 2), problem2=(3, 6))
self.check('---\n'
'- el1\n'
'- el2:\n'
@ -133,7 +133,8 @@ class IndentationTestCase(RuleTestCase):
' - el1\n'
' - el2:\n'
' - subel\n'
'...\n', conf, problem1=(2, 3), problem2=(4, 7))
'...\n', conf,
problem1=(2, 3), problem2=(3, 3), problem3=(4, 5))
def test_multi_lines(self):
self.check('---\n'
@ -154,18 +155,32 @@ class IndentationTestCase(RuleTestCase):
'...\n', None)
def test_nested_collections(self):
conf = 'indentation: {spaces: 2}'
self.check('---\n'
'- o:\n'
' k1: v1\n'
'...\n', None)
'...\n', conf)
self.check('---\n'
'- o:\n'
' k1: v1\n'
'...\n', conf, problem=(3, 2))
self.check('---\n'
'- o:\n'
' k1: v1\n'
'...\n', conf, problem=(3, 4))
conf = 'indentation: {spaces: 4}'
self.check('---\n'
'- o:\n'
' k1: v1\n'
'...\n', conf)
self.check('---\n'
'- o:\n'
' k1: v1\n'
'...\n', None, problem=(3, 4))
'...\n', conf, problem=(3, 4))
self.check('---\n'
'- o:\n'
' k1: v1\n'
'...\n', None, problem=(3, 6))
'...\n', conf, problem=(3, 6))
def test_return(self):
self.check('---\n'
@ -189,3 +204,23 @@ class IndentationTestCase(RuleTestCase):
# ' c:\n'
# ' d:\n'
# '...\n', None, problem=(5, 2))
def test_first_line(self):
conf = ('indentation: {spaces: 2}\n'
'document-start: disable\n')
self.check(' a: 1\n', conf, problem=(1, 3))
def test_broken_inline_flows(self):
conf = 'indentation: {spaces: 2}'
self.check('---\n'
'obj: {\n'
' a: 1,\n'
' b: 2,\n'
' c: 3\n'
'}\n', conf, problem1=(4, 4), problem2=(5, 2))
self.check('---\n'
'list: [\n'
' 1,\n'
' 2,\n'
' 3\n'
']\n', conf, problem1=(4, 4), problem2=(5, 2))

@ -25,39 +25,46 @@ CONF = {'spaces': int}
def check(conf, token, prev, next):
if isinstance(token, yaml.StreamEndToken):
if isinstance(token, (yaml.StreamStartToken, yaml.StreamEndToken)):
return
if (prev is None or isinstance(prev, yaml.StreamStartToken) or
isinstance(prev, yaml.DirectiveToken) or
isinstance(prev, yaml.DocumentStartToken)):
if token.start_mark.column != 0:
# Check if first token in line
if (not isinstance(prev, (yaml.StreamStartToken, yaml.DirectiveToken)) and
token.start_mark.line == prev.end_mark.line):
return
if token.start_mark.column % conf['spaces'] != 0:
yield LintProblem(
token.end_mark.line + 1, token.start_mark.column + 1,
'found indentation of %d instead of %d' %
(token.start_mark.column, 0))
'indentation is not a multiple of %d' % conf['spaces'])
return
if token.start_mark.line > prev.end_mark.line:
if isinstance(prev, (yaml.StreamStartToken,
yaml.DirectiveToken,
yaml.DocumentStartToken,
yaml.DocumentEndToken)):
indent = 0
else:
buffer = prev.end_mark.buffer
start = buffer.rfind('\n', 0, prev.end_mark.pointer) + 1
prev_indent = 0
# YAML recognizes two white space characters: space and tab.
# http://yaml.org/spec/1.2/spec.html#id2775170
while buffer[start + prev_indent] in ' \t':
prev_indent += 1
indent = 0
while buffer[start + indent] == ' ':
indent += 1
# Discard any leading '- '
if (buffer[start + prev_indent:start + prev_indent + 2] == '- '):
prev_indent += 2
while buffer[start + prev_indent] in ' \t':
prev_indent += 1
if token.start_mark.column > indent:
if not isinstance(prev, (yaml.BlockSequenceStartToken,
yaml.BlockMappingStartToken,
yaml.FlowSequenceStartToken,
yaml.FlowMappingStartToken,
yaml.KeyToken,
yaml.ValueToken)):
yield LintProblem(
token.end_mark.line + 1, token.start_mark.column + 1,
'unexpected indentation')
if (token.start_mark.column > prev_indent and
token.start_mark.column != prev_indent + conf['spaces']):
elif token.start_mark.column != indent + conf['spaces']:
yield LintProblem(
token.end_mark.line + 1, token.start_mark.column + 1,
'found indentation of %d instead of %d' %
(token.start_mark.column, prev_indent + conf['spaces']))
(token.start_mark.column, indent + conf['spaces']))

Loading…
Cancel
Save