use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Point {
pub byte: usize,
pub row: usize,
pub column: usize,
}
impl Point {
pub fn new(byte: usize, ts_point: tree_sitter::Point) -> Self {
Self {
byte,
row: ts_point.row,
column: ts_point.column,
}
}
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.row + 1, self.column + 1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Span {
pub start: Point,
pub end: Point,
}
impl Span {
pub fn new(start: Point, end: Point) -> Self {
Self { start, end }
}
pub fn from_node(node: &tree_sitter::Node) -> Self {
Self {
start: Point::new(node.start_byte(), node.start_position()),
end: Point::new(node.end_byte(), node.end_position()),
}
}
pub fn byte_range(&self) -> std::ops::Range<usize> {
self.start.byte..self.end.byte
}
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", self.start, self.end)
}
}