summaryrefslogtreecommitdiff
path: root/systrace/catapult/common/py_utils/py_utils/refactor/annotated_symbol/function_definition.py
blob: 384d3cf134de2bb4fb9a235ec3c8e266ef41caa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import symbol

from py_utils.refactor.annotated_symbol import base_symbol


__all__ = [
    'Function',
]


class Function(base_symbol.AnnotatedSymbol):
  @classmethod
  def Annotate(cls, symbol_type, children):
    if symbol_type != symbol.stmt:
      return None

    compound_statement = children[0]
    if compound_statement.type != symbol.compound_stmt:
      return None

    statement = compound_statement.children[0]
    if statement.type == symbol.funcdef:
      return cls(statement.type, statement.children)
    elif (statement.type == symbol.decorated and
          statement.children[-1].type == symbol.funcdef):
      return cls(statement.type, statement.children)
    else:
      return None

  @property
  def suite(self):
    # TODO: Complete.
    raise NotImplementedError()

  def FindChild(self, snippet_type, **kwargs):
    return self.suite.FindChild(snippet_type, **kwargs)

  def FindChildren(self, snippet_type):
    return self.suite.FindChildren(snippet_type)

  def Cut(self, child):
    self.suite.Cut(child)

  def Paste(self, child):
    self.suite.Paste(child)