Skip to content

Commit 98167ec

Browse files
committed
fix: prevent duplicate fragments for decorated definitions in tree-sitter
1 parent e7c18f0 commit 98167ec

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/treemapper/diffctx/fragments.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,13 @@ def _extract_definitions(
358358
fragments: list[Fragment],
359359
covered: set[tuple[int, int]],
360360
) -> None:
361-
if node.type in definition_types:
362-
start = node.start_point[0] + 1
363-
end = node.end_point[0] + 1
361+
start = node.start_point[0] + 1
362+
end = node.end_point[0] + 1
363+
364+
if self._is_covered(start, end, covered):
365+
return
364366

367+
if node.type in definition_types:
365368
if end - start + 1 >= _MIN_FRAGMENT_LINES:
366369
snippet = code_bytes[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
367370
if not snippet.endswith("\n"):
@@ -377,10 +380,17 @@ def _extract_definitions(
377380
)
378381
)
379382
covered.add((start, end))
383+
return
380384

381385
for child in node.children:
382386
self._extract_definitions(child, code_bytes, path, lines, definition_types, fragments, covered)
383387

388+
def _is_covered(self, start: int, end: int, covered: set[tuple[int, int]]) -> bool:
389+
for cov_start, cov_end in covered:
390+
if cov_start <= start and end <= cov_end:
391+
return True
392+
return False
393+
384394
def _node_type_to_kind(self, node_type: str) -> str:
385395
if "function" in node_type or "method" in node_type:
386396
return "function"

0 commit comments

Comments
 (0)