|
| 1 | +use super::LanguageExtractor; |
| 2 | +use crate::domain::models::ChunkType; |
| 3 | +use crate::{GitTypeError, Result}; |
| 4 | +use tree_sitter::{Node, Parser}; |
| 5 | + |
| 6 | +pub struct ZigExtractor; |
| 7 | + |
| 8 | +impl LanguageExtractor for ZigExtractor { |
| 9 | + fn tree_sitter_language(&self) -> tree_sitter::Language { |
| 10 | + tree_sitter_zig::LANGUAGE.into() |
| 11 | + } |
| 12 | + |
| 13 | + fn query_patterns(&self) -> &str { |
| 14 | + " |
| 15 | + (function_declaration name: (identifier) @name) @function |
| 16 | + (variable_declaration (identifier) @name \"=\" (struct_declaration)) @struct |
| 17 | + (variable_declaration (identifier) @name \"=\" (enum_declaration)) @enum |
| 18 | + (variable_declaration (identifier) @name \"=\" (union_declaration)) @union |
| 19 | + " |
| 20 | + } |
| 21 | + |
| 22 | + fn comment_query(&self) -> &str { |
| 23 | + "(comment) @comment" |
| 24 | + } |
| 25 | + |
| 26 | + fn capture_name_to_chunk_type(&self, capture_name: &str) -> Option<ChunkType> { |
| 27 | + match capture_name { |
| 28 | + "function" => Some(ChunkType::Function), |
| 29 | + "struct" => Some(ChunkType::Struct), |
| 30 | + "enum" => Some(ChunkType::Enum), |
| 31 | + "union" => Some(ChunkType::Struct), |
| 32 | + _ => None, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fn extract_name(&self, node: Node, source_code: &str, _capture_name: &str) -> Option<String> { |
| 37 | + self.extract_name_from_node(node, source_code) |
| 38 | + } |
| 39 | + |
| 40 | + fn middle_implementation_query(&self) -> &str { |
| 41 | + " |
| 42 | + (for_statement) @for_loop |
| 43 | + (while_statement) @while_loop |
| 44 | + (if_statement) @if_block |
| 45 | + (switch_expression) @switch_expr |
| 46 | + (block) @code_block |
| 47 | + " |
| 48 | + } |
| 49 | + |
| 50 | + fn middle_capture_name_to_chunk_type(&self, capture_name: &str) -> Option<ChunkType> { |
| 51 | + match capture_name { |
| 52 | + "for_loop" | "while_loop" => Some(ChunkType::Loop), |
| 53 | + "if_block" => Some(ChunkType::Conditional), |
| 54 | + "switch_expr" => Some(ChunkType::Conditional), |
| 55 | + "code_block" => Some(ChunkType::CodeBlock), |
| 56 | + _ => None, |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl ZigExtractor { |
| 62 | + fn extract_name_from_node(&self, node: Node, source_code: &str) -> Option<String> { |
| 63 | + let mut cursor = node.walk(); |
| 64 | + if cursor.goto_first_child() { |
| 65 | + loop { |
| 66 | + let child = cursor.node(); |
| 67 | + if child.kind() == "identifier" { |
| 68 | + let start = child.start_byte(); |
| 69 | + let end = child.end_byte(); |
| 70 | + return Some(source_code[start..end].to_string()); |
| 71 | + } |
| 72 | + if !cursor.goto_next_sibling() { |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + None |
| 78 | + } |
| 79 | + |
| 80 | + pub fn create_parser() -> Result<Parser> { |
| 81 | + let mut parser = Parser::new(); |
| 82 | + parser |
| 83 | + .set_language(&tree_sitter_zig::LANGUAGE.into()) |
| 84 | + .map_err(|e| { |
| 85 | + GitTypeError::ExtractionFailed(format!("Failed to set Zig language: {}", e)) |
| 86 | + })?; |
| 87 | + Ok(parser) |
| 88 | + } |
| 89 | +} |
0 commit comments