Rules: indentation: Fix check-multi-line-strings

For strings that continue on next line at a lower indentation level:

    Blaise Pascal: Je vous écris une longue lettre parce que
      je n'ai pas le temps d'en écrire une courte.
This commit is contained in:
Adrien Vergé 2016-01-25 11:01:42 +01:00
parent 97c446907c
commit 4410bc3e23
2 changed files with 29 additions and 18 deletions

View File

@ -507,7 +507,7 @@ class ScalarIndentationTestCase(RuleTestCase):
self.check('a key: multi\n' self.check('a key: multi\n'
' line\n', conf) ' line\n', conf)
self.check('a key: multi\n' self.check('a key: multi\n'
' line\n', conf, problem=(2, 3)) ' line\n', conf)
self.check('a key: multi\n' self.check('a key: multi\n'
' line\n', conf) ' line\n', conf)
self.check('a key:\n' self.check('a key:\n'
@ -528,6 +528,8 @@ class ScalarIndentationTestCase(RuleTestCase):
' line\n', conf, problem=(2, 2)) ' line\n', conf, problem=(2, 2))
self.check('- multi\n' self.check('- multi\n'
' line\n', conf, problem=(2, 4)) ' line\n', conf, problem=(2, 4))
self.check('a key: multi\n'
' line\n', conf, problem=(2, 3))
self.check('a key: multi\n' self.check('a key: multi\n'
' line\n', conf, problem=(2, 9)) ' line\n', conf, problem=(2, 9))
self.check('a key:\n' self.check('a key:\n'
@ -546,24 +548,13 @@ class ScalarIndentationTestCase(RuleTestCase):
'document-start: disable\n') 'document-start: disable\n')
self.check('"multi\n' self.check('"multi\n'
' line"\n', conf) ' line"\n', conf)
self.check('"multi\n'
'line"\n', conf, problem=(2, 1))
self.check('- "multi\n' self.check('- "multi\n'
' line"\n', conf) ' line"\n', conf)
self.check('- "multi\n'
' line"\n', conf, problem=(2, 3))
self.check('a key: "multi\n' self.check('a key: "multi\n'
' line"\n', conf) ' line"\n', conf)
self.check('a key: "multi\n'
' line"\n', conf, problem=(2, 3))
self.check('a key: "multi\n'
' line"\n', conf, problem=(2, 8))
self.check('a key:\n' self.check('a key:\n'
' "multi\n' ' "multi\n'
' line"\n', conf) ' line"\n', conf)
self.check('a key:\n'
' "multi\n'
' line"\n', conf, problem=(3, 3))
self.check('- jinja2: "{% if ansible is defined %}\n' self.check('- jinja2: "{% if ansible is defined %}\n'
' {{ ansible }}\n' ' {{ ansible }}\n'
' {% else %}\n' ' {% else %}\n'
@ -580,11 +571,22 @@ class ScalarIndentationTestCase(RuleTestCase):
conf = ('indentation: {spaces: 2, check-multi-line-strings: yes}\n' conf = ('indentation: {spaces: 2, check-multi-line-strings: yes}\n'
'document-start: disable\n') 'document-start: disable\n')
self.check('"multi\n' self.check('"multi\n'
'line"\n', conf, problem=(2, 1))
self.check('"multi\n'
' line"\n', conf, problem=(2, 3))
self.check('- "multi\n'
' line"\n', conf, problem=(2, 3)) ' line"\n', conf, problem=(2, 3))
self.check('- "multi\n' self.check('- "multi\n'
' line"\n', conf, problem=(2, 5)) ' line"\n', conf, problem=(2, 5))
self.check('a key: "multi\n'
' line"\n', conf, problem=(2, 3))
self.check('a key: "multi\n'
' line"\n', conf, problem=(2, 8))
self.check('a key: "multi\n' self.check('a key: "multi\n'
' line"\n', conf, problem=(2, 10)) ' line"\n', conf, problem=(2, 10))
self.check('a key:\n'
' "multi\n'
' line"\n', conf, problem=(3, 3))
self.check('a key:\n' self.check('a key:\n'
' "multi\n' ' "multi\n'
' line"\n', conf, problem=(3, 5)) ' line"\n', conf, problem=(3, 5))

View File

@ -113,6 +113,18 @@ Use this rule to control the indentation.
Je vous écris une longue lettre parce que Je vous écris une longue lettre parce que
je n'ai pas le temps d'en écrire une courte. je n'ai pas le temps d'en écrire une courte.
the following code snippet would **PASS**:
::
Blaise Pascal: Je vous écris une longue lettre parce que
je n'ai pas le temps d'en écrire une courte.
the following code snippet would **FAIL**:
::
Blaise Pascal: Je vous écris une longue lettre parce que
je n'ai pas le temps d'en écrire une courte.
the following code snippet would **FAIL**: the following code snippet would **FAIL**:
:: ::
@ -212,11 +224,7 @@ def check_scalar_indentation(conf, token, context):
if token.start_mark.buffer[line_start + indent] == '\n': if token.start_mark.buffer[line_start + indent] == '\n':
continue continue
if indent < expected_indent: if indent != expected_indent:
yield LintProblem(line_no, indent + 1,
('wrong indentation: expected at least %d but '
'found %d') % (expected_indent, indent))
elif conf['check-multi-line-strings'] and indent > expected_indent:
yield LintProblem(line_no, indent + 1, yield LintProblem(line_no, indent + 1,
'wrong indentation: expected %d but found %d' % 'wrong indentation: expected %d but found %d' %
(expected_indent, indent)) (expected_indent, indent))
@ -252,7 +260,8 @@ def check(conf, token, prev, next, context):
'wrong indentation: expected %d but found %d' % 'wrong indentation: expected %d but found %d' %
(expected, found_indentation)) (expected, found_indentation))
if isinstance(token, yaml.ScalarToken): if (isinstance(token, yaml.ScalarToken) and
conf['check-multi-line-strings']):
for problem in check_scalar_indentation(conf, token, context): for problem in check_scalar_indentation(conf, token, context):
yield problem yield problem