use perspective_client::ExprValidationError;
use yew::prelude::*;
pub struct Cursor<'a> {
row: u32,
col: u32,
index: u32,
pub err: &'a Option<ExprValidationError>,
pub txt: &'a str,
pub noderef: NodeRef,
pub auto: Option<String>,
}
impl<'a> Cursor<'a> {
pub fn new(err: &'a Option<ExprValidationError>) -> Self {
Self {
row: 1,
col: 0,
index: 0,
err,
txt: "",
auto: None,
noderef: NodeRef::default(),
}
}
pub const fn is_error(&self) -> bool {
if let Some(err) = &self.err {
err.line + 1 == self.row
&& err.column >= self.col
&& err.column < (self.col + self.txt.len() as u32)
} else {
false
}
}
pub const fn is_autocomplete(&self, position: u32) -> bool {
position > self.index && position <= self.index + self.txt.len() as u32
}
pub fn map_rows<T, F: Fn(u32) -> T>(self, f: F) -> impl Iterator<Item = T> {
(0..self.row).map(f)
}
pub fn increment_column(&mut self, size: u32) {
self.col += size;
self.index += size;
}
pub fn increment_line(&mut self) {
self.row += 1;
self.col = 0;
self.index += 1;
}
}