Add helpers
This commit is contained in:
+24
-1
@@ -14,9 +14,32 @@ class LocalGraph:
|
||||
self.graph = nx.DiGraph()
|
||||
|
||||
# --- Creation ---
|
||||
def add_node(self, node_id: str, **attrs):
|
||||
def add_node(self, node_id: str | dict, **attrs):
|
||||
"""
|
||||
If a dict is passed instead of a plain string, build the node_id
|
||||
as: <lang>::<type>::<file>::<name>
|
||||
"""
|
||||
if isinstance(node_id, dict):
|
||||
lang = node_id.get("lang", "unknown")
|
||||
kind = node_id.get("type", "Symbol")
|
||||
name = node_id.get("name", "unknown")
|
||||
fpath = node_id.get("file", "")
|
||||
node_id = f"{lang}::{kind}::{fpath}::{name}"
|
||||
self.graph.add_node(node_id, **attrs)
|
||||
|
||||
def _add_import_edges(self, src: str, imports: list[str]):
|
||||
for imp in imports:
|
||||
# Resolve import to a file node if present
|
||||
target = f"file::{imp}"
|
||||
self.add_edge(src, target, "imports")
|
||||
|
||||
def _add_call_edges(self, src: str, calls: list[str], file_path: str):
|
||||
for cal in calls:
|
||||
target = f"rust::function::{file_path}::{cal}"
|
||||
self.add_edge(src, target, "calls")
|
||||
|
||||
|
||||
|
||||
# --- Persistence ---
|
||||
def save(self):
|
||||
with open(self.graph_path, "wb") as f:
|
||||
|
||||
Reference in New Issue
Block a user