use yew::prelude::*;
use crate::js::PerspectiveValidationError;
pub struct Cursor<'a> {
row: usize,
col: usize,
index: u32,
pub err: &'a Option<PerspectiveValidationError>,
pub txt: &'a str,
pub noderef: NodeRef,
pub auto: Option<String>,
}
impl<'a> Cursor<'a> {
pub fn new(err: &'a Option<PerspectiveValidationError>) -> 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 as i32
&& err.column >= self.col as i32
&& err.column < (self.col + self.txt.len()) as i32
} 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(usize) -> T>(self, f: F) -> impl Iterator<Item = T> {
(0..self.row).map(f)
}
pub fn increment_column(&mut self, size: usize) {
self.col += size;
self.index += size as u32;
}
pub fn increment_line(&mut self) {
self.row += 1;
self.col = 0;
self.index += 1;
}
}