java-codebase-rag 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
chunk_heuristics.py ADDED
@@ -0,0 +1,62 @@
1
+ """Lightweight, query-time hints from chunk text — no AST / re-index required."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from dataclasses import dataclass
7
+
8
+
9
+ @dataclass
10
+ class ChunkHints:
11
+ """Heuristic metadata derived from a chunk's text."""
12
+
13
+ primary_type_hint: str | None = None
14
+ """First top-level ``class`` / ``interface`` / ``enum`` / ``record`` name in the chunk."""
15
+
16
+ import_heavy: bool = False
17
+ """True when most lines are ``import`` statements (low semantic density)."""
18
+
19
+
20
+ _JAVA_TYPE = re.compile(
21
+ r"\b(?:public\s+|private\s+|protected\s+|sealed\s+|final\s+|abstract\s+|static\s+)*"
22
+ r"(?:class|interface|enum|record)\s+([A-Za-z_][A-Za-z0-9_]*)"
23
+ )
24
+
25
+
26
+ def analyze_chunk(text: str | None, *, language: str, kind: str) -> ChunkHints:
27
+ if not text or not text.strip():
28
+ return ChunkHints()
29
+
30
+ lines = text.strip().split("\n")
31
+ n = len(lines)
32
+ lang = (language or "").lower()
33
+ is_java = kind == "java" or lang == "java"
34
+
35
+ import_heavy = False
36
+ if is_java and n >= 3:
37
+ imp = sum(1 for L in lines if L.lstrip().startswith("import "))
38
+ import_heavy = imp / n >= 0.55
39
+
40
+ primary: str | None = None
41
+ if is_java:
42
+ head = "\n".join(lines[: min(80, n)])
43
+ m = _JAVA_TYPE.search(head)
44
+ if m:
45
+ primary = m.group(1)
46
+
47
+ return ChunkHints(
48
+ primary_type_hint=primary,
49
+ import_heavy=import_heavy,
50
+ )
51
+
52
+
53
+ def looks_like_code_identifier(query: str) -> bool:
54
+ """PascalCase type name or SCREAMING_SNAKE constant — good candidates for FTS hybrid."""
55
+ q = query.strip()
56
+ if not q or len(q) < 2 or len(q) > 200:
57
+ return False
58
+ if re.fullmatch(r"[A-Z][a-zA-Z0-9_]*", q):
59
+ return True
60
+ if "_" in q and re.fullmatch(r"[A-Z][A-Z0-9_]*", q):
61
+ return True
62
+ return False