|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use tree_sitter::TreeCursor; |
| 4 | + |
| 5 | +// Handler for Rust function_item |
| 6 | +fn handle_rs_function(cursor: &mut TreeCursor, source_code: &str) -> String { |
| 7 | + let mut function_signature = String::new(); |
| 8 | + if cursor.goto_first_child() { |
| 9 | + loop { |
| 10 | + let node = cursor.node(); |
| 11 | + match node.kind() { |
| 12 | + "identifier" => { |
| 13 | + let start_byte = node.start_byte(); |
| 14 | + let end_byte = node.end_byte(); |
| 15 | + let child_name = &source_code[start_byte..end_byte]; |
| 16 | + function_signature.push_str(&format!("fn {}", child_name)); |
| 17 | + } |
| 18 | + "parameters" => { |
| 19 | + let start_byte = node.start_byte(); |
| 20 | + let end_byte = node.end_byte(); |
| 21 | + let parameters = &source_code[start_byte..end_byte]; |
| 22 | + function_signature.push_str(&format!("{}", parameters)); |
| 23 | + } |
| 24 | + "type_identifier" => { |
| 25 | + let start_byte = node.start_byte(); |
| 26 | + let end_byte = node.end_byte(); |
| 27 | + let return_type = &source_code[start_byte..end_byte]; |
| 28 | + function_signature.push_str(&format!(" -> {}", return_type)); |
| 29 | + } |
| 30 | + _ => {} |
| 31 | + } |
| 32 | + |
| 33 | + if !cursor.goto_next_sibling() { |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + cursor.goto_parent(); |
| 38 | + } |
| 39 | + function_signature |
| 40 | +} |
| 41 | + |
| 42 | +// Handler for Rust struct_item |
| 43 | +fn handle_rs_struct(cursor: &mut TreeCursor, source_code: &str) -> String { |
| 44 | + let mut struct_signature = String::new(); |
| 45 | + if cursor.goto_first_child() { |
| 46 | + loop { |
| 47 | + let node = cursor.node(); |
| 48 | + if node.kind() == "identifier" { |
| 49 | + let start_byte = node.start_byte(); |
| 50 | + let end_byte = node.end_byte(); |
| 51 | + let child_name = &source_code[start_byte..end_byte]; |
| 52 | + struct_signature.push_str(&format!("struct {} {{", child_name)); |
| 53 | + } |
| 54 | + // You may want to handle fields here... |
| 55 | + |
| 56 | + if !cursor.goto_next_sibling() { |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + cursor.goto_parent(); |
| 61 | + } |
| 62 | + struct_signature.push_str("}"); |
| 63 | + struct_signature |
| 64 | +} |
| 65 | + |
| 66 | +pub fn get_handlers() -> HashMap<&'static str, fn(&mut TreeCursor, &str) -> String> { |
| 67 | + let mut handlers: HashMap<&str, fn(&mut TreeCursor, &str) -> String> = HashMap::new(); |
| 68 | + handlers.insert("function_item", handle_rs_function); |
| 69 | + handlers.insert("struct_item", handle_rs_struct); |
| 70 | + // Insert more handlers as needed |
| 71 | + handlers |
| 72 | +} |
0 commit comments