parent
0fee494946
commit
ba205258d9
|
@ -297,7 +297,8 @@ class PyTreeUnwrapper(pytree_visitor.PyTreeVisitor):
|
|||
self._StartNewLine()
|
||||
elif leaf.type != grammar_token.COMMENT or leaf.value.strip():
|
||||
# Add non-whitespace tokens and comments that aren't empty.
|
||||
self._cur_logical_line.AppendToken(format_token.FormatToken(leaf))
|
||||
self._cur_logical_line.AppendToken(
|
||||
format_token.FormatToken(leaf, pytree_utils.NodeName(leaf)))
|
||||
|
||||
|
||||
_BRACKET_MATCH = {')': '(', '}': '{', ']': '['}
|
||||
|
|
|
@ -83,14 +83,23 @@ class FormatToken(object):
|
|||
newlines: The number of newlines needed before this token.
|
||||
"""
|
||||
|
||||
def __init__(self, node, node_name=None):
|
||||
def __init__(self, node, name):
|
||||
"""Constructor.
|
||||
|
||||
Arguments:
|
||||
node: (pytree.Leaf) The node that's being wrapped.
|
||||
none_name: (string) The name of the node.
|
||||
name: (string) The name of the node.
|
||||
"""
|
||||
self.node = node
|
||||
self.name = name
|
||||
self.type = node.type
|
||||
self.column = node.column
|
||||
self.lineno = node.lineno
|
||||
self.value = node.value
|
||||
|
||||
if self.is_continuation:
|
||||
self.value = node.value.rstrip()
|
||||
|
||||
self.next_token = None
|
||||
self.previous_token = None
|
||||
self.matching_bracket = None
|
||||
|
@ -105,20 +114,11 @@ class FormatToken(object):
|
|||
node, pytree_utils.Annotation.MUST_SPLIT, default=False)
|
||||
self.newlines = pytree_utils.GetNodeAnnotation(
|
||||
node, pytree_utils.Annotation.NEWLINES)
|
||||
|
||||
self.type = node.type
|
||||
self.column = node.column
|
||||
self.lineno = node.lineno
|
||||
self.name = pytree_utils.NodeName(node) if not node_name else node_name
|
||||
|
||||
self.spaces_required_before = 0
|
||||
|
||||
if self.is_comment:
|
||||
self.spaces_required_before = style.Get('SPACES_BEFORE_COMMENT')
|
||||
|
||||
self.value = node.value
|
||||
if self.is_continuation:
|
||||
self.value = node.value.rstrip()
|
||||
|
||||
stypes = pytree_utils.GetNodeAnnotation(node,
|
||||
pytree_utils.Annotation.SUBTYPE)
|
||||
self.subtypes = {subtypes.NONE} if not stypes else stypes
|
||||
|
|
|
@ -738,7 +738,8 @@ def _SingleOrMergedLines(lines):
|
|||
if line.last.value != ':':
|
||||
leaf = pytree.Leaf(
|
||||
type=token.SEMI, value=';', context=('', (line.lineno, column)))
|
||||
line.AppendToken(format_token.FormatToken(leaf))
|
||||
line.AppendToken(
|
||||
format_token.FormatToken(leaf, pytree_utils.NodeName(leaf)))
|
||||
for tok in lines[index].tokens:
|
||||
line.AppendToken(tok)
|
||||
index += 1
|
||||
|
|
|
@ -66,23 +66,27 @@ class TabbedContinuationAlignPaddingTest(unittest.TestCase):
|
|||
class FormatTokenTest(unittest.TestCase):
|
||||
|
||||
def testSimple(self):
|
||||
tok = format_token.FormatToken(pytree.Leaf(token.STRING, "'hello world'"))
|
||||
tok = format_token.FormatToken(
|
||||
pytree.Leaf(token.STRING, "'hello world'"), 'STRING')
|
||||
self.assertEqual(
|
||||
"FormatToken(name=DOCSTRING, value='hello world', column=0, "
|
||||
"lineno=0, splitpenalty=0)", str(tok))
|
||||
self.assertTrue(tok.is_string)
|
||||
|
||||
tok = format_token.FormatToken(pytree.Leaf(token.COMMENT, '# A comment'))
|
||||
tok = format_token.FormatToken(
|
||||
pytree.Leaf(token.COMMENT, '# A comment'), 'COMMENT')
|
||||
self.assertEqual(
|
||||
'FormatToken(name=COMMENT, value=# A comment, column=0, '
|
||||
'lineno=0, splitpenalty=0)', str(tok))
|
||||
self.assertTrue(tok.is_comment)
|
||||
|
||||
def testIsMultilineString(self):
|
||||
tok = format_token.FormatToken(pytree.Leaf(token.STRING, '"""hello"""'))
|
||||
tok = format_token.FormatToken(
|
||||
pytree.Leaf(token.STRING, '"""hello"""'), 'STRING')
|
||||
self.assertTrue(tok.is_multiline_string)
|
||||
|
||||
tok = format_token.FormatToken(pytree.Leaf(token.STRING, 'r"""hello"""'))
|
||||
tok = format_token.FormatToken(
|
||||
pytree.Leaf(token.STRING, 'r"""hello"""'), 'STRING')
|
||||
self.assertTrue(tok.is_multiline_string)
|
||||
|
||||
|
||||
|
|
|
@ -29,29 +29,32 @@ from yapftests import yapf_test_helper
|
|||
class LogicalLineBasicTest(unittest.TestCase):
|
||||
|
||||
def testConstruction(self):
|
||||
toks = _MakeFormatTokenList([(token.DOT, '.'), (token.VBAR, '|')])
|
||||
toks = _MakeFormatTokenList([(token.DOT, '.', 'DOT'),
|
||||
(token.VBAR, '|', 'VBAR')])
|
||||
lline = logical_line.LogicalLine(20, toks)
|
||||
self.assertEqual(20, lline.depth)
|
||||
self.assertEqual(['DOT', 'VBAR'], [tok.name for tok in lline.tokens])
|
||||
|
||||
def testFirstLast(self):
|
||||
toks = _MakeFormatTokenList([(token.DOT, '.'), (token.LPAR, '('),
|
||||
(token.VBAR, '|')])
|
||||
toks = _MakeFormatTokenList([(token.DOT, '.', 'DOT'),
|
||||
(token.LPAR, '(', 'LPAR'),
|
||||
(token.VBAR, '|', 'VBAR')])
|
||||
lline = logical_line.LogicalLine(20, toks)
|
||||
self.assertEqual(20, lline.depth)
|
||||
self.assertEqual('DOT', lline.first.name)
|
||||
self.assertEqual('VBAR', lline.last.name)
|
||||
|
||||
def testAsCode(self):
|
||||
toks = _MakeFormatTokenList([(token.DOT, '.'), (token.LPAR, '('),
|
||||
(token.VBAR, '|')])
|
||||
toks = _MakeFormatTokenList([(token.DOT, '.', 'DOT'),
|
||||
(token.LPAR, '(', 'LPAR'),
|
||||
(token.VBAR, '|', 'VBAR')])
|
||||
lline = logical_line.LogicalLine(2, toks)
|
||||
self.assertEqual(' . ( |', lline.AsCode())
|
||||
|
||||
def testAppendToken(self):
|
||||
lline = logical_line.LogicalLine(0)
|
||||
lline.AppendToken(_MakeFormatTokenLeaf(token.LPAR, '('))
|
||||
lline.AppendToken(_MakeFormatTokenLeaf(token.RPAR, ')'))
|
||||
lline.AppendToken(_MakeFormatTokenLeaf(token.LPAR, '(', 'LPAR'))
|
||||
lline.AppendToken(_MakeFormatTokenLeaf(token.RPAR, ')', 'RPAR'))
|
||||
self.assertEqual(['LPAR', 'RPAR'], [tok.name for tok in lline.tokens])
|
||||
|
||||
|
||||
|
@ -75,14 +78,14 @@ class LogicalLineFormattingInformationTest(yapf_test_helper.YAPFTest):
|
|||
self.assertEqual(lparen.split_penalty, split_penalty.UNBREAKABLE)
|
||||
|
||||
|
||||
def _MakeFormatTokenLeaf(token_type, token_value):
|
||||
return format_token.FormatToken(pytree.Leaf(token_type, token_value))
|
||||
def _MakeFormatTokenLeaf(token_type, token_value, name):
|
||||
return format_token.FormatToken(pytree.Leaf(token_type, token_value), name)
|
||||
|
||||
|
||||
def _MakeFormatTokenList(token_type_values):
|
||||
return [
|
||||
_MakeFormatTokenLeaf(token_type, token_value)
|
||||
for token_type, token_value in token_type_values
|
||||
_MakeFormatTokenLeaf(token_type, token_value, token_name)
|
||||
for token_type, token_value, token_name in token_type_values
|
||||
]
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue