code_splitter/
chunk.rs

1use std::fmt;
2use tree_sitter::Range;
3
4/// A chunk of code with a subtree and a range.
5#[derive(Debug)]
6pub struct Chunk {
7    /// Subtree representation of the code chunk.
8    pub subtree: String,
9    /// Range of the code chunk.
10    pub range: Range,
11    /// Size of the code chunk.
12    pub size: usize,
13}
14
15impl fmt::Display for Chunk {
16    /// Display the chunk with its range and subtree.
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        write!(
19            f,
20            "[{start}..{end}]: {size}\n{substree}",
21            start = self.range.start_point.row,
22            end = self.range.end_point.row,
23            size = self.size,
24            substree = self.subtree,
25        )
26    }
27}
28
29impl Chunk {
30    pub fn utf8_lossy(&self, code: &[u8]) -> String {
31        String::from_utf8_lossy(&code[self.range.start_byte..self.range.end_byte]).to_string()
32    }
33}