예제 #1
0
    def visit_Slice(self, node):
        # Slice(lower=Expr,
        #       upper=Expr,
        #       step=Expr)
        tokens = self._GetTokens(node)

        if hasattr(node, 'lower'):
            subrange = pyutils.GetTokensInSubRange(
                tokens, pyutils.TokenStart(node.lower),
                pyutils.TokenEnd(node.lower))
            _IncreasePenalty(subrange, split_penalty.EXPR)
            _DecreasePenalty(subrange[0], split_penalty.EXPR // 2)

        if hasattr(node, 'upper'):
            colon_index = pyutils.GetPrevTokenIndex(
                tokens, pyutils.TokenStart(node.upper))
            _IncreasePenalty(tokens[colon_index], split_penalty.UNBREAKABLE)
            subrange = pyutils.GetTokensInSubRange(
                tokens, pyutils.TokenStart(node.upper),
                pyutils.TokenEnd(node.upper))
            _IncreasePenalty(subrange, split_penalty.EXPR)
            _DecreasePenalty(subrange[0], split_penalty.EXPR // 2)

        if hasattr(node, 'step'):
            colon_index = pyutils.GetPrevTokenIndex(
                tokens, pyutils.TokenStart(node.step))
            _IncreasePenalty(tokens[colon_index], split_penalty.UNBREAKABLE)
            subrange = pyutils.GetTokensInSubRange(
                tokens, pyutils.TokenStart(node.step),
                pyutils.TokenEnd(node.step))
            _IncreasePenalty(subrange, split_penalty.EXPR)
            _DecreasePenalty(subrange[0], split_penalty.EXPR // 2)

        return self.generic_visit(node)
예제 #2
0
    def visit_DictComp(self, node):
        # DictComp(key=KeyExpr,
        #          value=ValExpr,
        #          generators=[
        #              comprehension(
        #                  target=TargetExpr
        #                  iter=IterExpr,
        #                  ifs=[Expr_1, Expr_2, ..., Expr_n]),
        #                  is_async=0)],
        #           ...
        #         ])
        tokens = self._GetTokens(node)
        key = pyutils.GetTokensInSubRange(tokens, pyutils.TokenStart(node.key),
                                          pyutils.TokenEnd(node.key))
        _IncreasePenalty(key[1:], split_penalty.EXPR)

        value = pyutils.GetTokensInSubRange(tokens,
                                            pyutils.TokenStart(node.value),
                                            pyutils.TokenEnd(node.value))
        _IncreasePenalty(value[1:], split_penalty.EXPR)

        for comp in node.generators:
            subrange = pyutils.GetTokensInSubRange(
                tokens, pyutils.TokenStart(comp.iter),
                pyutils.TokenEnd(comp.iter))
            _IncreasePenalty(subrange[1:], split_penalty.EXPR)

            for if_expr in comp.ifs:
                subrange = pyutils.GetTokensInSubRange(
                    tokens, pyutils.TokenStart(if_expr),
                    pyutils.TokenEnd(if_expr))
                _IncreasePenalty(subrange[1:], split_penalty.EXPR)
        return self.generic_visit(node)
예제 #3
0
    def visit_GeneratorExp(self, node):
        # GeneratorExp(elt=Expr,
        #              generators=[
        #                  comprehension(
        #                      target=Expr,
        #                      iter=Expr,
        #                      ifs=[Expr_1, Expr_2, ..., Expr_n],
        #                      is_async=0),
        #                ...
        #              ])
        tokens = self._GetTokens(node)
        element = pyutils.GetTokensInSubRange(tokens,
                                              pyutils.TokenStart(node.elt),
                                              pyutils.TokenEnd(node.elt))
        _IncreasePenalty(element[1:], split_penalty.EXPR)

        for comp in node.generators:
            subrange = pyutils.GetTokensInSubRange(
                tokens, pyutils.TokenStart(comp.iter),
                pyutils.TokenEnd(comp.iter))
            _IncreasePenalty(subrange[1:], split_penalty.EXPR)

            for if_expr in comp.ifs:
                subrange = pyutils.GetTokensInSubRange(
                    tokens, pyutils.TokenStart(if_expr),
                    pyutils.TokenEnd(if_expr))
                _IncreasePenalty(subrange[1:], split_penalty.EXPR)
        return self.generic_visit(node)
예제 #4
0
    def visit_FunctionDef(self, node):
        # FunctionDef(name=Name,
        #             args=arguments(
        #                 posonlyargs=[],
        #                 args=[],
        #                 vararg=[],
        #                 kwonlyargs=[],
        #                 kw_defaults=[],
        #                 defaults=[]),
        #             body=[...],
        #             decorator_list=[Expr_1, Expr_2, ..., Expr_n],
        #             keywords=[])
        for decorator in node.decorator_list:
            # Don't split after the '@'.
            tokens = self._GetTokens(decorator)
            tokens[0].split_penalty = split_penalty.UNBREAKABLE

        tokens = self._GetTokens(node)

        if node.returns:
            start_index = pyutils.GetTokenIndex(
                tokens, pyutils.TokenStart(node.returns))
            _IncreasePenalty(tokens[start_index - 1:start_index + 1],
                             split_penalty.VERY_STRONGLY_CONNECTED)
            end_index = pyutils.GetTokenIndex(tokens,
                                              pyutils.TokenEnd(node.returns))
            _IncreasePenalty(tokens[start_index + 1:end_index],
                             split_penalty.STRONGLY_CONNECTED)

        return self.generic_visit(node)
예제 #5
0
 def visit_Set(self, node):
     # Set(elts=[Expr_1, Expr_2, ..., Expr_n])
     tokens = self._GetTokens(node)
     for element in node.elts:
         subrange = pyutils.GetTokensInSubRange(tokens,
                                                pyutils.TokenStart(element),
                                                pyutils.TokenEnd(element))
         _IncreasePenalty(subrange[1:], split_penalty.EXPR)
     return self.generic_visit(node)
예제 #6
0
    def visit_Dict(self, node):
        # Dict(keys=[Expr_1, Expr_2, ..., Expr_n],
        #      values=[Expr_1, Expr_2, ..., Expr_n])
        tokens = self._GetTokens(node)

        # The keys should be on a single line if at all possible.
        for key in node.keys:
            subrange = pyutils.GetTokensInSubRange(tokens,
                                                   pyutils.TokenStart(key),
                                                   pyutils.TokenEnd(key))
            _IncreasePenalty(subrange[1:], split_penalty.DICT_KEY_EXPR)

        for value in node.values:
            subrange = pyutils.GetTokensInSubRange(tokens,
                                                   pyutils.TokenStart(value),
                                                   pyutils.TokenEnd(value))
            _IncreasePenalty(subrange[1:], split_penalty.DICT_VALUE_EXPR)
        return self.generic_visit(node)
예제 #7
0
    def visit_Subscript(self, node):
        # Subscript(value=ValueExpr,
        #           slice=SliceExpr)
        tokens = self._GetTokens(node)

        # Don't split before the opening bracket of a subscript.
        bracket_index = pyutils.GetNextTokenIndex(tokens,
                                                  pyutils.TokenEnd(node.value))
        _IncreasePenalty(tokens[bracket_index], split_penalty.UNBREAKABLE)
        return self.generic_visit(node)
예제 #8
0
    def visit_Compare(self, node):
        # Compare(left=LExpr,
        #         ops=[Op_1, Op_2, ..., Op_n],
        #         comparators=[Expr_1, Expr_2, ..., Expr_n])
        tokens = self._GetTokens(node)
        _IncreasePenalty(tokens[1:], split_penalty.EXPR)

        operator_indices = [
            pyutils.GetNextTokenIndex(tokens, pyutils.TokenEnd(node.left))
        ] + [
            pyutils.GetNextTokenIndex(tokens, pyutils.TokenEnd(comparator))
            for comparator in node.comparators[:-1]
        ]
        split_before = style.Get('SPLIT_BEFORE_ARITHMETIC_OPERATOR')
        for operator_index in operator_indices:
            if not split_before:
                operator_index += 1
            _DecreasePenalty(tokens[operator_index], split_penalty.EXPR * 2)
        return self.generic_visit(node)
예제 #9
0
 def visit_Attribute(self, node):
     # Attribute(value=Expr,
     #           attr=Identifier)
     tokens = self._GetTokens(node)
     split_before = style.Get('SPLIT_BEFORE_DOT')
     dot_indices = pyutils.GetNextTokenIndex(tokens,
                                             pyutils.TokenEnd(node.value))
     if not split_before:
         dot_indices += 1
     _IncreasePenalty(tokens[dot_indices],
                      split_penalty.VERY_STRONGLY_CONNECTED)
     return self.generic_visit(node)
예제 #10
0
    def visit_Call(self, node):
        # Call(func=Expr,
        #      args=[Expr_1, Expr_2, ..., Expr_n],
        #      keywords=[
        #          keyword(
        #              arg='d',
        #              value=Expr),
        #            ...
        #      ])
        tokens = self._GetTokens(node)

        # Don't never split before the opening parenthesis.
        paren_index = pyutils.GetNextTokenIndex(tokens,
                                                pyutils.TokenEnd(node.func))
        _IncreasePenalty(tokens[paren_index], split_penalty.UNBREAKABLE)

        for arg in node.args:
            subrange = pyutils.GetTokensInSubRange(tokens,
                                                   pyutils.TokenStart(arg),
                                                   pyutils.TokenEnd(arg))
            _IncreasePenalty(subrange[1:], split_penalty.EXPR)
        return self.generic_visit(node)
예제 #11
0
    def visit_BinOp(self, node):
        # BinOp(left=LExpr
        #       op=Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift |
        #          RShift | BitOr | BitXor | BitAnd | FloorDiv
        #       right=RExpr)
        tokens = self._GetTokens(node)
        _IncreasePenalty(tokens[1:], split_penalty.EXPR)

        # Lower the split penalty to allow splitting before or after the arithmetic
        # operator.
        operator_index = pyutils.GetNextTokenIndex(tokens,
                                                   pyutils.TokenEnd(node.left))
        if not style.Get('SPLIT_BEFORE_ARITHMETIC_OPERATOR'):
            operator_index += 1
        _DecreasePenalty(tokens[operator_index], split_penalty.EXPR * 2)
        return self.generic_visit(node)
예제 #12
0
    def visit_BoolOp(self, node):
        # BoolOp(op=And | Or,
        #        values=[Expr_1, Expr_2, ..., Expr_n])
        tokens = self._GetTokens(node)
        _IncreasePenalty(tokens[1:], split_penalty.EXPR)

        # Lower the split penalty to allow splitting before or after the logical
        # operator.
        split_before_operator = style.Get('SPLIT_BEFORE_LOGICAL_OPERATOR')
        operator_indices = [
            pyutils.GetNextTokenIndex(tokens, pyutils.TokenEnd(value))
            for value in node.values[:-1]
        ]
        for operator_index in operator_indices:
            if not split_before_operator:
                operator_index += 1
            _DecreasePenalty(tokens[operator_index], split_penalty.EXPR * 2)

        return self.generic_visit(node)