Skip to content

Commit 30d860a

Browse files
committed
CaptureInfo: store parents
1 parent affaa0c commit 30d860a

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/cedarscript_editor/tree_sitter_identifier_finder.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from functools import cached_property
23
from typing import Callable, TypeAlias, Sequence, NamedTuple, Iterable
34

45
from cedarscript_ast_parser import Marker, MarkerType, Segment
@@ -64,7 +65,6 @@ def _select_finder(file_path: str, source: str, search_range: RangeSpec = RangeS
6465

6566
source = source.splitlines()
6667

67-
6868
def find_by_marker(mos: Marker | Segment, search_range: RangeSpec | None = None) -> IdentifierBoundaries | RangeSpec | None:
6969
match mos:
7070

@@ -119,6 +119,27 @@ def identifier(self):
119119
return None
120120
return self.node.text.decode("utf-8")
121121

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+
122143

123144
def associate_identifier_parts(captures: Iterable[CaptureInfo], lines: Sequence[str]) -> list[IdentifierBoundaries]:
124145
"""Associates related identifier parts (definition, body, docstring, etc) into IdentifierBoundaries.
@@ -160,6 +181,7 @@ def associate_identifier_parts(captures: Iterable[CaptureInfo], lines: Sequence[
160181

161182

162183
def find_parent_definition(node):
184+
"""Returns the first parent node that ends with '_definition'"""
163185
# TODO How to deal with 'decorated_definition' ?
164186
while node.parent:
165187
node = node.parent

0 commit comments

Comments
 (0)