Rules: indentation: Fix flow sequences with multi-line scalars

Typically sequences like this:

    ["multi
      line 1", "multi
                line 2"]
This commit is contained in:
Adrien Vergé
2016-01-31 22:21:31 +01:00
parent 14c99da2bb
commit dd163ed551
3 changed files with 55 additions and 8 deletions

View File

@@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import string
import yaml
from yamllint.linter import LintProblem
@@ -77,6 +79,25 @@ def get_line_indent(token):
return content - start
def get_real_end_line(token):
"""Finds the line on which the token really ends.
With pyyaml, scalar tokens often end on a next line.
"""
end_line = token.end_mark.line + 1
if not isinstance(token, yaml.ScalarToken):
return end_line
pos = token.end_mark.pointer - 1
while (pos >= token.start_mark.pointer - 1 and
token.end_mark.buffer[pos] in string.whitespace):
if token.end_mark.buffer[pos] == '\n':
end_line -= 1
pos -= 1
return end_line
def get_comments_between_tokens(token1, token2, skip_first_line=False):
if token2 is None:
buf = token1.end_mark.buffer[token1.end_mark.pointer:]

View File

@@ -145,7 +145,7 @@ Use this rule to control the indentation.
import yaml
from yamllint.linter import LintProblem
from yamllint.rules.common import is_explicit_key
from yamllint.rules.common import is_explicit_key, get_real_end_line
ID = 'indentation'
@@ -237,13 +237,14 @@ def check(conf, token, prev, next, context):
# Step 1: Lint
needs_lint = (
is_visible = (
not isinstance(token, (yaml.StreamStartToken, yaml.StreamEndToken)) and
not isinstance(token, yaml.BlockEndToken) and
not (isinstance(token, yaml.ScalarToken) and token.value == '') and
token.start_mark.line + 1 > context['cur_line'])
not (isinstance(token, yaml.ScalarToken) and token.value == ''))
first_in_line = (is_visible and
token.start_mark.line + 1 > context['cur_line'])
if needs_lint:
if first_in_line:
found_indentation = token.start_mark.column
expected = context['stack'][-1].indent
@@ -267,9 +268,10 @@ def check(conf, token, prev, next, context):
# Step 2.a:
if needs_lint:
context['cur_line_indent'] = found_indentation
context['cur_line'] = token.end_mark.line + 1
if is_visible:
context['cur_line'] = get_real_end_line(token)
if first_in_line:
context['cur_line_indent'] = found_indentation
# Step 2.b: Update state