If the cursor is not found, None is returned.
"""
- children = []
- if isinstance(source, Cursor):
- children = source.get_children()
- else:
- # Assume TU
- children = source.cursor.get_children()
-
- for cursor in children:
+ # Convenience for calling on a TU.
+ root_cursor = source if isinstance(source, Cursor) else source.cursor
+
+ for cursor in root_cursor.walk_preorder():
if cursor.spelling == spelling:
return cursor
- # Recurse into children.
- result = get_cursor(cursor, spelling)
- if result is not None:
- return result
-
return None
-
+
def get_cursors(source, spelling):
"""Obtain all cursors from a source object with a specific spelling.
- This provides a convenient search mechanism to find all cursors with specific
- spelling within a source. The first argument can be either a
+ This provides a convenient search mechanism to find all cursors with
+ specific spelling within a source. The first argument can be either a
TranslationUnit or Cursor instance.
If no cursors are found, an empty list is returned.
"""
+ # Convenience for calling on a TU.
+ root_cursor = source if isinstance(source, Cursor) else source.cursor
+
cursors = []
- children = []
- if isinstance(source, Cursor):
- children = source.get_children()
- else:
- # Assume TU
- children = source.cursor.get_children()
-
- for cursor in children:
+ for cursor in root_cursor.walk_preorder():
if cursor.spelling == spelling:
cursors.append(cursor)
- # Recurse into children.
- cursors.extend(get_cursors(cursor, spelling))
-
return cursors
-
-
__all__ = [
'get_cursor',