Пример #1
0
 def testFirstLast(self):
   toks = _MakeFormatTokenList([(token.DOT, '.'), (token.LPAR, '('),
                                (token.VBAR, '|')])
   uwl = unwrapped_line.UnwrappedLine(20, toks)
   self.assertEqual(20, uwl.depth)
   self.assertEqual('DOT', uwl.first.name)
   self.assertEqual('VBAR', uwl.last.name)
Пример #2
0
  def __init__(self):
    # A list of all unwrapped lines finished visiting so far.
    self._unwrapped_lines = []

    # Builds up a "current" unwrapped line while visiting pytree nodes. Some
    # nodes will finish a line and start a new one.
    self._cur_unwrapped_line = unwrapped_line.UnwrappedLine(0)

    # Current indentation depth.
    self._cur_depth = 0
Пример #3
0
    def testSimpleFunctionDefWithNoSplitting(self):
        code = textwrap.dedent(r"""
      def f(a, b):
        pass
      """)
        print(code)
        uwlines = yapf_test_helper.ParseAndUnwrap(code)
        uwline = unwrapped_line.UnwrappedLine(0, _FilterLine(uwlines[0]))
        uwline.CalculateFormattingInformation()

        # Add: 'f'
        state = format_decision_state.FormatDecisionState(uwline, 0)
        state.MoveStateToNextToken()
        self.assertEqual('f', state.next_token.value)
        self.assertFalse(state.CanSplit(False))

        # Add: '('
        state.AddTokenToState(False, True)
        self.assertEqual('(', state.next_token.value)
        self.assertFalse(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: 'a'
        state.AddTokenToState(False, True)
        self.assertEqual('a', state.next_token.value)
        self.assertTrue(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: ','
        state.AddTokenToState(False, True)
        self.assertEqual(',', state.next_token.value)
        self.assertFalse(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: 'b'
        state.AddTokenToState(False, True)
        self.assertEqual('b', state.next_token.value)
        self.assertTrue(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: ')'
        state.AddTokenToState(False, True)
        self.assertEqual(')', state.next_token.value)
        self.assertTrue(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        # Add: ':'
        state.AddTokenToState(False, True)
        self.assertEqual(':', state.next_token.value)
        self.assertFalse(state.CanSplit(False))
        self.assertFalse(state.MustSplit())

        clone = state.Clone()
        self.assertEqual(repr(state), repr(clone))
Пример #4
0
  def _StartNewLine(self):
    """Finish current line and start a new one.

    Place the currently accumulated line into the _unwrapped_lines list and
    start a new one.
    """
    if self._cur_unwrapped_line.tokens:
      self._unwrapped_lines.append(self._cur_unwrapped_line)
      _MatchBrackets(self._cur_unwrapped_line)
      _AdjustSplitPenalty(self._cur_unwrapped_line)
    self._cur_unwrapped_line = unwrapped_line.UnwrappedLine(self._cur_depth)
Пример #5
0
    def _ParseAndUnwrap(self, code):
        tree = pytree_utils.ParseCodeToTree(code)
        comment_splicer.SpliceComments(tree)
        subtype_assigner.AssignSubtypes(tree)
        split_penalty.ComputeSplitPenalties(tree)

        uwlines = pytree_unwrapper.UnwrapPyTree(tree)
        for i, uwline in enumerate(uwlines):
            uwlines[i] = unwrapped_line.UnwrappedLine(uwline.depth, [
                ft for ft in uwline.tokens
                if ft.name not in pytree_utils.NONSEMANTIC_TOKENS
            ])
        return uwlines
Пример #6
0
 def testAppendNode(self):
   uwl = unwrapped_line.UnwrappedLine(0)
   uwl.AppendNode(pytree.Leaf(token.LPAR, '('))
   uwl.AppendNode(pytree.Leaf(token.RPAR, ')'))
   self.assertEqual(['LPAR', 'RPAR'], [tok.name for tok in uwl.tokens])
Пример #7
0
 def testAppendToken(self):
   uwl = unwrapped_line.UnwrappedLine(0)
   uwl.AppendToken(_MakeFormatTokenLeaf(token.LPAR, '('))
   uwl.AppendToken(_MakeFormatTokenLeaf(token.RPAR, ')'))
   self.assertEqual(['LPAR', 'RPAR'], [tok.name for tok in uwl.tokens])
Пример #8
0
 def testAsCode(self):
   toks = _MakeFormatTokenList([(token.DOT, '.'), (token.LPAR, '('),
                                (token.VBAR, '|')])
   uwl = unwrapped_line.UnwrappedLine(2, toks)
   self.assertEqual('    . ( |', uwl.AsCode())
Пример #9
0
 def testConstruction(self):
   toks = _MakeFormatTokenList([(token.DOT, '.'), (token.VBAR, '|')])
   uwl = unwrapped_line.UnwrappedLine(20, toks)
   self.assertEqual(20, uwl.depth)
   self.assertEqual(['DOT', 'VBAR'], [tok.name for tok in uwl.tokens])