1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::fmt;
use tree_sitter::Range;

/// A chunk of code with a subtree and a range.
#[derive(Debug)]
pub struct Chunk {
    /// Subtree representation of the code chunk.
    pub subtree: String,
    /// Range of the code chunk.
    pub range: Range,
    /// Size of the code chunk.
    pub size: usize,
}

impl fmt::Display for Chunk {
    /// Display the chunk with its range and subtree.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[{start}..{end}]: {size}\n{substree}",
            start = self.range.start_point.row,
            end = self.range.end_point.row,
            size = self.size,
            substree = self.subtree,
        )
    }
}

impl Chunk {
    pub fn utf8_lossy(&self, code: &[u8]) -> String {
        String::from_utf8_lossy(&code[self.range.start_byte..self.range.end_byte]).to_string()
    }
}