codegraphx 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.
Files changed (44) hide show
  1. codegraph/__init__.py +37 -0
  2. codegraph/cli.py +106 -0
  3. codegraph/commands/__init__.py +1 -0
  4. codegraph/commands/conflicts.py +154 -0
  5. codegraph/commands/diff.py +189 -0
  6. codegraph/commands/export.py +141 -0
  7. codegraph/commands/graph.py +200 -0
  8. codegraph/commands/index.py +924 -0
  9. codegraph/commands/serve.py +62 -0
  10. codegraph/commands/why.py +645 -0
  11. codegraph/decisions/__init__.py +18 -0
  12. codegraph/decisions/conflicts.py +238 -0
  13. codegraph/decisions/extractor.py +698 -0
  14. codegraph/decisions/llm_extractor.py +161 -0
  15. codegraph/decisions/sources.py +298 -0
  16. codegraph/git/__init__.py +5 -0
  17. codegraph/git/diff.py +115 -0
  18. codegraph/git/history.py +156 -0
  19. codegraph/git/incremental.py +75 -0
  20. codegraph/git/refs.py +70 -0
  21. codegraph/models.py +211 -0
  22. codegraph/parser/__init__.py +1 -0
  23. codegraph/parser/base.py +164 -0
  24. codegraph/parser/common.py +147 -0
  25. codegraph/parser/parallel.py +96 -0
  26. codegraph/parser/registry.py +83 -0
  27. codegraph/parser/treesitter_cpp.py +102 -0
  28. codegraph/parser/treesitter_go.py +113 -0
  29. codegraph/parser/treesitter_java.py +79 -0
  30. codegraph/parser/treesitter_js.py +15 -0
  31. codegraph/parser/treesitter_py.py +163 -0
  32. codegraph/parser/treesitter_rust.py +120 -0
  33. codegraph/parser/treesitter_ts.py +178 -0
  34. codegraph/storage.py +767 -0
  35. codegraph/web/__init__.py +1 -0
  36. codegraph/web/app.py +271 -0
  37. codegraph/web/static/app.js +356 -0
  38. codegraph/web/static/index.html +59 -0
  39. codegraph/web/static/styles.css +92 -0
  40. codegraphx-0.1.0.dist-info/METADATA +262 -0
  41. codegraphx-0.1.0.dist-info/RECORD +44 -0
  42. codegraphx-0.1.0.dist-info/WHEEL +4 -0
  43. codegraphx-0.1.0.dist-info/entry_points.txt +2 -0
  44. codegraphx-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,163 @@
