Commits

Anonymous committed 406e5e6

outline: get_kind() method can be used to infer node type

Comments (0)

Files changed (2)

ropeide/outline.py

             self.children = _get_ast_children(self.node)
         return self.children
 
+    def get_kind(self):
+        """Return the type of this node
+
+        Returns 'function' for functions can 'class' for classes.
+
+        """
+        if isinstance(self.node, ast.FunctionDef):
+            return 'function'
+        if isinstance(self.node, ast.ClassDef):
+            return 'class'
+
     def __cmp__(self, obj):
         return cmp(self.get_line_number(), obj.get_line_number())
 

ropeidetest/outlinetest.py

         self.assertEquals(0, len(nodes[0].get_children()))
 
     def test_nested_outlines(self):
-        nodes = self.outline.get_root_nodes('class Sample(object):\n    def a_method(self):\n        pass\n')
+        nodes = self.outline.get_root_nodes(
+            'class Sample(object):\n    def a_method(self):\n        pass\n')
         self.assertEquals(1, len(nodes))
         self.assertEquals('Sample', nodes[0].get_name())
         sample_class = nodes[0]
         self.assertEquals(2, a_method.get_line_number())
 
     def test_sorting_by_line_number(self):
-        nodes = self.outline.get_root_nodes('def a_func2():\n    pass\ndef a_func1():\n    pass\n')
+        nodes = self.outline.get_root_nodes(
+            'def a_func2():\n    pass\ndef a_func1():\n    pass\n')
         self.assertEquals('a_func2', nodes[0].get_name())
         self.assertEquals('a_func1', nodes[1].get_name())
 
         nodes = self.outline.get_root_nodes(src)
         self.assertEquals(0, len(nodes[1].get_children()))
 
+    def test_node_kind(self):
+        src = 'class C(object):\n    pass\ndef f():\n    pass\n'
+        nodes = self.outline.get_root_nodes(src)
+        self.assertEquals('class', nodes[0].get_kind())
+        self.assertEquals('function', nodes[1].get_kind())
+
 
 if __name__ == '__main__':
     unittest.main()