Skip to main content

git_topology/chunking/
mod.rs

1pub mod languages;
2pub mod parser;
3
4pub use parser::parse;
5
6use anyhow::Result;
7
8#[derive(Debug, Clone)]
9pub struct CodeChunk {
10    pub text: String,
11    pub start_line: usize,
12    pub end_line: usize,
13}
14
15pub fn chunk_code(text: &str, file_path: Option<&str>) -> Result<Vec<CodeChunk>> {
16    if let Some(path) = file_path {
17        if let Some(language) = languages::detect_language(path) {
18            return parser::parse_with_tree_sitter(text, language);
19        }
20    }
21
22    Ok(vec![CodeChunk {
23        text: text.to_string(),
24        start_line: 0,
25        end_line: text.lines().count(),
26    }])
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn chunk_unsupported_language_returns_whole_file() {
35        let text = "hello world\nfoo bar\n";
36        let chunks = chunk_code(text, Some("file.txt")).unwrap();
37        assert_eq!(chunks.len(), 1);
38        assert_eq!(chunks[0].text, text);
39        assert_eq!(chunks[0].start_line, 0);
40    }
41
42    #[test]
43    fn chunk_no_path_returns_whole_file() {
44        let text = "fn main() {}";
45        let chunks = chunk_code(text, None).unwrap();
46        assert_eq!(chunks.len(), 1);
47    }
48
49    #[test]
50    fn chunk_rust_finds_functions() {
51        let code = "fn foo() { let x = 1; }\nfn bar() { let y = 2; }\n";
52        let chunks = chunk_code(code, Some("src/main.rs")).unwrap();
53        assert!(chunks.len() >= 2);
54    }
55
56    #[test]
57    fn chunk_python_finds_functions() {
58        let code = "def foo():\n    pass\n\ndef bar():\n    pass\n";
59        let chunks = chunk_code(code, Some("main.py")).unwrap();
60        assert!(chunks.len() >= 2);
61    }
62
63    #[test]
64    fn chunk_lines_are_ordered() {
65        let code = "fn a() {}\nfn b() {}\nfn c() {}\n";
66        let chunks = chunk_code(code, Some("lib.rs")).unwrap();
67        let starts: Vec<usize> = chunks.iter().map(|c| c.start_line).collect();
68        let mut sorted = starts.clone();
69        sorted.sort();
70        assert_eq!(starts, sorted);
71    }
72}