Figuring out how indentation of chained brackets works.

This commit is contained in:
Dave Halter
2017-06-15 01:26:38 +02:00
parent c9fe2596c1
commit d5e9ecea12
2 changed files with 99 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ from parso.normalizer import Normalizer, Rule, NormalizerConfig
_IMPORT_TYPES = ('import_name', 'import_from') _IMPORT_TYPES = ('import_name', 'import_from')
_SUITE_INTRODUCERS = ('classdef', 'funcdef', 'if_stmt', 'while_stmt', _SUITE_INTRODUCERS = ('classdef', 'funcdef', 'if_stmt', 'while_stmt',
'for_stmt', 'try_stmt', 'with_stmt') 'for_stmt', 'try_stmt', 'with_stmt')
_OPENING_BRACKETS = '([{'
class CompressNormalizer(Normalizer): class CompressNormalizer(Normalizer):
""" """
@@ -81,6 +82,8 @@ class IndentationNode(object):
class BracketNode(IndentationNode): class BracketNode(IndentationNode):
def __init__(self, config, parent_indentation, leaf): def __init__(self, config, parent_indentation, leaf):
self.leaf = leaf
next_leaf = leaf.get_next_leaf() next_leaf = leaf.get_next_leaf()
if '\n' in next_leaf.prefix: if '\n' in next_leaf.prefix:
# This implies code like: # This implies code like:
@@ -256,14 +259,14 @@ class PEP8Normalizer(Normalizer):
self.add_issue(123, "Losing bracket does not match indentation of opening bracket's line", leaf) self.add_issue(123, "Losing bracket does not match indentation of opening bracket's line", leaf)
else: else:
if len(info.indentation) < len(should_be_indentation): if len(info.indentation) < len(should_be_indentation):
if node.type == BracketNode.SAME_INDENT_TYPE: if node.type == IndentationNode.SAME_INDENT_TYPE:
self.add_issue(128, 'Continuation line under-indented for visual indent', leaf) self.add_issue(128, 'Continuation line under-indented for visual indent', leaf)
elif node.type == BracketNode.BACKSLASH_TYPE: elif node.type == IndentationNode.BACKSLASH_TYPE:
self.add_issue(122, 'Continuation line missing indentation or outdented', leaf) self.add_issue(122, 'Continuation line missing indentation or outdented', leaf)
else: else:
self.add_issue(121, 'Continuation line under-indented for hanging indent', leaf) self.add_issue(121, 'Continuation line under-indented for hanging indent', leaf)
else: else:
if node.type == BracketNode.SAME_INDENT_TYPE: if node.type == IndentationNode.SAME_INDENT_TYPE:
self.add_issue(127, 'Continuation line over-indented for visual indent', leaf) self.add_issue(127, 'Continuation line over-indented for visual indent', leaf)
else: else:
self.add_issue(126, 'Continuation line over-indented for hanging indent', leaf) self.add_issue(126, 'Continuation line over-indented for hanging indent', leaf)
@@ -307,10 +310,22 @@ class PEP8Normalizer(Normalizer):
# ------------------------------- # -------------------------------
if value and value in '()[]{}' and leaf.type != 'error_leaf' \ if value and value in '()[]{}' and leaf.type != 'error_leaf' \
and leaf.parent.type != 'error_node': and leaf.parent.type != 'error_node':
if value in '([{': if value in _OPENING_BRACKETS:
# Figure out here what the indentation is. For chained brackets
# we can basically use the previous indentation.
previous_leaf = leaf
index = 1
while not info.newline_count:
previous_leaf = previous_leaf.get_previous_leaf()
n = self._indentation_stack[-index]
if not isinstance(n, BracketNode) or previous_leaf != n.leaf:
break
index += 1
indentation = self._indentation_stack[-index].indentation
self._indentation_stack.append( self._indentation_stack.append(
BracketNode( BracketNode(
self._config, self._indentation_stack[-1].indentation, self._config, indentation,
leaf leaf
) )
) )

View File

@@ -0,0 +1,79 @@
print "E121", (
#: E121:2
"dent")
print "E122", (
#: E121:0
"dent")
my_list = [
1, 2, 3,
4, 5, 6,
#: E123
]
print "E124", ("visual",
"indent_two"
#: E124:14
)
print "E124", ("visual",
"indent_five"
#: E124:0
)
a = (123,
#: E124:0
)
#: E129+1:4
if (row < 0 or self.moduleCount <= row or
col < 0 or self.moduleCount <= col):
raise Exception("%s,%s - %s" % (row, col, self.moduleCount))
print "E126", (
#: E126:12
"dent")
print "E126", (
#: E126:8
"dent")
print "E127", ("over-",
#: E127:18
"over-indent")
print "E128", ("visual",
#: E128:4
"hanging")
print "E128", ("under-",
#: E128:14
"under-indent")
my_list = [
1, 2, 3,
4, 5, 6,
#: E123:5
]
result = {
#: E121:3
'key1': 'value',
#: E121:3
'key2': 'value',
}
rv.update(dict.fromkeys((
'qualif_nr', 'reasonComment_en', 'reasonComment_fr',
'reasonComment_de', 'reasonComment_it'),
#: E128
'?'),
"foo")
#: E126+1:10 E126+2:10
abricot = 3 + \
4 + \
5 + 6
print "hello", (
"there",
#: E131:5
# "john",
"dude")
part = set_mimetype((
a.get('mime_type', 'text')),
'default')
part = set_mimetype((
a.get('mime_type', 'text')),
#: E127:21
'default')