2020-09-05 17:01:05 +04:00
|
|
|
use crate::state::{Direction, Granularity, Mode, State};
|
2020-06-07 19:15:39 +04:00
|
|
|
|
|
|
|
/// A command is a function that takes the current state and a count, and does a side-effect on the
|
|
|
|
/// state (usually by creating and applying a transaction).
|
2020-06-07 19:31:11 +04:00
|
|
|
pub type Command = fn(state: &mut State, count: usize);
|
2020-06-07 19:15:39 +04:00
|
|
|
|
2020-06-07 19:31:11 +04:00
|
|
|
pub fn move_char_left(state: &mut State, count: usize) {
|
2020-06-07 19:15:39 +04:00
|
|
|
// TODO: use a transaction
|
2020-06-07 19:31:11 +04:00
|
|
|
let selection = state.move_selection(
|
|
|
|
// TODO: remove the clone here
|
|
|
|
state.selection.clone(),
|
2020-06-07 19:15:39 +04:00
|
|
|
Direction::Backward,
|
|
|
|
Granularity::Character,
|
|
|
|
count,
|
|
|
|
);
|
2020-06-07 19:31:11 +04:00
|
|
|
state.selection = selection;
|
2020-06-07 19:15:39 +04:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:31:11 +04:00
|
|
|
pub fn move_char_right(state: &mut State, count: usize) {
|
2020-06-07 19:15:39 +04:00
|
|
|
// TODO: use a transaction
|
|
|
|
state.selection = state.move_selection(
|
2020-06-07 19:31:11 +04:00
|
|
|
// TODO: remove the clone here
|
|
|
|
state.selection.clone(),
|
2020-06-07 19:15:39 +04:00
|
|
|
Direction::Forward,
|
|
|
|
Granularity::Character,
|
|
|
|
count,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:31:11 +04:00
|
|
|
pub fn move_line_up(state: &mut State, count: usize) {
|
2020-06-07 19:15:39 +04:00
|
|
|
// TODO: use a transaction
|
|
|
|
state.selection = state.move_selection(
|
2020-06-07 19:31:11 +04:00
|
|
|
// TODO: remove the clone here
|
|
|
|
state.selection.clone(),
|
2020-06-07 19:15:39 +04:00
|
|
|
Direction::Backward,
|
|
|
|
Granularity::Line,
|
|
|
|
count,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:31:11 +04:00
|
|
|
pub fn move_line_down(state: &mut State, count: usize) {
|
2020-06-07 19:15:39 +04:00
|
|
|
// TODO: use a transaction
|
|
|
|
state.selection = state.move_selection(
|
2020-06-07 19:31:11 +04:00
|
|
|
// TODO: remove the clone here
|
|
|
|
state.selection.clone(),
|
2020-06-07 19:15:39 +04:00
|
|
|
Direction::Forward,
|
|
|
|
Granularity::Line,
|
|
|
|
count,
|
|
|
|
);
|
|
|
|
}
|
2020-09-05 17:01:05 +04:00
|
|
|
|
|
|
|
pub fn insert_mode(state: &mut State, _count: usize) {
|
|
|
|
state.mode = Mode::Insert;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn normal_mode(state: &mut State, _count: usize) {
|
|
|
|
state.mode = Mode::Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: insert means add text just before cursor, on exit we should be on the last letter.
|
|
|
|
pub fn insert(state: &mut State, c: char) {
|
|
|
|
// TODO: needs to work with multiple cursors
|
|
|
|
use crate::transaction::ChangeSet;
|
|
|
|
|
|
|
|
let pos = state.selection.primary().head;
|
|
|
|
let changes = ChangeSet::insert(&state.doc, pos, c);
|
|
|
|
// TODO: need to store history
|
2020-09-07 06:28:52 +04:00
|
|
|
changes.apply(&mut state.doc);
|
2020-09-05 17:01:05 +04:00
|
|
|
state.selection = state.selection.clone().map(&changes);
|
|
|
|
}
|