summaryrefslogtreecommitdiff
path: root/bindings/python/tests/cindex/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'bindings/python/tests/cindex/util.py')
-rw-r--r--bindings/python/tests/cindex/util.py40
1 files changed, 11 insertions, 29 deletions
diff --git a/bindings/python/tests/cindex/util.py b/bindings/python/tests/cindex/util.py
index 8614b02ad258..c53ba7c81bde 100644
--- a/bindings/python/tests/cindex/util.py
+++ b/bindings/python/tests/cindex/util.py
@@ -39,52 +39,34 @@ def get_cursor(source, spelling):
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',