Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_SetSplitPenalty(
pytree_utils.LastLeafNode(node.children[1].children[1]), 0)
else:
_SetSplitPenalty(
pytree_utils.FirstLeafNode(node.children[1].children[2]), 0)
# Don't split the ending bracket of a subscript list.
_SetVeryStronglyConnected(node.children[-1])
elif name not in {
'arglist', 'argument', 'term', 'or_test', 'and_test', 'comparison',
'atom', 'power'
}:
# Don't split an argument list with one element if at all possible.
subtypes = pytree_utils.GetNodeAnnotation(
pytree_utils.FirstLeafNode(node), pytree_utils.Annotation.SUBTYPE)
if subtypes and format_token.Subtype.SUBSCRIPT_BRACKET in subtypes:
_IncreasePenalty(node, SUBSCRIPT)
# Bump up the split penalty for the first part of a subscript. We
# would rather not split there.
_IncreasePenalty(node.children[1], CONNECTED)
else:
_SetStronglyConnected(node.children[1], node.children[2])
if name == 'arglist':
_SetStronglyConnected(node.children[-1])
self.DefaultNodeVisit(node)
def Visit_term(self, node): # pylint: disable=invalid-name
# term ::= factor (('*'|'/'|'%'|'//'|'@') factor)*
for child in node.children:
self.Visit(child)
if _IsMExprOperator(child):
_AppendTokenSubtype(child, format_token.Subtype.BINARY_OPERATOR)
_AppendTokenSubtype(child, format_token.Subtype.M_EXPR_OPERATOR)
if _IsSimpleExpression(node):
for child in node.children:
if _IsMExprOperator(child):
_AppendTokenSubtype(child, format_token.Subtype.SIMPLE_EXPRESSION)
if not colon.is_pseudo_paren:
break
colon = colon.previous_token
if not colon or colon.value != ':':
return False
key = colon.previous_token
if not key:
return False
return format_token.Subtype.DICTIONARY_KEY_PART in key.subtypes
closing = opening.matching_bracket
entry_start = opening.next_token
current = opening.next_token.next_token
while current and current != closing:
if format_token.Subtype.DICTIONARY_KEY in current.subtypes:
prev = PreviousNonCommentToken(current)
if prev.value == ',':
prev = PreviousNonCommentToken(prev.previous_token)
if not DictValueIsContainer(prev.matching_bracket, prev):
length = prev.total_length - entry_start.total_length
length += len(entry_start.value)
if length + self.stack[-2].indent >= self.column_limit:
return False
entry_start = current
if current.OpensScope():
if ((current.value == '{' or
(current.is_pseudo_paren and current.next_token.value == '{') and
format_token.Subtype.DICTIONARY_VALUE in current.subtypes) or
ImplicitStringConcatenation(current)):
# A dictionary entry that cannot fit on a single line shouldn't matter
# to this calculation. If it can't fit on a single line, then the
def DictValueIsContainer(opening, closing):
if not opening or not closing:
return False
colon = opening.previous_token
while colon:
if not colon.is_pseudo_paren:
break
colon = colon.previous_token
if not colon or colon.value != ':':
return False
key = colon.previous_token
if not key:
return False
return format_token.Subtype.DICTIONARY_KEY_PART in key.subtypes
return True
if lval == 'from' and rval == '.':
# Space before the '.' in an import statement.
return True
if lval == '.' and rval == 'import':
# Space after the '.' in an import statement.
return True
if (lval == '=' and rval == '.' and
format_token.Subtype.DEFAULT_OR_NAMED_ASSIGN not in left.subtypes):
# Space between equal and '.' as in "X = ...".
return True
if ((right.is_keyword or right.is_name) and
(left.is_keyword or left.is_name)):
# Don't merge two keywords/identifiers.
return True
if (format_token.Subtype.SUBSCRIPT_COLON in left.subtypes or
format_token.Subtype.SUBSCRIPT_COLON in right.subtypes):
# A subscript shouldn't have spaces separating its colons.
return False
if (format_token.Subtype.TYPED_NAME in left.subtypes or
format_token.Subtype.TYPED_NAME in right.subtypes):
# A typed argument should have a space after the colon.
return True
if left.is_string:
if (rval == '=' and
format_token.Subtype.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST in right.subtypes
):
# If there is a type hint, then we don't want to add a space between the
# equal sign and the hint.
return False
if rval not in '[)]}.' and not right.is_binary_op:
# A string followed by something other than a subscript, closing bracket,
def AppendNode(self, node):
"""Convenience method to append a pytree node directly.
Wraps the node with a FormatToken.
Arguments:
node: the node to append
"""
self.AppendToken(format_token.FormatToken(node))
def RecSplicer(node):
"""Inserts a continuation marker into the node."""
if isinstance(node, pytree.Leaf):
if node.prefix.lstrip().startswith('\\\n'):
new_lineno = node.lineno - node.prefix.count('\n')
return pytree.Leaf(
type=format_token.CONTINUATION,
value=node.prefix,
context=('', (new_lineno, 0)))
return None
num_inserted = 0
for index, child in enumerate(node.children[:]):
continuation_node = RecSplicer(child)
if continuation_node:
node.children.insert(index + num_inserted, continuation_node)
num_inserted += 1
def _IdentifyParameterLists(uwline):
"""Visit the node to create a state for parameter lists.
For instance, a parameter is considered an "object" with its first and last
token uniquely identifying the object.
Arguments:
uwline: (UnwrappedLine) An unwrapped line.
"""
func_stack = []
param_stack = []
for tok in uwline.tokens:
# Identify parameter list objects.
if format_token.Subtype.FUNC_DEF in tok.subtypes:
assert tok.next_token.value == '('
func_stack.append(tok.next_token)
continue
if func_stack and tok.value == ')':
if tok == func_stack[-1].matching_bracket:
func_stack.pop()
continue
# Identify parameter objects.
if format_token.Subtype.PARAMETER_START in tok.subtypes:
param_stack.append(tok)
# Not "elif", a parameter could be a single token.
if param_stack and format_token.Subtype.PARAMETER_STOP in tok.subtypes:
start = param_stack.pop()
def has_default_value(self):
"""Returns true if the parameter has a default value."""
tok = self.first_token
while tok != self.last_token:
if format_token.Subtype.DEFAULT_OR_NAMED_ASSIGN in tok.subtypes:
return True
if tok.OpensScope():
tok = tok.matching_bracket
else:
tok = tok.next_token
return False
if format_token.Subtype.FUNC_DEF in tok.subtypes:
assert tok.next_token.value == '('
func_stack.append(tok.next_token)
continue
if func_stack and tok.value == ')':
if tok == func_stack[-1].matching_bracket:
func_stack.pop()
continue
# Identify parameter objects.
if format_token.Subtype.PARAMETER_START in tok.subtypes:
param_stack.append(tok)
# Not "elif", a parameter could be a single token.
if param_stack and format_token.Subtype.PARAMETER_STOP in tok.subtypes:
start = param_stack.pop()
func_stack[-1].parameters.append(object_state.Parameter(start, tok))