|
1 | 1 | import logging |
| 2 | +from functools import cached_property |
2 | 3 | from typing import Callable, TypeAlias, Sequence, NamedTuple, Iterable |
3 | 4 |
|
4 | 5 | from cedarscript_ast_parser import Marker, MarkerType, Segment |
@@ -64,7 +65,6 @@ def _select_finder(file_path: str, source: str, search_range: RangeSpec = RangeS |
64 | 65 |
|
65 | 66 | source = source.splitlines() |
66 | 67 |
|
67 | | - |
68 | 68 | def find_by_marker(mos: Marker | Segment, search_range: RangeSpec | None = None) -> IdentifierBoundaries | RangeSpec | None: |
69 | 69 | match mos: |
70 | 70 |
|
@@ -119,6 +119,27 @@ def identifier(self): |
119 | 119 | return None |
120 | 120 | return self.node.text.decode("utf-8") |
121 | 121 |
|
| 122 | + @cached_property |
| 123 | + def parents(self) -> list[tuple[str, str]]: |
| 124 | + """Returns a list of (node_type, node_name) tuples representing the hierarchy. |
| 125 | + The list is ordered from immediate parent to root.""" |
| 126 | + parents = [] |
| 127 | + current = self.node.parent |
| 128 | + |
| 129 | + while current: |
| 130 | + # Check if current node is a container type we care about |
| 131 | + if current.type.endswith('_definition'): |
| 132 | + # Try to find the name node - exact field depends on language |
| 133 | + name = None |
| 134 | + for child in current.children: |
| 135 | + if child.type == 'identifier' or child.type == 'name': |
| 136 | + name = child.text.decode('utf-8') |
| 137 | + break |
| 138 | + parents.append((current.type, name)) |
| 139 | + current = current.parent |
| 140 | + |
| 141 | + return parents |
| 142 | + |
122 | 143 |
|
123 | 144 | def associate_identifier_parts(captures: Iterable[CaptureInfo], lines: Sequence[str]) -> list[IdentifierBoundaries]: |
124 | 145 | """Associates related identifier parts (definition, body, docstring, etc) into IdentifierBoundaries. |
@@ -160,6 +181,7 @@ def associate_identifier_parts(captures: Iterable[CaptureInfo], lines: Sequence[ |
160 | 181 |
|
161 | 182 |
|
162 | 183 | def find_parent_definition(node): |
| 184 | + """Returns the first parent node that ends with '_definition'""" |
163 | 185 | # TODO How to deal with 'decorated_definition' ? |
164 | 186 | while node.parent: |
165 | 187 | node = node.parent |
|
0 commit comments