构建完整的代码图(增量构建)
(&mut self, dir: &Path)
| 684 | |
| 685 | /// 构建完整的代码图(增量构建) |
| 686 | pub fn build_code_graph(&mut self, dir: &Path) -> Result<CodeGraph, String> { |
| 687 | // 1. 尝试从本地数据库加载现有的图 |
| 688 | let mut code_graph = self._load_existing_code_graph(dir)?; |
| 689 | let has_existing_data = code_graph.is_some(); |
| 690 | |
| 691 | if let Some(ref mut existing_graph) = code_graph { |
| 692 | info!("Loaded existing CodeGraph with {} functions", existing_graph.functions.len()); |
| 693 | } else { |
| 694 | info!("No existing CodeGraph found, starting fresh analysis"); |
| 695 | code_graph = Some(CodeGraph::new()); |
| 696 | } |
| 697 | |
| 698 | let mut code_graph = code_graph.unwrap(); |
| 699 | |
| 700 | // 2. 扫描目录下的所有文件 |
| 701 | let files = self.scan_directory(dir); |
| 702 | info!("Found {} files to process", files.len()); |
| 703 | |
| 704 | // 3. 加载文件哈希值(如果存在) |
| 705 | let mut file_hashes = self._load_file_hashes(dir)?; |
| 706 | |
| 707 | // 4. 逐个处理文件,检查是否需要重新解析 |
| 708 | let mut processed_files = 0; |
| 709 | let mut skipped_files = 0; |
| 710 | |
| 711 | for file_path in files { |
| 712 | if self._should_skip_file(&file_path, &mut file_hashes)? { |
| 713 | skipped_files += 1; |
| 714 | continue; |
| 715 | } |
| 716 | |
| 717 | if let Err(e) = self.parse_file(&file_path) { |
| 718 | warn!("Failed to parse {}: {}", file_path.display(), e); |
| 719 | } else { |
| 720 | processed_files += 1; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | info!("File processing completed: {} processed, {} skipped", processed_files, skipped_files); |
| 725 | |
| 726 | // 5. 如果这是增量构建,需要合并新解析的函数 |
| 727 | if has_existing_data { |
| 728 | if !self.file_functions.is_empty() { |
| 729 | self._merge_new_functions_to_code_graph(&mut code_graph); |
| 730 | } |
| 731 | // 如果没有新解析的函数,保持现有的图不变 |
| 732 | } else { |
| 733 | // 全量构建:直接添加所有函数 |
| 734 | for (_file_path, functions) in &self.file_functions { |
| 735 | for function in functions { |
| 736 | code_graph.add_function(function.clone()); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // 6. 分析调用关系 |
| 742 | self._analyze_call_relations(&mut code_graph); |
| 743 |
no test coverage detected