(path: &Path, source: &str)
| 11 | use tracing::trace; |
| 12 | |
| 13 | pub(crate) fn extract_python(path: &Path, source: &str) -> ExtractionResult { |
| 14 | let mut result = ExtractionResult::default(); |
| 15 | let file_node = make_file_node(path); |
| 16 | let file_id = file_node.id.clone(); |
| 17 | result.nodes.push(file_node); |
| 18 | |
| 19 | let lines: Vec<&str> = source.lines().collect(); |
| 20 | let ps = path_str(path); |
| 21 | |
| 22 | let mut class_ids: HashMap<String, String> = HashMap::new(); |
| 23 | for cap in RE_PY_CLASS.captures_iter(source) { |
| 24 | let name = &cap[2]; |
| 25 | let line = line_of(source, &cap); |
| 26 | let node = make_node(name, path, NodeType::Class, line); |
| 27 | let node_id = node.id.clone(); |
| 28 | class_ids.insert(name.to_string(), node_id.clone()); |
| 29 | result.nodes.push(node); |
| 30 | result.edges.push(make_edge( |
| 31 | &file_id, |
| 32 | &node_id, |
| 33 | "defines", |
| 34 | path, |
| 35 | Confidence::Extracted, |
| 36 | )); |
| 37 | } |
| 38 | |
| 39 | // Functions / methods: `def foo(...):` |
| 40 | let mut functions: Vec<(String, String, usize, usize)> = Vec::new(); |
| 41 | let func_matches: Vec<_> = RE_PY_FUNC.captures_iter(source).collect(); |
| 42 | for (i, cap) in func_matches.iter().enumerate() { |
| 43 | let indent = cap[1].len(); |
| 44 | let name = cap[2].to_string(); |
| 45 | let start_line = line_of(source, cap); |
| 46 | |
| 47 | let node_type = if indent > 0 { |
| 48 | NodeType::Method |
| 49 | } else { |
| 50 | NodeType::Function |
| 51 | }; |
| 52 | let node = make_node(&name, path, node_type, start_line); |
| 53 | let node_id = node.id.clone(); |
| 54 | |
| 55 | let parent_id = if indent > 0 { |
| 56 | let mut parent = None; |
| 57 | for line_idx in (0..start_line.saturating_sub(1)).rev() { |
| 58 | if let Some(line) = lines.get(line_idx) |
| 59 | && let Some(cls_cap) = RE_PY_CLASS_LOOKUP.captures(line) |
| 60 | && cls_cap[1].len() < indent |
| 61 | { |
| 62 | parent = class_ids.get(&cls_cap[2]).cloned(); |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | parent.unwrap_or_else(|| file_id.clone()) |
| 67 | } else { |
| 68 | file_id.clone() |
| 69 | }; |
| 70 |
no test coverage detected