use crate::text::{EditableText, Selection};
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Movement {
Left,
Right,
LeftOfLine,
RightOfLine,
}
pub fn movement(m: Movement, s: Selection, text: &impl EditableText, modify: bool) -> Selection {
let offset = match m {
Movement::Left => {
if s.is_caret() || modify {
if let Some(offset) = text.prev_grapheme_offset(s.end) {
offset
} else {
0
}
} else {
s.min()
}
}
Movement::Right => {
if s.is_caret() || modify {
if let Some(offset) = text.next_grapheme_offset(s.end) {
offset
} else {
s.end
}
} else {
s.max()
}
}
Movement::LeftOfLine => 0,
Movement::RightOfLine => text.len(),
};
Selection::new(if modify { s.start } else { offset }, offset)
}