Skip to content

Commit 3fc6458

Browse files
committed
fix: include decorators in TreeSitter fragments and use chunk for gaps
- Fix TreeSitterStrategy to detect decorated_definition nodes and map them to function/class based on the inner definition - Change gap fragments from kind="module" to kind="chunk" since they are arbitrary code chunks, not complete modules - Update test to expect chunk instead of module for gap fragments
1 parent 4573b42 commit 3fc6458

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

src/treemapper/diffctx/fragments.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def _extract_definitions(
366366
end = node.end_point[0] + 1
367367

368368
if node.type in definition_types:
369-
kind = self._node_type_to_kind(node.type)
369+
kind = self._node_type_to_kind(node.type, node)
370370

371371
if (kind, end) in added_ends:
372372
for child in node.children:
@@ -392,7 +392,14 @@ def _extract_definitions(
392392
for child in node.children:
393393
self._extract_definitions(child, code_bytes, path, lines, definition_types, fragments, covered, added_ends)
394394

395-
def _node_type_to_kind(self, node_type: str) -> str:
395+
def _node_type_to_kind(self, node_type: str, node: Node | None = None) -> str:
396+
if node_type == "decorated_definition" and node is not None:
397+
for child in node.children:
398+
if child.type in {"function_definition", "async_function_definition"}:
399+
return "function"
400+
if child.type == "class_definition":
401+
return "class"
402+
return "function"
396403
if "function" in node_type or "method" in node_type:
397404
return "function"
398405
if "class" in node_type:
@@ -460,7 +467,7 @@ def _create_gap_fragments(self, path: Path, lines: list[str], covered: list[tupl
460467
fragments.append(
461468
Fragment(
462469
id=FragmentId(path=path, start_line=start, end_line=end),
463-
kind="module",
470+
kind="chunk",
464471
content=snippet,
465472
identifiers=extract_identifiers(snippet, profile="code"),
466473
)
@@ -607,7 +614,7 @@ def _create_gap_fragments(self, path: Path, lines: list[str], covered: list[tupl
607614
fragments.append(
608615
Fragment(
609616
id=FragmentId(path=path, start_line=start, end_line=end),
610-
kind="module",
617+
kind="chunk",
611618
content=snippet,
612619
identifiers=extract_identifiers(snippet, profile="code"),
613620
)

tests/test_diff_fragments_regression.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ def main():
199199

200200
fragments = fragment_file(file_path, code)
201201

202-
module_frags = [f for f in fragments if f.kind == "module"]
203-
assert len(module_frags) >= 1
202+
chunk_frags = [f for f in fragments if f.kind == "chunk"]
203+
assert len(chunk_frags) >= 1
204204

205-
module_frag = module_frags[0]
206-
assert "import os" in module_frag.content
207-
assert "CONFIG_PATH" in module_frag.content
205+
chunk_frag = chunk_frags[0]
206+
assert "import os" in chunk_frag.content
207+
assert "CONFIG_PATH" in chunk_frag.content
208208

209209
def test_frag_py_021_module_gap_between_definitions(self, tmp_path):
210210
code = """\

0 commit comments

Comments
 (0)