From 92980f9cc3afce1cd7219c452dc8f642e4d1137e Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Tue, 29 May 2012 19:47:55 +0200 Subject: Refactor: use 'row' for the line number, and 'line' for the content of the line; re-enable debugging. --- pep8.py | 247 +++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 119 insertions(+), 128 deletions(-) diff --git a/pep8.py b/pep8.py index ae0cdb1..d4dad6b 100755 --- a/pep8.py +++ b/pep8.py @@ -432,9 +432,9 @@ def continuation_line_indentation(logical_line, tokens, indent_level): continuation line. """ - first_line = tokens[0][2][0] - last_line = tokens[-1][3][0] - if first_line == last_line: + first_row = tokens[0][2][0] + last_row = tokens[-1][3][0] + if first_row == last_row: return # indent_next tells us whether the next block is indented; assuming @@ -445,108 +445,106 @@ def continuation_line_indentation(logical_line, tokens, indent_level): depth = 0 - parens = [] # for looking back to see where a bracket was opened - max_physical_line = 0 - visual_min = [None] # visual indent columns by indent depth - rel_indent = [[0, 0]] # relative indents of physical lines + parens = [] # remember where a bracket was opened + max_physical_row = 0 + visual_min = [None] # visual indent columns by indent depth + rel_indent = [[0, 0]] # relative indents of physical lines last_indent = None last_backslash = [] - # if options.verbose >= 3: - # print ">>> " + tokens[0][4], + if options.verbose >= 3: + print(">>> " + tokens[0][4].rstrip()) - for token in tokens: - token_type, text, start, end, orig_line = token - line = start[0] - first_line - while len(parens) - 1 < line: + for token_type, text, start, end, line in tokens: + row = start[0] - first_row + while len(parens) < row + 1: parens.append([]) - if line > 0: - if max_physical_line < line and not token_type in ( - tokenize.NEWLINE, tokenize.NL): - # if depth > 0 and len(last_backslash) >= line and \ - # last_backslash[line - 1] is not None: - # yield last_backslash[line - 1], \ - # "E502 unnecessary continuation backslash" - # this is the beginning of a continuation line. - max_physical_line = line - last_indent = start - # if options.verbose >= 3: - # print "... " + orig_line, - - # record the initial indent. if lines were missing (eg, - # multi-line strings) then pad the list out - while len(rel_indent) - 1 < line: - rel_indent.append(None) - rel_indent[line] = [depth, start[1] - indent_level] - - if depth > 0: - # a bracket expression in a continuation line. - - # first, find the line that it was opened on - open_line = None - for open_line in range(line - 1, -1, -1): - if len(parens[open_line]) > 0: - break - start_col, end_col = parens[open_line][-1] - hang = rel_indent[line][1] - rel_indent[open_line][1] - else: - # an unbracketed continuation line (ie, backslash) - start_col = 0 - hang = rel_indent[line][1] - - err = None - # check to see if visual indenting is active - min_indent = visual_min[depth] - if min_indent is not None: - if start[1] < min_indent: - err = (start, 'E126 continuation line ' - 'insufficiently indented for visual ' - 'indent; hanging indents should have no ' - 'arguments on the first line') - - # for E121 - eff_depth = depth - if token_type == tokenize.OP and text in ']})': - eff_depth -= 1 - - # check that this line is either visually indented vs - # the opening parens, or a hanging indent. - if start[1] == last_token_multiline: - # continuing right after a multiline string is OK - pass - elif start[1] == start_col: - # visual. fine. - pass - elif min_indent is not None: - over_indent = start[1] - end_col - if start[1] > end_col and not ( - over_indent == 4 and indent_next): - err = (start, "E127 continuation line over-" - "indented for visual indent") - else: - # hanging indent. - if eff_depth != depth: - if hang != 0: - err = ( - start, 'E121 lines starting with a ' - 'closing bracket should be indented ' - "to match that of the opening " - "bracket's line" - ) - elif (hang <= 0): - err = (start, 'E124 continuation line ' - 'missing indent or outdented') - elif (hang % 4): - err = (start, 'E122 continuation line not ' - 'indented on a 4-space boundary') - elif (hang > 4) and not (hang == 8 and indent_next): - err = (start, "E125 continuation line over-" - "indented") - - if err: - # TODO Correctly yield successive errors - yield err - return + if max_physical_row < row and token_type not in ( + tokenize.NEWLINE, tokenize.NL): + # if depth > 0 and len(last_backslash) >= row and \ + # last_backslash[row - 1] is not None: + # yield last_backslash[row - 1], \ + # "E502 unnecessary continuation backslash" + # this is the beginning of a continuation line. + max_physical_row = row + last_indent = start + if options.verbose >= 3: + print("... " + line.rstrip()) + + # record the initial indent. if lines were missing (eg, + # multi-line strings) then pad the list out + while len(rel_indent) < row + 1: + rel_indent.append(None) + rel_indent[row] = [depth, start[1] - indent_level] + + if depth > 0: + # a bracket expression in a continuation line. + + # first, find the line that it was opened on + open_row = None + for open_row in range(row - 1, -1, -1): + if len(parens[open_row]) > 0: + break + start_col, end_col = parens[open_row][-1] + hang = rel_indent[row][1] - rel_indent[open_row][1] + else: + # an unbracketed continuation line (ie, backslash) + start_col = 0 + hang = rel_indent[row][1] + + err = None + # check to see if visual indenting is active + min_indent = visual_min[depth] + if min_indent is not None: + if start[1] < min_indent: + err = (start, 'E126 continuation line ' + 'insufficiently indented for visual ' + 'indent; hanging indents should have no ' + 'arguments on the first line') + + # for E121 + eff_depth = depth + if token_type == tokenize.OP and text in ']})': + eff_depth -= 1 + + # check that this line is either visually indented vs + # the opening parens, or a hanging indent. + if start[1] == last_token_multiline: + # continuing right after a multiline string is OK + pass + elif start[1] == start_col: + # visual. fine. + pass + elif min_indent is not None: + over_indent = start[1] - end_col + if start[1] > end_col and not ( + over_indent == 4 and indent_next): + err = (start, "E127 continuation line over-" + "indented for visual indent") + else: + # hanging indent. + if eff_depth != depth: + if hang != 0: + err = ( + start, 'E121 lines starting with a ' + 'closing bracket should be indented ' + "to match that of the opening " + "bracket's line" + ) + elif (hang <= 0): + err = (start, 'E124 continuation line ' + 'missing indent or outdented') + elif (hang % 4): + err = (start, 'E122 continuation line not ' + 'indented on a 4-space boundary') + elif (hang > 4) and not (hang == 8 and indent_next): + err = (start, "E125 continuation line over-" + "indented") + + if err: + # TODO Correctly yield successive errors + yield err + return # keep track of bracket depth and look for visual indenting if token_type == tokenize.OP and text in '([{': @@ -555,40 +553,33 @@ def continuation_line_indentation(logical_line, tokens, indent_level): assert len(visual_min) == depth + 1 if depth > 0: visual_min[depth] = visual_min[depth - 1] - parens[line].append([start[1], end[1]]) - # if options.verbose >= 4: - # print "bracket depth {d} seen, col {c}, visual min = {m}"\ - # .format( - # d=depth, - # c=start[1], - # m=visual_min[depth], - # ) - elif len(parens[line]) > 0 and token_type != tokenize.NL: + parens[row].append([start[1], end[1]]) + if options.verbose >= 4: + print("bracket depth %s seen, col %s, visual min = %s" % + (depth, start[1], visual_min[depth])) + elif len(parens[row]) > 0 and token_type != tokenize.NL: # text after an open parens starts visual indenting if visual_min[depth] is None: visual_min[depth] = start[1] - # if options.verbose >= 4: - # print "bracket depth {d} indent to {c}".format( - # d=depth, - # c=start[1], - # ) + if options.verbose >= 4: + print("bracket depth %s indent to %s" % (depth, start[1])) if token_type == tokenize.OP and text in ')]}': depth -= 1 visual_min.pop() - for open_line in range(line, -1, -1): - if len(parens[open_line]) > 0: - parens[open_line].pop() + for open_row in range(row, -1, -1): + if len(parens[open_row]) > 0: + parens[open_row].pop() break - if len(parens[open_line]): - visual_min[depth] = parens[open_line][-1][1] + if len(parens[open_row]): + visual_min[depth] = parens[open_row][-1][1] # exceptions that need to processed at the end of lines - if len(last_backslash) - 1 < line: - while len(last_backslash) < line: + if len(last_backslash) - 1 < row: + while len(last_backslash) < row: last_backslash.append(None) - if orig_line.endswith('\\\n'): - last_backslash.append((end[0], len(orig_line) - 2)) + if line.endswith('\\\n'): + last_backslash.append((end[0], len(line) - 2)) else: last_backslash.append(None) @@ -1187,16 +1178,16 @@ class Checker(object): if token_type == tokenize.STRING: text = mute_string(text) if previous: - end_line, end = previous[3] - start_line, start = token[2] - if end_line != start_line: # different row - prev_text = self.lines[end_line - 1][end - 1] + end_row, end = previous[3] + start_row, start = token[2] + if end_row != start_row: # different row + prev_text = self.lines[end_row - 1][end - 1] if prev_text == ',' or (prev_text not in '{[(' and text not in '}])'): logical.append(' ') length += 1 elif end != start: # different column - fill = self.lines[end_line - 1][end:start] + fill = self.lines[end_row - 1][end:start] logical.append(fill) length += len(fill) self.mapping.append((length, token)) -- cgit v1.2.1