perspective_viewer/exprtk/cursor.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors. ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use perspective_client::ExprValidationError;
14use yew::prelude::*;
15
16/// A tokenizer cursor for ExprTK parsing.
17///
18/// Because ExprTK reports errors in column/row coordinates and visually needs
19/// to be applied to an entire token rather than a single character, we need
20/// fairly obnoxious counter logic to figure out how to generate the resulting
21/// syntax-highlighted HTML. The `Counter<'a>` struct encapsulates this logic,
22/// generating a `NodeRef` to any autocomplete-able `<span>` tokens, as well
23/// as other convenient data for HTML rendering, and can be called incrementally
24/// while iterating tokens after parsing.
25pub struct Cursor<'a> {
26 row: u32,
27 col: u32,
28 index: u32,
29
30 /// Error text
31 pub err: &'a Option<ExprValidationError>,
32
33 /// Expression text
34 pub txt: &'a str,
35
36 /// Actual element
37 pub noderef: NodeRef,
38
39 /// Auto complete text
40 pub auto: Option<String>,
41}
42
43impl<'a> Cursor<'a> {
44 /// Create a new Cursor (given its error state).
45 pub fn new(err: &'a Option<ExprValidationError>) -> Self {
46 Self {
47 row: 1,
48 col: 0,
49 index: 0,
50 err,
51 txt: "",
52 auto: None,
53 noderef: NodeRef::default(),
54 }
55 }
56
57 /// Is the cursor currently overlapping a token with an error?
58 pub const fn is_error(&self) -> bool {
59 if let Some(err) = &self.err {
60 err.line + 1 == self.row
61 && err.column >= self.col
62 && err.column < (self.col + self.txt.len() as u32)
63 } else {
64 false
65 }
66 }
67
68 /// Is the cursor currently overlapping an autocomplete-able token?
69 pub const fn is_autocomplete(&self, position: u32) -> bool {
70 position > self.index && position <= self.index + self.txt.len() as u32
71 }
72
73 /// TODO this is alot of type backage for what could just be `num_rows()`.
74 pub fn map_rows<T, F: Fn(u32) -> T>(self, f: F) -> impl Iterator<Item = T> {
75 (0..self.row).map(f)
76 }
77
78 /// Increment the counter column by `size` characters.
79 pub fn increment_column(&mut self, size: u32) {
80 self.col += size;
81 self.index += size;
82 }
83
84 /// Increment to the next line, typewriter-style.
85 pub fn increment_line(&mut self) {
86 self.row += 1;
87 self.col = 0;
88 self.index += 1;
89 }
90}