示例#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_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)
示例#3
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)
示例#4
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)
示例#5
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)
示例#6
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)