def tidy_indent():
    global line_indent
    global indent
    line_indent = 0
    indent = 0
    line = create_line()
    line_key_dict = {}
    bracket_key_dict = {}

    def deal_indent(line, delta=0):
        line.set_indent((indent + delta))

    def inc_indent(delta):
        global line_indent
        global indent
        if ((line_indent + delta) > 1):
            return
        if ((line_indent + delta) < (- 1)):
            return
        line_indent += delta
        indent += delta
        if (indent < 0):
            indent = 0
    for node in NodeIterator():
        line.add(node)
        key = str(node)
        line_key_dict[key] = (line_key_dict.get(key, 0) + 1)
        if ((node.type is NodeType.BRACKET) or (node.type is NodeType.REVERSE_BRACKET)):
            bracket_key_dict[key] = (bracket_key_dict.get(key, 0) + 1)
        if (node.type is NodeType.ENTER):
            inc_indent((1 if (line_key_dict.get('(', 0) > line_key_dict.get(')', 0)) else 0))
            inc_indent((1 if (line_key_dict.get('{', 0) > line_key_dict.get('}', 0)) else 0))
            inc_indent((1 if (line_key_dict.get('[', 0) > line_key_dict.get(']', 0)) else 0))
            if (line_key_dict.get('(', 0) < line_key_dict.get(')', 0)):
                inc_indent((- 1))
                deal_indent(line)
            if (line_key_dict.get('{', 0) < line_key_dict.get('}', 0)):
                inc_indent((- 1))
                deal_indent(line)
            if (line_key_dict.get('[', 0) < line_key_dict.get(']', 0)):
                inc_indent((- 1))
                deal_indent(line)
            do_count = line_key_dict.get('do', 0)
            end_count = line_key_dict.get('end', 0)
            if ((do_count > 0) and (do_count <= end_count)):
                indent += (end_count - do_count)
                deal_indent(line)
                line = create_line()
            else:
                line = create_line()
                deal_indent(line)
            line_indent = 0
            del line_key_dict
            line_key_dict = {}
        if ((str(node) == 'else') or (str(node) == 'elseif')):
            deal_indent(line, (- 1))
        if (str(node) in IndentKeyword):
            inc_indent(1)
        if (str(node) in UnindentKeyword):
            inc_indent((- 1))
            deal_indent(line)
def tidy_indent():
    global line_indent
    global indent
    line_indent = 0
    indent = 0
    line = create_line()
    line_key_dict = {}
    bracket_key_dict = {}

    def deal_indent(line, delta=0):
        line.set_indent((indent + delta))

    def inc_indent(delta):
        global line_indent
        global indent
        if ((line_indent + delta) > 1):
            return
        if ((line_indent + delta) < (- 1)):
            return
        line_indent += delta
        indent += delta
        if (indent < 0):
            indent = 0
    for node in NodeIterator():
        line.add(node)
        key = str(node)
        line_key_dict[key] = (line_key_dict.get(key, 0) + 1)
        if ((node.type is NodeType.BRACKET) or (node.type is NodeType.REVERSE_BRACKET)):
            bracket_key_dict[key] = (bracket_key_dict.get(key, 0) + 1)
        if (node.type is NodeType.ENTER):
            inc_indent((1 if (line_key_dict.get('(', 0) > line_key_dict.get(')', 0)) else 0))
            inc_indent((1 if (line_key_dict.get('{', 0) > line_key_dict.get('}', 0)) else 0))
            inc_indent((1 if (line_key_dict.get('[', 0) > line_key_dict.get(']', 0)) else 0))
            if (line_key_dict.get('(', 0) < line_key_dict.get(')', 0)):
                inc_indent((- 1))
                deal_indent(line)
            if (line_key_dict.get('{', 0) < line_key_dict.get('}', 0)):
                inc_indent((- 1))
                deal_indent(line)
            if (line_key_dict.get('[', 0) < line_key_dict.get(']', 0)):
                inc_indent((- 1))
                deal_indent(line)
            do_count = line_key_dict.get('do', 0)
            end_count = line_key_dict.get('end', 0)
            if ((do_count > 0) and (do_count <= end_count)):
                indent += (end_count - do_count)
                deal_indent(line)
                line = create_line()
            else:
                line = create_line()
                deal_indent(line)
            line_indent = 0
            del line_key_dict
            line_key_dict = {}
        if ((str(node) == 'else') or (str(node) == 'elseif')):
            deal_indent(line, (- 1))
        if (str(node) in IndentKeyword):
            inc_indent(1)
        if (str(node) in UnindentKeyword):
            inc_indent((- 1))
            deal_indent(line)