rope-qickfix / ropeide / outline.py

from rope.base import ast


class PythonASTOutlineNode(object):

    def __init__(self, ast_node):
        self.name = ast_node.name
        self.node = ast_node
        self.children = None

    def get_name(self):
        return self.name

    def get_line_number(self):
        return self.node.lineno

    def get_children(self):
        if self.children is None:
            self.children = _get_ast_children(self.node)
        return self.children

    def __cmp__(self, obj):
        return cmp(self.get_line_number(), obj.get_line_number())


class _ASTDefinedVisitor(object):

    def __init__(self):
        self.result = []

    def _FunctionDef(self, node):
        self.result.append(PythonASTOutlineNode(node))

    def _ClassDef(self, node):
        self.result.append(PythonASTOutlineNode(node))


def _get_ast_children(node):
    visitor = _ASTDefinedVisitor()
    for child in ast.get_child_nodes(node):
        ast.walk(child, visitor)
    return visitor.result


class PythonOutline(object):

    def __init__(self, project):
        self.project = project

    def get_root_nodes(self, source_code):
        if isinstance(source_code, unicode):
            source_code = source_code.encode('utf-8')
        ast_node = ast.parse(source_code)
        return _get_ast_children(ast_node)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.