Ejemplo n.º 1
0
    def _SetBlankLinesBetweenCommentAndClassFunc(self, node):
        """Set the number of blanks between a comment and class or func definition.

    Class and function definitions have leading comments as children of the
    classdef and functdef nodes.

    Arguments:
      node: (pytree.Node) The classdef or funcdef node.

    Returns:
      The index of the first child past the comment nodes.
    """
        index = 0
        while pytree_utils.IsCommentStatement(node.children[index]):
            # Standalone comments are wrapped in a simple_stmt node with the comment
            # node as its only child.
            self.Visit(node.children[index].children[0])
            if not self.last_was_decorator:
                self._SetNumNewlines(node.children[index].children[0],
                                     _ONE_BLANK_LINE)
            index += 1
        if (index and node.children[index].lineno - 1
                == node.children[index - 1].children[0].lineno):
            self._SetNumNewlines(node.children[index], _NO_BLANK_LINES)
        else:
            if self.last_comment_lineno + 1 == node.children[index].lineno:
                num_newlines = _NO_BLANK_LINES
            else:
                num_newlines = self._GetNumNewlines(node)
            self._SetNumNewlines(node.children[index], num_newlines)
        return index
Ejemplo n.º 2
0
  def Visit_simple_stmt(self, node):
    # A 'simple_stmt' conveniently represents a non-compound Python statement,
    # i.e. a statement that does not contain other statements.

    # When compound nodes have a single statement as their suite, the parser
    # can leave it in the tree directly without creating a suite. But we have
    # to increase depth in these cases as well. However, don't increase the
    # depth of we have a simple_stmt that's a comment node. This represents a
    # standalone comment and in the case of it coming directly after the
    # funcdef, it is a "top" comment for the whole function.
    # TODO(eliben): add more relevant compound statements here.
    single_stmt_suite = (node.parent and
                         pytree_utils.NodeName(node.parent) in self._STMT_TYPES)
    is_comment_stmt = pytree_utils.IsCommentStatement(node)
    if single_stmt_suite and not is_comment_stmt:
      self._cur_depth += 1
    self._StartNewLine()
    self.DefaultNodeVisit(node)
    if single_stmt_suite and not is_comment_stmt:
      self._cur_depth -= 1