解析单个文件(完整实现,支持多语言)
(&mut self, file_path: &PathBuf)
| 401 | |
| 402 | /// 解析单个文件(完整实现,支持多语言) |
| 403 | pub fn parse_file(&mut self, file_path: &PathBuf) -> Result<(), String> { |
| 404 | info!("Parsing file: {}", file_path.display()); |
| 405 | |
| 406 | // 检查文件是否存在 |
| 407 | if !file_path.exists() { |
| 408 | return Err(format!("File does not exist: {}", file_path.display())); |
| 409 | } |
| 410 | |
| 411 | // 使用TreeSitter解析器解析文件 |
| 412 | let symbols = self.ts_parser.parse_file(file_path) |
| 413 | .map_err(|e| format!("Failed to parse file {}: {:?}", file_path.display(), e))?; |
| 414 | info!("TreeSitter parsing completed, found {} symbols", symbols.len()); |
| 415 | |
| 416 | |
| 417 | |
| 418 | // 读取文件内容用于代码片段提取 |
| 419 | let file_content = fs::read_to_string(file_path) |
| 420 | .map_err(|e| format!("Failed to read file {}: {}", file_path.display(), e))?; |
| 421 | |
| 422 | let language = self._detect_language(file_path); |
| 423 | let namespace = self._extract_namespace_from_content(&file_content, file_path); |
| 424 | |
| 425 | let mut functions = Vec::new(); |
| 426 | let mut classes = Vec::new(); |
| 427 | let mut function_calls = Vec::new(); |
| 428 | |
| 429 | // 分析每个AST符号 |
| 430 | for symbol in symbols { |
| 431 | let symbol_guard = symbol.read(); |
| 432 | let symbol_ref = symbol_guard.as_ref(); |
| 433 | debug!("Found symbol: {:?} - {}", symbol_ref.symbol_type(), symbol_ref.name()); |
| 434 | |
| 435 | match symbol_ref.symbol_type() { |
| 436 | crate::codegraph::treesitter::structs::SymbolType::FunctionDeclaration => { |
| 437 | // 提取函数信息 |
| 438 | let function = self._extract_function_info(symbol_ref, file_path, &namespace, &language); |
| 439 | functions.push(function); |
| 440 | }, |
| 441 | crate::codegraph::treesitter::structs::SymbolType::StructDeclaration => { |
| 442 | // 提取类/结构体信息 |
| 443 | let class = self._extract_class_info(symbol_ref, file_path, &language, &namespace); |
| 444 | classes.push(class); |
| 445 | }, |
| 446 | crate::codegraph::treesitter::structs::SymbolType::FunctionCall => { |
| 447 | // 提取函数调用信息 |
| 448 | let call_info = self._extract_function_call_info(symbol_ref, file_path); |
| 449 | function_calls.push(call_info); |
| 450 | }, |
| 451 | _ => {} |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | // 注册函数到全局注册表 |
| 456 | for function in &functions { |
| 457 | self.function_registry.insert(function.name.clone(), function.clone()); |
| 458 | } |
| 459 | |
| 460 | // 保存文件函数映射 |