Skip to content

Commit d928565

Browse files
committed
Fix parent definition resolution
1 parent c6ce9e5 commit d928565

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/cedarscript_editor/tree_sitter_identifier_finder.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ def associate_identifier_parts(captures: Iterable[CaptureInfo], lines: list[str]
7676
identifier_map[range_spec.start] = IdentifierBoundaries(range_spec)
7777

7878
else:
79-
parent_key = capture.node.parent.start_point[0]
80-
parent = identifier_map.get(parent_key)
79+
parent = find_parent_definition(capture.node)
80+
if parent:
81+
parent_key = parent.start_point[0]
82+
parent = identifier_map.get(parent_key)
8183
if parent is None:
82-
raise ValueError(f'Parent node not found for "{capture.node_type}"')
84+
raise ValueError(f'Parent node not found for [{capture.capture_type} - {capture.node_type}] ({capture.node.text.decode("utf-8").strip()})')
8385
match capture_type:
8486
case 'body':
8587
parent = parent._replace(body=range_spec)
@@ -94,6 +96,15 @@ def associate_identifier_parts(captures: Iterable[CaptureInfo], lines: list[str]
9496
return sorted(identifier_map.values(), key=lambda x: x.whole.start)
9597

9698

99+
def find_parent_definition(node):
100+
# TODO How to deal with 'decorated_definition' ?
101+
while node.parent:
102+
node = node.parent
103+
if node.type.endswith('_definition'):
104+
return node
105+
return None
106+
107+
97108
def _find_identifier(language, source: str, tree, query_scm: dict[str, dict[str, str]], marker: Marker) \
98109
-> IdentifierBoundaries | None:
99110
"""
@@ -113,7 +124,7 @@ def _find_identifier(language, source: str, tree, query_scm: dict[str, dict[str,
113124
source.splitlines()
114125
)
115126
except Exception as e:
116-
raise ValueError(f"Unable to capture nodes for {marker}") from e
127+
raise ValueError(f"Unable to capture nodes for {marker}: {e}") from e
117128

118129
candidate_count = len(candidates)
119130
if not candidate_count:

0 commit comments

Comments
 (0)