Rules: comments-indentation: Allow two levels

Previously only comments that were indented like the following content
line were allowed, e.g.:

    prev: line:
      # commented line
      current: line

With this change, such new cases are also allowed:

      prev: line
      # commented line 1
    # commented line 2
    current: line
This commit is contained in:
Adrien Vergé
2016-01-18 18:34:40 +01:00
parent 01c12f2462
commit f09aef4f89
4 changed files with 88 additions and 11 deletions

View File

@@ -17,27 +17,48 @@
import yaml
from yamllint.errors import LintProblem
from yamllint.rules.common import get_comments_between_tokens
from yamllint.rules.common import get_line_indent, get_comments_between_tokens
ID = 'comments-indentation'
TYPE = 'token'
# Case A:
#
# prev: line:
# # commented line
# current: line
#
# Case B:
#
# prev: line
# # commented line 1
# # commented line 2
# current: line
def check(conf, token, prev, next):
if prev is None:
return
token_indent = token.start_mark.column
curr_line_indent = token.start_mark.column
if isinstance(token, yaml.StreamEndToken):
token_indent = 0
curr_line_indent = 0
skip_first = True
skip_first_line = True
if isinstance(prev, yaml.StreamStartToken):
skip_first = False
skip_first_line = False
prev_line_indent = 0
else:
prev_line_indent = get_line_indent(prev)
for comment in get_comments_between_tokens(prev, token,
skip_first_line=skip_first):
if comment.column != token_indent + 1:
if prev_line_indent <= curr_line_indent:
prev_line_indent = -1 # disable it
for comment in get_comments_between_tokens(
prev, token, skip_first_line=skip_first_line):
if comment.column - 1 == curr_line_indent:
prev_line_indent = -1 # disable it
elif comment.column - 1 != prev_line_indent:
yield LintProblem(comment.line, comment.column,
'comment not indented like content')

View File

@@ -64,6 +64,16 @@ class Comment(object):
str(self) == str(other))
def get_line_indent(token):
"""Finds the indent of the line the token starts in."""
start = token.start_mark.buffer.rfind('\n', 0,
token.start_mark.pointer) + 1
content = start
while token.start_mark.buffer[content] == ' ':
content += 1
return content - start
def get_comments_between_tokens(token1, token2, skip_first_line=False):
if token2 is None:
buf = token1.end_mark.buffer[token1.end_mark.pointer:]