Save all annotations (file definitions, marks, symbol definitions) to `.coderlm/annotations.json` in the project root.
(
root: &Path,
file_tree: &Arc<FileTree>,
symbol_table: &Arc<SymbolTable>,
)
| 27 | /// Save all annotations (file definitions, marks, symbol definitions) |
| 28 | /// to `.coderlm/annotations.json` in the project root. |
| 29 | pub fn save_annotations( |
| 30 | root: &Path, |
| 31 | file_tree: &Arc<FileTree>, |
| 32 | symbol_table: &Arc<SymbolTable>, |
| 33 | ) -> Result<(), String> { |
| 34 | let mut data = AnnotationData::default(); |
| 35 | |
| 36 | // Collect file definitions and marks |
| 37 | for entry in file_tree.files.iter() { |
| 38 | let fe = entry.value(); |
| 39 | if let Some(def) = &fe.definition { |
| 40 | data.file_definitions |
| 41 | .insert(fe.rel_path.clone(), def.clone()); |
| 42 | } |
| 43 | if !fe.marks.is_empty() { |
| 44 | let mark_strs: Vec<String> = fe |
| 45 | .marks |
| 46 | .iter() |
| 47 | .map(|m| format!("{:?}", m).to_lowercase()) |
| 48 | .collect(); |
| 49 | data.file_marks.insert(fe.rel_path.clone(), mark_strs); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Collect symbol definitions |
| 54 | for entry in symbol_table.symbols.iter() { |
| 55 | let sym = entry.value(); |
| 56 | if let Some(def) = &sym.definition { |
| 57 | let key = SymbolTable::make_key(&sym.file, &sym.name); |
| 58 | data.symbol_definitions.insert(key, def.clone()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | let annotations_path = root.join(ANNOTATIONS_FILE); |
| 63 | if let Some(parent) = annotations_path.parent() { |
| 64 | std::fs::create_dir_all(parent) |
| 65 | .map_err(|e| format!("Failed to create annotations dir: {}", e))?; |
| 66 | } |
| 67 | |
| 68 | let json = serde_json::to_string_pretty(&data) |
| 69 | .map_err(|e| format!("Failed to serialize annotations: {}", e))?; |
| 70 | std::fs::write(&annotations_path, json) |
| 71 | .map_err(|e| format!("Failed to write annotations: {}", e))?; |
| 72 | |
| 73 | debug!( |
| 74 | "Saved annotations: {} file defs, {} file marks, {} symbol defs", |
| 75 | data.file_definitions.len(), |
| 76 | data.file_marks.len(), |
| 77 | data.symbol_definitions.len() |
| 78 | ); |
| 79 | |
| 80 | Ok(()) |
| 81 | } |
| 82 | |
| 83 | /// Load annotations from `.coderlm/annotations.json` and apply them |
| 84 | /// to the file tree and symbol table. |