Skip to content

Commit 34d666a

Browse files
committed
feat: each handler for each language
1 parent 6e2d5a3 commit 34d666a

7 files changed

Lines changed: 291 additions & 210 deletions

File tree

crates/codeindex/src/handlers.rs

Lines changed: 0 additions & 191 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::collections::HashMap;
2+
3+
use tree_sitter::TreeCursor;
4+
5+
// Handler for JavaScript function_declaration
6+
fn handle_js_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!("function {}", child_name));
17+
}
18+
"formal_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+
// JavaScript doesn't have explicit return types, so we don't handle that here
25+
_ => {}
26+
}
27+
28+
if !cursor.goto_next_sibling() {
29+
break;
30+
}
31+
}
32+
cursor.goto_parent();
33+
}
34+
function_signature
35+
}
36+
37+
pub fn get_handlers() -> HashMap<&'static str, fn(&mut TreeCursor, &str) -> String> {
38+
let mut handlers: HashMap<&str, fn(&mut TreeCursor, &str) -> String> = HashMap::new();
39+
handlers.insert("function_declaration", handle_js_function);
40+
// Insert more handlers as needed
41+
handlers
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
mod javascript;
2+
mod python;
3+
mod rust;
4+
mod typescript;
5+
6+
use std::collections::HashMap;
7+
8+
use tree_sitter::TreeCursor;
9+
10+
pub fn get_handlers(extension: &str) -> HashMap<&'static str, fn(&mut TreeCursor, &str) -> String> {
11+
match extension {
12+
"js" | "jsx" => javascript::get_handlers(),
13+
"ts" | "tsx" => typescript::get_handlers(),
14+
"rs" => rust::get_handlers(),
15+
"py" => python::get_handlers(),
16+
_ => HashMap::new(),
17+
}
18+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::collections::HashMap;
2+
3+
use tree_sitter::TreeCursor;
4+
5+
// Handler for Python function_definition
6+
fn handle_py_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!("def {}", 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+
// Python doesn't have explicit return types, so we don't handle that here
25+
_ => {}
26+
}
27+
28+
if !cursor.goto_next_sibling() {
29+
break;
30+
}
31+
}
32+
cursor.goto_parent();
33+
}
34+
function_signature
35+
}
36+
37+
// Handler for Python class_definition
38+
fn handle_py_class(cursor: &mut TreeCursor, source_code: &str) -> String {
39+
let mut class_signature = String::new();
40+
if cursor.goto_first_child() {
41+
loop {
42+
let node = cursor.node();
43+
if node.kind() == "identifier" {
44+
let start_byte = node.start_byte();
45+
let end_byte = node.end_byte();
46+
let child_name = &source_code[start_byte..end_byte];
47+
class_signature.push_str(&format!("class {}:", child_name));
48+
}
49+
// You may want to handle fields, methods, etc. here...
50+
51+
if !cursor.goto_next_sibling() {
52+
break;
53+
}
54+
}
55+
cursor.goto_parent();
56+
}
57+
class_signature
58+
}
59+
60+
pub fn get_handlers() -> HashMap<&'static str, fn(&mut TreeCursor, &str) -> String> {
61+
let mut handlers: HashMap<&str, fn(&mut TreeCursor, &str) -> String> = HashMap::new();
62+
handlers.insert("function_definition", handle_py_function);
63+
handlers.insert("class_definition", handle_py_class);
64+
// Insert more handlers as needed
65+
handlers
66+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)