1
+ """Python tree-sitter parser."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from datetime import datetime
7
+ from pathlib import Path
8
+
9
+ import tree_sitter_python as tspython
10
+ from tree_sitter import Language, Node, Parser
11
+
12
+ from codegraph.models import CodeNode, make_uid, normalize_location_path
13
+ from codegraph.parser.base import BaseParser
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+ _PY_LANGUAGE = Language(tspython.language())
18
+
19
+ _KIND_MAP = {
20
+ "function_definition": "function",
21
+ "class_definition": "class",
22
+ }
23
+
24
+
25
+ class PythonParser(BaseParser):
26
+ """Parse .py files for functions/classes and call relations."""
27
+
28
+ language = "python"
29
+
30
+ def parse_file(self, path: Path) -> list[CodeNode]:
31
+ """Parse a Python source file from disk."""
32
+ try:
33
+ rel = normalize_location_path(str(path.resolve().relative_to(Path.cwd())))
34
+ except ValueError:
35
+ rel = normalize_location_path(str(path))
36
+ try:
37
+ source = path.read_text(encoding="utf-8")
38
+ except (OSError, UnicodeDecodeError) as exc:
39
+ logger.warning("Read failed, skip %s: %s", path, exc)
40
+ return []
41
+ return self.parse_text(rel, source)
42
+
43
+ def parse_text(
44
+ self,
45
+ rel_path: str,
46
+ source: str,
47
+ commit_sha: str = "",
48
+ valid_from: datetime | None = None,
49
+ ) -> list[CodeNode]:
50
+ """Parse Python source text into CodeNode records."""
51
+ rel = normalize_location_path(rel_path)
52
+ self._calls.clear()
53
+ raw = source.encode("utf-8")
54
+ tree = Parser(_PY_LANGUAGE).parse(raw)
55
+ out: list[CodeNode] = []
56
+ self._walk(
57
+ tree.root_node,
58
+ raw,
59
+ rel,
60
+ out,
61
+ parent_uid=None,
62
+ current_fn="",
63
+ commit_sha=commit_sha,
64
+ valid_from=valid_from,
65
+ )
66
+ return out
67
+
68
+ def _walk(
69
+ self,
70
+ node: Node,
71
+ raw: bytes,
72
+ rel: str,
73
+ out: list[CodeNode],
74
+ parent_uid: str | None,
75
+ current_fn: str,
76
+ commit_sha: str = "",
77
+ valid_from: datetime | None = None,
78
+ ) -> None:
79
+ """Depth-first walk collecting declarations and calls."""
80
+ kind = _KIND_MAP.get(node.type)
81
+ name = self._decl_name(node, raw)
82
+ next_parent = parent_uid
83
+ next_fn = current_fn
84
+
85
+ if kind and name:
86
+ code_node = self._make_node(
87
+ node, rel, kind, name, parent_uid, commit_sha, valid_from
88
+ )
89
+ out.append(code_node)
90
+ if kind in {"class", "function"}:
91
+ next_parent = code_node.uid
92
+ if kind == "function":
93
+ next_fn = name
94
+
95
+ if node.type == "call":
96
+ self._record_call(node, raw, rel, current_fn if current_fn else name or "")
97
+
98
+ for child in node.children:
99
+ self._walk(
100
+ child,
101
+ raw,
102
+ rel,
103
+ out,
104
+ next_parent,
105
+ next_fn,
106
+ commit_sha=commit_sha,
107
+ valid_from=valid_from,
108
+ )
109
+
110
+ def _make_node(
111
+ self,
112
+ node: Node,
113
+ rel: str,
114
+ kind: str,
115
+ name: str,
116
+ parent_uid: str | None,
117
+ commit_sha: str = "",
118
+ valid_from: datetime | None = None,
119
+ ) -> CodeNode:
120
+ """Build a CodeNode from an AST node."""
121
+ return CodeNode(
122
+ uid=make_uid(rel, kind, name, node.start_point[0] + 1, commit_sha),
123
+ kind=kind,
124
+ name=name,
125
+ file_path=rel,
126
+ line_start=node.start_point[0] + 1,
127
+ line_end=node.end_point[0] + 1,
128
+ language=self.language,
129
+ parent_uid=parent_uid,
130
+ commit_sha=commit_sha,
131
+ valid_from=valid_from or datetime.fromtimestamp(0),
132
+ valid_to=None,
133
+ )
134
+
135
+ @staticmethod
136
+ def _decl_name(node: Node, raw: bytes) -> str | None:
137
+ """Extract function or class name."""
138
+ name_node = node.child_by_field_name("name")
139
+ if name_node is None:
140
+ return None
141
+ return raw[name_node.start_byte : name_node.end_byte].decode("utf-8", errors="replace")
142
+
143
+ def _record_call(self, node: Node, raw: bytes, rel: str, caller_name: str) -> None:
144
+ """Record a call site with its callee name."""
145
+ fn = node.child_by_field_name("function")
146
+ if fn is None:
147
+ return
148
+ callee = self._callee_name(fn, raw)
149
+ if not callee:
150
+ return
151
+ line = node.start_point[0] + 1
152
+ self._calls.append((caller_name, callee, line, rel))
153
+
154
+ @staticmethod
155
+ def _callee_name(fn_node: Node, raw: bytes) -> str | None:
156
+ """Extract a simple callee name (identifier / attribute name)."""
157
+ if fn_node.type == "identifier":
158
+ return raw[fn_node.start_byte : fn_node.end_byte].decode("utf-8", errors="replace")
159
+ if fn_node.type == "attribute":
160
+ attr = fn_node.child_by_field_name("attribute")
161
+ if attr is not None:
162
+ return raw[attr.start_byte : attr.end_byte].decode("utf-8", errors="replace")
163
+ return None
@@ -0,0 +1,120 @@
1
+ """Rust tree-sitter parser."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from datetime import datetime
7
+ from pathlib import Path
8
+
9
+ import tree_sitter_rust as tsrust
10
+ from tree_sitter import Language, Parser
11
+
12
+ from codegraph.models import CodeNode
13
+ from codegraph.parser.base import BaseParser
14
+ from codegraph.parser.common import decl_name, make_node, rel_path, simple_callee, type_name
15
+
16
+ logger = logging.getLogger(__name__)
17
+ _LANG = Language(tsrust.language())
18
+
19
+ KIND_MAP = {
20
+ "function_item": "function",
21
+ "struct_item": "class",
22
+ "enum_item": "class",
23
+ "trait_item": "class",
24
+ "function_signature_item": "function",
25
+ }
26
+
27
+
28
+ class RustParser(BaseParser):
29
+ """Parse Rust sources for fns, types, and calls."""
30
+
31
+ language = "rust"
32
+
33
+ def parse_file(self, path: Path) -> list[CodeNode]:
34
+ """Parse a Rust file from disk."""
35
+ rel = rel_path(path)
36
+ try:
37
+ source = path.read_text(encoding="utf-8")
38
+ except (OSError, UnicodeDecodeError) as exc:
39
+ logger.warning("Read failed, skip %s: %s", path, exc)
40
+ return []
41
+ return self.parse_text(rel, source)
42
+
43
+ def parse_text(
44
+ self,
45
+ rel_path_: str,
46
+ source: str,
47
+ commit_sha: str = "",
48
+ valid_from: datetime | None = None,
49
+ ) -> list[CodeNode]:
50
+ """Parse Rust source text."""
51
+ rel = rel_path_.replace("\\", "/")
52
+ self._calls.clear()
53
+ raw = source.encode("utf-8")
54
+ tree = Parser(_LANG).parse(raw)
55
+ out: list[CodeNode] = []
56
+ pending: list[tuple[CodeNode, str]] = []
57
+ self._walk(
58
+ tree.root_node, raw, rel, out, None, "", commit_sha, valid_from, "", pending
59
+ )
60
+ _attach_to_types(out, pending)
61
+ return out
62
+
63
+ def _walk(
64
+ self,
65
+ node,
66
+ raw,
67
+ rel,
68
+ out,
69
+ parent_uid,
70
+ current_fn,
71
+ commit_sha,
72
+ valid_from,
73
+ impl_type: str,
74
+ pending: list[tuple[CodeNode, str]],
75
+ ):
76
+ kind = KIND_MAP.get(node.type)
77
+ name = decl_name(node, raw)
78
+ next_parent, next_fn = parent_uid, current_fn
79
+ next_impl = impl_type
80
+ if node.type == "impl_item":
81
+ # `impl Calculator { fn compute }` has no name of its own; its
82
+ # functions belong to the type it implements.
83
+ type_node = node.child_by_field_name("type")
84
+ resolved = type_name(type_node, raw) if type_node is not None else None
85
+ next_impl = resolved or impl_type
86
+ if kind and name:
87
+ code_node = make_node(
88
+ node, rel, kind, name, parent_uid, self.language, commit_sha, valid_from
89
+ )
90
+ out.append(code_node)
91
+ if kind in {"class", "function"}:
92
+ next_parent = code_node.uid
93
+ if kind == "function":
94
+ next_fn = name
95
+ if parent_uid is None and next_impl:
96
+ pending.append((code_node, next_impl))
97
+ if node.type == "call_expression":
98
+ fn = node.child_by_field_name("function")
99
+ callee = simple_callee(fn, raw) if fn is not None else None
100
+ if callee:
101
+ self._calls.append(
102
+ (current_fn or name or "", callee, node.start_point[0] + 1, rel)
103
+ )
104
+ for child in node.children:
105
+ self._walk(
106
+ child, raw, rel, out, next_parent, next_fn, commit_sha, valid_from,
107
+ next_impl, pending,
108
+ )
109
+
110
+
111
+ def _attach_to_types(out: list[CodeNode], pending: list[tuple[CodeNode, str]]) -> None:
112
+ """Point functions in an `impl` block at the type they implement."""
113
+ by_name: dict[str, CodeNode] = {}
114
+ for node in out:
115
+ if node.kind == "class":
116
+ by_name.setdefault(node.name, node)
117
+ for code_node, type_name_ in pending:
118
+ parent = by_name.get(type_name_)
119
+ if parent is not None:
120
+ code_node.parent_uid = parent.uid
@@ -0,0 +1,178 @@
1
+ """TypeScript / TSX tree-sitter parser."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from datetime import datetime
7
+ from pathlib import Path
8
+
9
+ import tree_sitter_typescript as tstypescript
10
+ from tree_sitter import Language, Node, Parser
11
+
12
+ from codegraph.models import CodeNode, make_uid, normalize_location_path
13
+ from codegraph.parser.base import BaseParser
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+ _TS_LANGUAGE = Language(tstypescript.language_typescript())
18
+ _TSX_LANGUAGE = Language(tstypescript.language_tsx())
19
+
20
+ _KIND_MAP = {
21
+ "function_declaration": "function",
22
+ "class_declaration": "class",
23
+ "variable_declaration": "variable",
24
+ "method_definition": "function",
25
+ }
26
+
27
+
28
+ class TypeScriptParser(BaseParser):
29
+ """Parse .ts / .tsx for declarations and call relations."""
30
+
31
+ language = "typescript"
32
+
33
+ def parse_file(self, path: Path) -> list[CodeNode]:
34
+ """Parse a TypeScript source file from disk."""
35
+ try:
36
+ rel = normalize_location_path(str(path.resolve().relative_to(Path.cwd())))
37
+ except ValueError:
38
+ rel = normalize_location_path(str(path))
39
+ try:
40
+ source = path.read_text(encoding="utf-8")
41
+ except (OSError, UnicodeDecodeError) as exc:
42
+ logger.warning("Read failed, skip %s: %s", path, exc)
43
+ return []
44
+ return self.parse_text(rel, source)
45
+
46
+ def parse_text(
47
+ self,
48
+ rel_path: str,
49
+ source: str,
50
+ commit_sha: str = "",
51
+ valid_from: datetime | None = None,
52
+ ) -> list[CodeNode]:
53
+ """Parse TypeScript source text into CodeNode records."""
54
+ rel = normalize_location_path(rel_path)
55
+ self._calls.clear()
56
+ raw = source.encode("utf-8")
57
+ suffix = Path(rel_path).suffix.lower()
58
+ lang = _TSX_LANGUAGE if suffix == ".tsx" else _TS_LANGUAGE
59
+ tree = Parser(lang).parse(raw)
60
+ out: list[CodeNode] = []
61
+ self._walk(
62
+ tree.root_node,
63
+ raw,
64
+ rel,
65
+ out,
66
+ parent_uid=None,
67
+ current_fn="",
68
+ commit_sha=commit_sha,
69
+ valid_from=valid_from,
70
+ )
71
+ return out
72
+
73
+ def _walk(
74
+ self,
75
+ node: Node,
76
+ raw: bytes,
77
+ rel: str,
78
+ out: list[CodeNode],
79
+ parent_uid: str | None,
80
+ current_fn: str,
81
+ commit_sha: str = "",
82
+ valid_from: datetime | None = None,
83
+ ) -> None:
84
+ """Depth-first walk collecting declarations and calls."""
85
+ kind = _KIND_MAP.get(node.type)
86
+ name = self._decl_name(node, raw)
87
+ next_parent = parent_uid
88
+ next_fn = current_fn
89
+
90
+ if kind and name:
91
+ code_node = self._make_node(
92
+ node, rel, kind, name, parent_uid, commit_sha, valid_from
93
+ )
94
+ out.append(code_node)
95
+ if kind in {"class", "function"}:
96
+ next_parent = code_node.uid
97
+ if kind == "function":
98
+ next_fn = name
99
+
100
+ if node.type == "call_expression":
101
+ self._record_call(node, raw, rel, current_fn if current_fn else name or "")
102
+
103
+ for child in node.children:
104
+ self._walk(
105
+ child,
106
+ raw,
107
+ rel,
108
+ out,
109
+ next_parent,
110
+ next_fn,
111
+ commit_sha=commit_sha,
112
+ valid_from=valid_from,
113
+ )
114
+
115
+ def _make_node(
116
+ self,
117
+ node: Node,
118
+ rel: str,
119
+ kind: str,
120
+ name: str,
121
+ parent_uid: str | None,
122
+ commit_sha: str = "",
123
+ valid_from: datetime | None = None,
124
+ ) -> CodeNode:
125
+ """Build a CodeNode from an AST node."""
126
+ return CodeNode(
127
+ uid=make_uid(rel, kind, name, node.start_point[0] + 1, commit_sha),
128
+ kind=kind,
129
+ name=name,
130
+ file_path=rel,
131
+ line_start=node.start_point[0] + 1,
132
+ line_end=node.end_point[0] + 1,
133
+ language=self.language,
134
+ parent_uid=parent_uid,
135
+ commit_sha=commit_sha,
136
+ valid_from=valid_from or datetime.fromtimestamp(0),
137
+ valid_to=None,
138
+ )
139
+
140
+ @staticmethod
141
+ def _decl_name(node: Node, raw: bytes) -> str | None:
142
+ """Extract declaration name; variable_declaration digs into declarator."""
143
+ if node.type == "variable_declaration":
144
+ for child in node.children:
145
+ if child.type == "variable_declarator":
146
+ name_node = child.child_by_field_name("name")
147
+ if name_node is not None:
148
+ return raw[name_node.start_byte : name_node.end_byte].decode(
149
+ "utf-8", errors="replace"
150
+ )
151
+ return None
152
+
153
+ name_node = node.child_by_field_name("name")
154
+ if name_node is None:
155
+ return None
156
+ return raw[name_node.start_byte : name_node.end_byte].decode("utf-8", errors="replace")
157
+
158
+ def _record_call(self, node: Node, raw: bytes, rel: str, caller_name: str) -> None:
159
+ """Record a call_expression with its callee name."""
160
+ fn = node.child_by_field_name("function")
161
+ if fn is None:
162
+ return
163
+ callee = self._callee_name(fn, raw)
164
+ if not callee:
165
+ return
166
+ line = node.start_point[0] + 1
167
+ self._calls.append((caller_name, callee, line, rel))
168
+
169
+ @staticmethod
170
+ def _callee_name(fn_node: Node, raw: bytes) -> str | None:
171
+ """Extract a simple callee name (identifier / member property)."""
172
+ if fn_node.type == "identifier":
173
+ return raw[fn_node.start_byte : fn_node.end_byte].decode("utf-8", errors="replace")
174
+ if fn_node.type == "member_expression":
175
+ prop = fn_node.child_by_field_name("property")
176
+ if prop is not None:
177
+ return raw[prop.start_byte : prop.end_byte].decode("utf-8", errors="replace")
178
+ return None