use nadir_core::Key;
use crate::input::Edit;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Caret {
pub(crate) caret: usize,
pub(crate) anchor: usize,
}
impl Caret {
pub(crate) fn at(byte: usize) -> Self {
Self {
caret: byte,
anchor: byte,
}
}
pub(crate) fn selection(&self) -> (usize, usize) {
(self.caret.min(self.anchor), self.caret.max(self.anchor))
}
pub(crate) fn fit(&mut self, text: &str) {
self.caret = floor_boundary(text, self.caret);
self.anchor = floor_boundary(text, self.anchor);
}
}
fn floor_boundary(text: &str, byte: usize) -> usize {
let mut byte = byte.min(text.len());
while !text.is_char_boundary(byte) {
byte -= 1;
}
byte
}
fn previous(text: &str, byte: usize) -> usize {
text[..byte]
.char_indices()
.next_back()
.map_or(0, |(start, _)| start)
}
fn next(text: &str, byte: usize) -> usize {
text[byte..]
.chars()
.next()
.map_or(byte, |c| byte + c.len_utf8())
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Edited {
pub(crate) changed: bool,
pub(crate) submitted: bool,
pub(crate) escaped: bool,
}
pub(crate) fn apply(text: &mut String, caret: &mut Caret, edit: Edit) -> Edited {
let mut edited = Edited::default();
let (start, end) = caret.selection();
let cut = |text: &mut String, caret: &mut Caret| {
if start != end {
text.replace_range(start..end, "");
*caret = Caret::at(start);
true
} else {
false
}
};
match edit {
Edit::Char(character) => {
cut(text, caret);
text.insert(caret.caret, character);
*caret = Caret::at(caret.caret + character.len_utf8());
edited.changed = true;
}
Edit::Key {
key,
shift,
command,
} => match key {
Key::Backspace | Key::Delete => {
if cut(text, caret) {
edited.changed = true;
} else {
let at = caret.caret;
let (from, to) = if key == Key::Backspace {
(previous(text, at), at)
} else {
(at, next(text, at))
};
if from != to {
text.replace_range(from..to, "");
*caret = Caret::at(from);
edited.changed = true;
}
}
}
Key::Left | Key::Right | Key::Home | Key::End => {
let to = match key {
Key::Home => 0,
Key::End => text.len(),
_ if !shift && start != end => {
if key == Key::Left {
start
} else {
end
}
}
Key::Left => previous(text, caret.caret),
_ => next(text, caret.caret),
};
caret.caret = to;
if !shift {
caret.anchor = to;
}
}
Key::A if command => {
*caret = Caret {
caret: text.len(),
anchor: 0,
};
}
Key::Enter | Key::NumpadEnter => edited.submitted = true,
Key::Escape => edited.escaped = true,
_ => {}
},
}
edited
}
#[cfg(test)]
mod tests {
use super::*;
fn key(key: Key) -> Edit {
Edit::Key {
key,
shift: false,
command: false,
}
}
fn shifted(key: Key) -> Edit {
Edit::Key {
key,
shift: true,
command: false,
}
}
fn run(text: &str, at: usize, edits: &[Edit]) -> (String, usize, usize) {
let mut text = text.to_string();
let mut caret = Caret::at(at);
for &edit in edits {
apply(&mut text, &mut caret, edit);
}
(text, caret.caret, caret.anchor)
}
#[test]
fn typing_inserts_at_the_caret_and_backspace_and_delete_remove_one_character() {
let typed: Vec<Edit> = "abc".chars().map(Edit::Char).collect();
assert_eq!(run("", 0, &typed), ("abc".into(), 3, 3));
assert_eq!(run("ac", 1, &[Edit::Char('b')]), ("abc".into(), 2, 2));
assert_eq!(run("abc", 3, &[key(Key::Backspace)]), ("ab".into(), 2, 2));
assert_eq!(run("abc", 0, &[key(Key::Delete)]), ("bc".into(), 0, 0));
assert_eq!(run("ab", 0, &[key(Key::Backspace)]), ("ab".into(), 0, 0));
assert_eq!(run("ab", 2, &[key(Key::Delete)]), ("ab".into(), 2, 2));
assert_eq!(run("aéb", 3, &[key(Key::Backspace)]), ("ab".into(), 1, 1));
assert_eq!(run("aéb", 1, &[key(Key::Delete)]), ("ab".into(), 1, 1));
assert_eq!(
run("", 0, &[Edit::Char('c'), key(Key::Backspace)]),
("".into(), 0, 0)
);
}
#[test]
fn the_caret_moves_by_characters_and_shift_selects() {
assert_eq!(run("aéb", 3, &[key(Key::Left)]), ("aéb".into(), 1, 1));
assert_eq!(run("aéb", 1, &[key(Key::Right)]), ("aéb".into(), 3, 3));
assert_eq!(run("ab", 0, &[key(Key::Left)]), ("ab".into(), 0, 0));
assert_eq!(run("ab", 2, &[key(Key::Right)]), ("ab".into(), 2, 2));
assert_eq!(run("abc", 1, &[key(Key::End)]), ("abc".into(), 3, 3));
assert_eq!(run("abc", 1, &[key(Key::Home)]), ("abc".into(), 0, 0));
let selected = run("abcd", 3, &[shifted(Key::Left), shifted(Key::Left)]);
assert_eq!(selected, ("abcd".into(), 1, 3));
assert_eq!(run("abcd", 1, &[shifted(Key::End)]), ("abcd".into(), 4, 1));
}
#[test]
fn typing_replaces_a_selection_and_left_or_right_ends_it_at_its_edge() {
let mut text = "abcd".to_string();
let mut caret = Caret {
caret: 1,
anchor: 3,
};
apply(&mut text, &mut caret, Edit::Char('x'));
assert_eq!((text.as_str(), caret), ("axd", Caret::at(2)));
for (key, at) in [(Key::Left, 1), (Key::Right, 3)] {
let mut caret = Caret {
caret: 3,
anchor: 1,
};
apply(&mut "abcd".to_string(), &mut caret, self::key(key));
assert_eq!(caret, Caret::at(at), "{key:?}");
}
let mut caret = Caret {
caret: 3,
anchor: 1,
};
let mut text = "abcd".to_string();
let edited = apply(&mut text, &mut caret, key(Key::Delete));
assert!(edited.changed);
assert_eq!((text.as_str(), caret), ("ad", Caret::at(1)));
}
#[test]
fn control_or_super_a_selects_all_and_enter_and_escape_are_told() {
let command = Edit::Key {
key: Key::A,
shift: false,
command: true,
};
assert_eq!(run("abc", 1, &[command]), ("abc".into(), 3, 0));
assert_eq!(run("abc", 1, &[key(Key::A)]), ("abc".into(), 1, 1));
let mut text = "x".to_string();
let mut caret = Caret::at(1);
let enter = apply(&mut text, &mut caret, key(Key::Enter));
let numpad = apply(&mut text, &mut caret, key(Key::NumpadEnter));
let escape = apply(&mut text, &mut caret, key(Key::Escape));
assert!(enter.submitted && numpad.submitted && escape.escaped);
assert!(!enter.changed && !escape.changed);
for key in [Key::Up, Key::Down, Key::Tab, Key::PageUp, Key::PageDown] {
assert_eq!(run("ab", 1, &[self::key(key)]), ("ab".into(), 1, 1));
}
}
#[test]
fn a_caret_fits_a_text_changed_under_it() {
let mut caret = Caret {
caret: 9,
anchor: 2,
};
caret.fit("aé");
assert_eq!(
caret,
Caret {
caret: 3,
anchor: 1
}
);
let mut caret = Caret::at(2);
caret.fit("a");
let mut text = "a".to_string();
apply(&mut text, &mut caret, Edit::Char('b'));
assert_eq!(text, "ab");
}
}