Use OpensScope/ClosesScope instead of testing token's text

This commit is contained in:
Bill Wendling 2022-01-01 17:22:18 -08:00
parent c8d9637ac6
commit ebd977af00
4 changed files with 22 additions and 21 deletions

View File

@ -40,6 +40,9 @@ from yapf.yapflib import object_state
from yapf.yapflib import style
from yapf.yapflib import subtypes
_OPENING_BRACKETS = frozenset({'(', '[', '{'})
_CLOSING_BRACKETS = frozenset({')', ']', '}'})
def UnwrapPyTree(tree):
"""Create and return a list of logical lines from the given pytree.
@ -312,9 +315,9 @@ def _MatchBrackets(line):
"""
bracket_stack = []
for token in line.tokens:
if token.value in pytree_utils.OPENING_BRACKETS:
if token.value in _OPENING_BRACKETS:
bracket_stack.append(token)
elif token.value in pytree_utils.CLOSING_BRACKETS:
elif token.value in _CLOSING_BRACKETS:
bracket_stack[-1].matching_bracket = token
token.matching_bracket = bracket_stack[-1]
bracket_stack.pop()
@ -373,9 +376,9 @@ def _AdjustSplitPenalty(line):
pytree_utils.SetNodeAnnotation(token.node,
pytree_utils.Annotation.SPLIT_PENALTY,
split_penalty.UNBREAKABLE)
if token.value in pytree_utils.OPENING_BRACKETS:
if token.value in _OPENING_BRACKETS:
bracket_level += 1
elif token.value in pytree_utils.CLOSING_BRACKETS:
elif token.value in _CLOSING_BRACKETS:
bracket_level -= 1

View File

@ -39,9 +39,6 @@ from lib2to3.pgen2 import token
# unwrapper.
NONSEMANTIC_TOKENS = frozenset(['DEDENT', 'INDENT', 'NEWLINE', 'ENDMARKER'])
OPENING_BRACKETS = frozenset({'(', '[', '{'})
CLOSING_BRACKETS = frozenset({')', ']', '}'})
class Annotation(object):
"""Annotation names associated with pytrees."""

View File

@ -25,6 +25,9 @@ from yapf.yapflib import subtypes
CONTINUATION = token.N_TOKENS
_OPENING_BRACKETS = frozenset({'(', '[', '{'})
_CLOSING_BRACKETS = frozenset({')', ']', '}'})
def _TabbedContinuationAlignPadding(spaces, align_style, tab_width):
"""Build padding string for continuation alignment in tabbed indentation.
@ -207,10 +210,10 @@ class FormatToken(object):
self.spaces_required_before = cur_column - (prev_column + prev_len)
def OpensScope(self):
return self.value in pytree_utils.OPENING_BRACKETS
return self.value in _OPENING_BRACKETS
def ClosesScope(self):
return self.value in pytree_utils.CLOSING_BRACKETS
return self.value in _CLOSING_BRACKETS
def AddSubtype(self, subtype):
self.subtypes.add(subtype)

View File

@ -302,9 +302,9 @@ def _SpaceRequiredBetween(left, right, is_line_disabled):
return True
if style.Get('SPACE_INSIDE_BRACKETS'):
# Supersede the "no space before a colon or comma" check.
if lval in pytree_utils.OPENING_BRACKETS and rval == ':':
if left.OpensScope() and rval == ':':
return True
if rval in pytree_utils.CLOSING_BRACKETS and lval == ':':
if right.ClosesScope() and lval == ':':
return True
if (style.Get('SPACES_AROUND_SUBSCRIPT_COLON') and
(_IsSubscriptColonAndValuePair(left, right) or
@ -355,7 +355,7 @@ def _SpaceRequiredBetween(left, right, is_line_disabled):
# A string followed by something other than a subscript, closing bracket,
# dot, or a binary op should have a space after it.
return True
if rval in pytree_utils.CLOSING_BRACKETS:
if right.ClosesScope():
# A string followed by closing brackets should have a space after it
# depending on SPACE_INSIDE_BRACKETS. A string followed by opening
# brackets, however, should not.
@ -439,28 +439,26 @@ def _SpaceRequiredBetween(left, right, is_line_disabled):
(rval == ')' and
_IsDictListTupleDelimiterTok(right, is_opening=False)))):
return True
if (lval in pytree_utils.OPENING_BRACKETS and
rval in pytree_utils.OPENING_BRACKETS):
if left.OpensScope() and right.OpensScope():
# Nested objects' opening brackets shouldn't be separated, unless enabled
# by SPACE_INSIDE_BRACKETS.
return style.Get('SPACE_INSIDE_BRACKETS')
if (lval in pytree_utils.CLOSING_BRACKETS and
rval in pytree_utils.CLOSING_BRACKETS):
if left.ClosesScope() and right.ClosesScope():
# Nested objects' closing brackets shouldn't be separated, unless enabled
# by SPACE_INSIDE_BRACKETS.
return style.Get('SPACE_INSIDE_BRACKETS')
if lval in pytree_utils.CLOSING_BRACKETS and rval in '([':
if left.ClosesScope() and rval in '([':
# A call, set, dictionary, or subscript that has a call or subscript after
# it shouldn't have a space between them.
return False
if lval in pytree_utils.OPENING_BRACKETS and _IsIdNumberStringToken(right):
if left.OpensScope() and _IsIdNumberStringToken(right):
# Don't separate the opening bracket from the first item, unless enabled
# by SPACE_INSIDE_BRACKETS.
return style.Get('SPACE_INSIDE_BRACKETS')
if left.is_name and rval in '([':
# Don't separate a call or array access from the name.
return False
if rval in pytree_utils.CLOSING_BRACKETS:
if right.ClosesScope():
# Don't separate the closing bracket from the last item, unless enabled
# by SPACE_INSIDE_BRACKETS.
# FIXME(morbo): This might be too permissive.
@ -468,11 +466,11 @@ def _SpaceRequiredBetween(left, right, is_line_disabled):
if lval == 'print' and rval == '(':
# Special support for the 'print' function.
return False
if lval in pytree_utils.OPENING_BRACKETS and _IsUnaryOperator(right):
if left.OpensScope() and _IsUnaryOperator(right):
# Don't separate a unary operator from the opening bracket, unless enabled
# by SPACE_INSIDE_BRACKETS.
return style.Get('SPACE_INSIDE_BRACKETS')
if (lval in pytree_utils.OPENING_BRACKETS and
if (left.OpensScope() and
(subtypes.VARARGS_STAR in right.subtypes or
subtypes.KWARGS_STAR_STAR in right.subtypes)):
# Don't separate a '*' or '**' from the opening bracket, unless enabled