Fix rust connection for graph
This commit is contained in:
+46
-3
@@ -828,7 +828,7 @@ def parse_go_file_cached(filepath_str: str, file_hash: str) -> tuple:
|
|||||||
chunk_text += f"Code:\n{chunk_code}"
|
chunk_text += f"Code:\n{chunk_code}"
|
||||||
|
|
||||||
# Build comprehensive metadata
|
# Build comprehensive metadata
|
||||||
metadata = {
|
parse_rust_file_cached = {
|
||||||
"file": str(filepath),
|
"file": str(filepath),
|
||||||
"name": name,
|
"name": name,
|
||||||
"type": typ,
|
"type": typ,
|
||||||
@@ -1648,6 +1648,26 @@ def parse_rust_file_cached(filepath_str: str, file_hash: str) -> tuple:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"parse_rust_file failed {filepath}: {e}")
|
logger.warning(f"parse_rust_file failed {filepath}: {e}")
|
||||||
|
|
||||||
|
# Very simple call extractor: finds foo(), bar::<T>(), module::foo()
|
||||||
|
call_re = re.compile(r'([A-Za-z_][A-Za-z0-9_]*)\s*!\s*\(|([A-Za-z_][A-Za-z0-9_:]*)\s*\(', re.MULTILINE)
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
for match in call_re.findall(code):
|
||||||
|
name1, name2 = match
|
||||||
|
name = name1 or name2
|
||||||
|
if "::" in name:
|
||||||
|
name = name.split("::")[-1]
|
||||||
|
# filter out keywords and type constructors
|
||||||
|
if name not in ["if", "for", "match", "while", "loop"]:
|
||||||
|
calls.append(name)
|
||||||
|
|
||||||
|
# attach to every chunk belonging to the file
|
||||||
|
for c in chunks:
|
||||||
|
md = dict(c["metadata"])
|
||||||
|
md["calls"] = calls
|
||||||
|
c["metadata"] = md
|
||||||
|
|
||||||
|
|
||||||
final_result = tuple((c["text"], tuple(c["metadata"].items())) for c in chunks)
|
final_result = tuple((c["text"], tuple(c["metadata"].items())) for c in chunks)
|
||||||
logger.info(f"📦 Final result: {len(final_result)} chunks with metadata")
|
logger.info(f"📦 Final result: {len(final_result)} chunks with metadata")
|
||||||
return final_result
|
return final_result
|
||||||
@@ -2124,6 +2144,10 @@ def build_indexes():
|
|||||||
if not name or node_type == "unknown" or not fpath:
|
if not name or node_type == "unknown" or not fpath:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# convert rust defs
|
||||||
|
if "implements_traits" in m and m["implements_traits"]:
|
||||||
|
m["implements"] = m["implements_traits"]
|
||||||
|
|
||||||
# ---- File node ---------------------------------------------
|
# ---- File node ---------------------------------------------
|
||||||
# Keep the same ID (`file::<path>`) but store the full set of
|
# Keep the same ID (`file::<path>`) but store the full set of
|
||||||
# attributes so that downstream code can rely on `name`, `lang`,
|
# attributes so that downstream code can rely on `name`, `lang`,
|
||||||
@@ -2194,6 +2218,15 @@ def build_indexes():
|
|||||||
param_parts = param.split(':')
|
param_parts = param.split(':')
|
||||||
if len(param_parts) >= 2:
|
if len(param_parts) >= 2:
|
||||||
param_type = param_parts[-1].strip()
|
param_type = param_parts[-1].strip()
|
||||||
|
param_type = (
|
||||||
|
param_type
|
||||||
|
.replace('&', '')
|
||||||
|
.replace('mut ', '')
|
||||||
|
.replace('<', '')
|
||||||
|
.replace('>', '')
|
||||||
|
.replace(',', '')
|
||||||
|
.strip()
|
||||||
|
)
|
||||||
param_type = param_type.replace('&', '').replace('mut ', '').strip()
|
param_type = param_type.replace('&', '').replace('mut ', '').strip()
|
||||||
if (param_type and
|
if (param_type and
|
||||||
param_type not in ['self', '&self', '&mut self', 'mut self'] and
|
param_type not in ['self', '&self', '&mut self', 'mut self'] and
|
||||||
@@ -2222,6 +2255,15 @@ def build_indexes():
|
|||||||
param_parts = param.split(':')
|
param_parts = param.split(':')
|
||||||
if len(param_parts) >= 2:
|
if len(param_parts) >= 2:
|
||||||
param_type = param_parts[-1].strip()
|
param_type = param_parts[-1].strip()
|
||||||
|
param_type = (
|
||||||
|
param_type
|
||||||
|
.replace('&', '')
|
||||||
|
.replace('mut ', '')
|
||||||
|
.replace('<', '')
|
||||||
|
.replace('>', '')
|
||||||
|
.replace(',', '')
|
||||||
|
.strip()
|
||||||
|
)
|
||||||
param_type = param_type.replace('&', '').replace('mut ', '').strip()
|
param_type = param_type.replace('&', '').replace('mut ', '').strip()
|
||||||
if (param_type and
|
if (param_type and
|
||||||
param_type not in ['self', '&self', '&mut self', 'mut self'] and
|
param_type not in ['self', '&self', '&mut self', 'mut self'] and
|
||||||
@@ -2286,8 +2328,9 @@ def build_indexes():
|
|||||||
graph.add_edge(file_node_id, node_id, "contains")
|
graph.add_edge(file_node_id, node_id, "contains")
|
||||||
|
|
||||||
# ---------- IMPORTS ----------
|
# ---------- IMPORTS ----------
|
||||||
# The imports metadata might be a list or a comma-separated string
|
if node_type == "import" and "parameters" in m:
|
||||||
# We need to handle both cases
|
imports_raw = m["parameters"] # override metadata pathway
|
||||||
|
else:
|
||||||
imports_raw = m.get("imports", [])
|
imports_raw = m.get("imports", [])
|
||||||
if isinstance(imports_raw, str):
|
if isinstance(imports_raw, str):
|
||||||
# Split the string back into a list
|
# Split the string back into a list
|
||||||
|
|||||||
+32
-11
@@ -4,8 +4,8 @@ use std::env;
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::process;
|
use std::process;
|
||||||
use syn::{
|
use syn::{
|
||||||
spanned::Spanned, FnArg, Item, ItemEnum, ItemFn, ItemImpl, ItemMod, ItemStruct, ItemTrait, Pat,
|
spanned::Spanned, FnArg, Item, ItemEnum, ItemFn, ItemImpl, ItemMod, ItemStruct, ItemTrait,
|
||||||
Type, Visibility,
|
ItemUse, Pat, Type, Visibility,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -88,6 +88,27 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for item in &syntax.items {
|
||||||
|
if let Item::Use(item_use) = item {
|
||||||
|
let path = path_to_string(&item_use.tree);
|
||||||
|
decls.push(RustDecl {
|
||||||
|
name: path.clone(),
|
||||||
|
type_: "import".to_string(),
|
||||||
|
start_line: 0,
|
||||||
|
end_line: 0,
|
||||||
|
visibility: "".to_string(),
|
||||||
|
is_async: false,
|
||||||
|
is_unsafe: false,
|
||||||
|
generics: vec![],
|
||||||
|
traits: vec![],
|
||||||
|
fields: vec![],
|
||||||
|
methods: vec![],
|
||||||
|
return_type: None,
|
||||||
|
parameters: vec![path],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let output = serde_json::to_string(&decls).unwrap_or_else(|_| "[]".to_string());
|
let output = serde_json::to_string(&decls).unwrap_or_else(|_| "[]".to_string());
|
||||||
println!("{}", output);
|
println!("{}", output);
|
||||||
}
|
}
|
||||||
@@ -106,13 +127,13 @@ fn parse_function(item_fn: &ItemFn) -> Option<RustDecl> {
|
|||||||
let mut parameters = Vec::new();
|
let mut parameters = Vec::new();
|
||||||
for input in &item_fn.sig.inputs {
|
for input in &item_fn.sig.inputs {
|
||||||
if let FnArg::Typed(pat_type) = input {
|
if let FnArg::Typed(pat_type) = input {
|
||||||
// pat_type.pat is Box<Pat>
|
let name = if let Pat::Ident(pat_ident) = &*pat_type.pat {
|
||||||
if let Pat::Ident(pat_ident) = &*pat_type.pat {
|
pat_ident.ident.to_string()
|
||||||
parameters.push(pat_ident.ident.to_string());
|
|
||||||
} else {
|
} else {
|
||||||
// fallback: try to stringify the pattern
|
format!("{}", quote::ToTokens::to_token_stream(&pat_type.pat))
|
||||||
parameters.push(format!("{}", quote::ToTokens::to_token_stream(&pat_type.pat)));
|
};
|
||||||
}
|
let ty = type_to_string(&*pat_type.ty);
|
||||||
|
parameters.push(format!("{}: {}", name, ty));
|
||||||
} else if let FnArg::Receiver(_) = input {
|
} else if let FnArg::Receiver(_) = input {
|
||||||
parameters.push("self".to_string());
|
parameters.push("self".to_string());
|
||||||
}
|
}
|
||||||
@@ -263,12 +284,12 @@ fn parse_impl(item_impl: &ItemImpl) -> Option<RustDecl> {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// If this impl implements a trait, extract its path
|
// If this impl implements a trait, extract its path
|
||||||
let traits: Vec<String> = item_impl
|
let implements = item_impl
|
||||||
.trait_
|
.trait_
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|(_, path, _)| path_to_string(path))
|
.map(|(_, path, _)| path_to_string(path))
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
Some(RustDecl {
|
Some(RustDecl {
|
||||||
name: format!("impl_{}", target),
|
name: format!("impl_{}", target),
|
||||||
@@ -284,7 +305,7 @@ fn parse_impl(item_impl: &ItemImpl) -> Option<RustDecl> {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(generic_param_to_string)
|
.map(generic_param_to_string)
|
||||||
.collect(),
|
.collect(),
|
||||||
traits,
|
traits: implements.clone(),
|
||||||
fields: Vec::new(),
|
fields: Vec::new(),
|
||||||
methods,
|
methods,
|
||||||
return_type: None,
|
return_type: None,
|
||||||
|
|||||||
Reference in New Issue
Block a user