@@ -128,7 +128,8 @@ def find_identifiers(
128128 match identifier_type :
129129 case 'method' :
130130 identifier_type = 'function'
131- candidate_nodes = self .language .query (self .query_info [identifier_type ].format (name = name )).captures (self .tree .root_node )
131+ _query = self .query_info [identifier_type ].format (name = name )
132+ candidate_nodes = self .language .query (_query ).captures (self .tree .root_node )
132133 if not candidate_nodes :
133134 return []
134135 # Convert captures to boundaries and filter by parent
@@ -198,9 +199,9 @@ def parents(self) -> list[ParentInfo]:
198199 current = self .node .parent
199200
200201 while current :
201- # Check if current node is a container type we care about
202- if current .type .endswith ('_definition' ):
203- # Try to find the name node - exact field depends on language
202+ # Check if current node is a container type we care about - TODO exact field depends on language
203+ if current .type .endswith ('_definition' ) and current . type != 'decorated_definition' :
204+ # Try to find the name node - TODO exact field depends on language
204205 name = None
205206 for child in current .children :
206207 if child .type == 'identifier' or child .type == 'name' :
@@ -242,14 +243,13 @@ def associate_identifier_parts(captures: Iterable[CaptureInfo], lines: Sequence[
242243 raise ValueError (f'Parent node not found for [{ capture .capture_type } - { capture .node_type } ] ({ capture .node .text .decode ("utf-8" ).strip ()} )' )
243244 match capture_type :
244245 case 'body' :
245- parent = parent . _replace ( body = range_spec )
246+ parent . body = range_spec
246247 case 'docstring' :
247- parent = parent . _replace ( docstring = range_spec )
248+ parent . docstring = range_spec
248249 case 'decorator' :
249- parent = parent . decorators . append (range_spec )
250+ parent . append_decorator (range_spec )
250251 case _ as invalid :
251252 raise ValueError (f'Invalid capture type: { invalid } ' )
252- identifier_map [parent_key ] = parent
253253
254254 return sorted (identifier_map .values (), key = lambda x : x .whole .start )
255255
@@ -260,6 +260,8 @@ def find_parent_definition(node):
260260 while node .parent :
261261 node = node .parent
262262 if node .type .endswith ('_definition' ):
263+ if node .type == 'decorated_definition' :
264+ node = node .named_children [0 ].next_named_sibling
263265 return node
264266 return None
265267
@@ -278,4 +280,46 @@ def capture2identifier_boundaries(captures, lines: Sequence[str]) -> list[Identi
278280 unique_captures = {}
279281 for capture in captures :
280282 unique_captures [f'{ capture .range [0 ]} :{ capture .capture_type } ' ] = capture
281- return associate_identifier_parts (unique_captures .values (), lines )
283+ # unique_captures={
284+ # '157:function.decorator': CaptureInfo(capture_type='function.decorator', node=<Node type=decorator, start_point=(157, 4), end_point=(157, 17)>),
285+ # '158:function.definition': CaptureInfo(capture_type='function.definition', node=<Node type=function_definition, start_point=(158, 4), end_point=(207, 19)>),
286+ # '159:function.body': CaptureInfo(capture_type='function.body', node=<Node type=block, start_point=(159, 8), end_point=(207, 19)>)
287+ # }
288+ return associate_identifier_parts (sort_captures (unique_captures ), lines )
289+
290+ def parse_capture_key (key ):
291+ """
292+ Parses the dictionary key into line number and capture type.
293+ Args:
294+ key (str): The key in the format 'line_number:capture_type'.
295+ Returns:
296+ tuple: (line_number as int, capture_type as str)
297+ """
298+ line_number , capture_type = key .split (':' )
299+ return int (line_number ), capture_type .split ('.' )[- 1 ]
300+
301+ def get_sort_priority ():
302+ """
303+ Returns a dictionary mapping capture types to their sort priority.
304+ Returns:
305+ dict: Capture type priorities.
306+ """
307+ return {'definition' : 1 , 'decorator' : 2 , 'body' : 3 , 'docstring' : 4 }
308+
309+ def sort_captures (captures ):
310+ """
311+ Sorts the values of the captures dictionary by capture type and line number.
312+ Args:
313+ captures (dict): The dictionary to sort.
314+ Returns:
315+ list: Sorted list of values.
316+ """
317+ priority = get_sort_priority ()
318+ sorted_items = sorted (
319+ captures .items (),
320+ key = lambda item : (
321+ priority [parse_capture_key (item [0 ])[1 ]], # Sort by capture type priority
322+ parse_capture_key (item [0 ])[0 ] # Then by line number
323+ )
324+ )
325+ return [value for _ , value in sorted_items ]
0 commit comments