2020-09-13 04:32:37 +02:00
|
|
|
use crate::graphemes;
|
2020-09-13 12:51:42 +02:00
|
|
|
use crate::selection::{Range, Selection};
|
2020-09-05 15:01:05 +02:00
|
|
|
use crate::state::{Direction, Granularity, Mode, State};
|
2020-09-07 10:08:28 +02:00
|
|
|
use crate::transaction::{ChangeSet, Transaction};
|
|
|
|
use crate::Tendril;
|
2020-06-07 17:15:39 +02: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 17:31:11 +02:00
|
|
|
pub type Command = fn(state: &mut State, count: usize);
|
2020-06-07 17:15:39 +02:00
|
|
|
|
2020-06-07 17:31:11 +02:00
|
|
|
pub fn move_char_left(state: &mut State, count: usize) {
|
2020-06-07 17:15:39 +02:00
|
|
|
// TODO: use a transaction
|
2020-09-12 16:52:38 +02:00
|
|
|
let selection = state.move_selection(Direction::Backward, Granularity::Character, count);
|
2020-06-07 17:31:11 +02:00
|
|
|
state.selection = selection;
|
2020-06-07 17:15:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 17:31:11 +02:00
|
|
|
pub fn move_char_right(state: &mut State, count: usize) {
|
2020-06-07 17:15:39 +02:00
|
|
|
// TODO: use a transaction
|
2020-09-12 16:52:38 +02:00
|
|
|
state.selection = state.move_selection(Direction::Forward, Granularity::Character, count);
|
2020-06-07 17:15:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 17:31:11 +02:00
|
|
|
pub fn move_line_up(state: &mut State, count: usize) {
|
2020-06-07 17:15:39 +02:00
|
|
|
// TODO: use a transaction
|
2020-09-12 16:52:38 +02:00
|
|
|
state.selection = state.move_selection(Direction::Backward, Granularity::Line, count);
|
2020-06-07 17:15:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 17:31:11 +02:00
|
|
|
pub fn move_line_down(state: &mut State, count: usize) {
|
2020-06-07 17:15:39 +02:00
|
|
|
// TODO: use a transaction
|
2020-09-12 16:52:38 +02:00
|
|
|
state.selection = state.move_selection(Direction::Forward, Granularity::Line, count);
|
2020-06-07 17:15:39 +02:00
|
|
|
}
|
2020-09-05 15:01:05 +02:00
|
|
|
|
2020-09-07 10:08:28 +02:00
|
|
|
// avoid select by default by having a visual mode switch that makes movements into selects
|
|
|
|
|
|
|
|
// insert mode:
|
|
|
|
// first we calculate the correct cursors/selections
|
|
|
|
// then we just append at each cursor
|
|
|
|
// lastly, if it was append mode we shift cursor by 1?
|
|
|
|
|
|
|
|
// inserts at the start of each selection
|
2020-09-05 15:01:05 +02:00
|
|
|
pub fn insert_mode(state: &mut State, _count: usize) {
|
|
|
|
state.mode = Mode::Insert;
|
2020-09-07 10:08:28 +02:00
|
|
|
|
|
|
|
state.selection = state
|
|
|
|
.selection
|
|
|
|
.transform(|range| Range::new(range.to(), range.from()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// inserts at the end of each selection
|
|
|
|
pub fn append_mode(state: &mut State, _count: usize) {
|
|
|
|
state.mode = Mode::Insert;
|
|
|
|
|
|
|
|
// TODO: as transaction
|
2020-09-07 10:17:14 +02:00
|
|
|
let text = &state.doc.slice(..);
|
2020-09-12 16:52:38 +02:00
|
|
|
state.selection = state.selection.transform(|range| {
|
2020-09-07 10:08:28 +02:00
|
|
|
// TODO: to() + next char
|
2020-09-13 04:32:37 +02:00
|
|
|
Range::new(
|
|
|
|
range.from(),
|
|
|
|
graphemes::next_grapheme_boundary(text, range.to()),
|
|
|
|
)
|
2020-09-07 10:08:28 +02:00
|
|
|
})
|
2020-09-05 15:01:05 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 13:04:16 +02:00
|
|
|
// TODO: I, A, o and O can share a lot of the primitives.
|
|
|
|
|
2020-09-07 10:08:28 +02:00
|
|
|
// I inserts at the start of each line with a selection
|
2020-09-13 13:04:16 +02:00
|
|
|
pub fn prepend_to_line(state: &mut State, _count: usize) {
|
|
|
|
state.mode = Mode::Insert;
|
|
|
|
|
|
|
|
// calculate line numbers for each selection range
|
|
|
|
let mut lines = state
|
|
|
|
.selection
|
|
|
|
.ranges()
|
|
|
|
.iter()
|
|
|
|
.map(|range| state.doc.char_to_line(range.head))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
lines.sort();
|
|
|
|
lines.dedup();
|
|
|
|
|
|
|
|
let positions: Vec<_> = lines
|
|
|
|
.into_iter()
|
|
|
|
.map(|index| {
|
2020-09-13 13:06:53 +02:00
|
|
|
// adjust all positions to the start of the line.
|
|
|
|
state.doc.line_to_char(index)
|
2020-09-13 13:04:16 +02:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let selection = Selection::new(
|
|
|
|
positions
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.map(|pos| Range::new(pos, pos))
|
|
|
|
.collect(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
let transaction = Transaction::new(state).with_selection(selection);
|
|
|
|
|
|
|
|
transaction.apply(state);
|
|
|
|
// TODO: need to store into history if successful
|
|
|
|
}
|
|
|
|
|
2020-09-07 10:08:28 +02:00
|
|
|
// A inserts at the end of each line with a selection
|
2020-09-13 13:04:16 +02:00
|
|
|
pub fn append_to_line(state: &mut State, _count: usize) {
|
|
|
|
state.mode = Mode::Insert;
|
|
|
|
|
|
|
|
// calculate line numbers for each selection range
|
|
|
|
let mut lines = state
|
|
|
|
.selection
|
|
|
|
.ranges()
|
|
|
|
.iter()
|
|
|
|
.map(|range| state.doc.char_to_line(range.head))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
lines.sort();
|
|
|
|
lines.dedup();
|
|
|
|
|
|
|
|
let positions: Vec<_> = lines
|
|
|
|
.into_iter()
|
|
|
|
.map(|index| {
|
|
|
|
// adjust all positions to the end of the line.
|
|
|
|
let line = state.doc.line(index);
|
|
|
|
let line_start = state.doc.line_to_char(index);
|
|
|
|
line_start + line.len_chars() - 1
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let selection = Selection::new(
|
|
|
|
positions
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.map(|pos| Range::new(pos, pos))
|
|
|
|
.collect(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
let transaction = Transaction::new(state).with_selection(selection);
|
|
|
|
|
|
|
|
transaction.apply(state);
|
|
|
|
// TODO: need to store into history if successful
|
|
|
|
}
|
2020-09-13 12:51:42 +02:00
|
|
|
|
|
|
|
// o inserts a new line after each line with a selection
|
|
|
|
pub fn open_below(state: &mut State, _count: usize) {
|
|
|
|
state.mode = Mode::Insert;
|
|
|
|
|
|
|
|
// calculate line numbers for each selection range
|
|
|
|
let mut lines = state
|
|
|
|
.selection
|
|
|
|
.ranges()
|
|
|
|
.iter()
|
|
|
|
.map(|range| state.doc.char_to_line(range.head))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
lines.sort();
|
|
|
|
lines.dedup();
|
|
|
|
|
|
|
|
let positions: Vec<_> = lines
|
|
|
|
.into_iter()
|
|
|
|
.map(|index| {
|
|
|
|
// adjust all positions to the end of the line.
|
|
|
|
let line = state.doc.line(index);
|
|
|
|
let line_start = state.doc.line_to_char(index);
|
|
|
|
line_start + line.len_chars()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let changes = positions.iter().copied().map(|index|
|
|
|
|
// generate changes
|
|
|
|
(index, index, Some(Tendril::from_char('\n'))));
|
|
|
|
|
|
|
|
// TODO: count actually inserts "n" new lines and starts editing on all of them.
|
|
|
|
// TODO: append "count" newlines and modify cursors to those lines
|
|
|
|
|
|
|
|
let selection = Selection::new(
|
|
|
|
positions
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.map(|pos| Range::new(pos, pos))
|
|
|
|
.collect(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
let transaction = Transaction::change(state, changes.collect()).with_selection(selection);
|
|
|
|
|
|
|
|
transaction.apply(state);
|
|
|
|
// TODO: need to store into history if successful
|
|
|
|
}
|
|
|
|
|
|
|
|
// O inserts a new line before each line with a selection
|
2020-09-07 10:08:28 +02:00
|
|
|
|
2020-09-05 15:01:05 +02:00
|
|
|
pub fn normal_mode(state: &mut State, _count: usize) {
|
2020-09-07 10:17:14 +02:00
|
|
|
// TODO: if leaving append mode, move cursor back by 1
|
2020-09-05 15:01:05 +02:00
|
|
|
state.mode = Mode::Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: insert means add text just before cursor, on exit we should be on the last letter.
|
2020-09-07 10:08:28 +02:00
|
|
|
pub fn insert_char(state: &mut State, c: char) {
|
|
|
|
let c = Tendril::from_char(c);
|
|
|
|
let transaction = Transaction::insert(&state, c);
|
2020-09-05 15:01:05 +02:00
|
|
|
|
2020-09-07 10:08:28 +02:00
|
|
|
transaction.apply(state);
|
|
|
|
// TODO: need to store into history if successful
|
2020-09-05 15:01:05 +02:00
|
|
|
}
|
2020-09-13 04:32:37 +02:00
|
|
|
|
|
|
|
// TODO: handle indent-aware delete
|
|
|
|
pub fn delete_char_backward(state: &mut State, count: usize) {
|
|
|
|
let text = &state.doc.slice(..);
|
|
|
|
let transaction = Transaction::change_by_selection(state, |range| {
|
|
|
|
(
|
|
|
|
graphemes::nth_prev_grapheme_boundary(text, range.head, count),
|
|
|
|
range.head,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
transaction.apply(state);
|
|
|
|
// TODO: need to store into history if successful
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_char_forward(state: &mut State, count: usize) {
|
|
|
|
let text = &state.doc.slice(..);
|
|
|
|
let transaction = Transaction::change_by_selection(state, |range| {
|
|
|
|
(
|
|
|
|
graphemes::nth_next_grapheme_boundary(text, range.head, count),
|
|
|
|
range.head,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
transaction.apply(state);
|
|
|
|
// TODO: need to store into history if successful
|
|
|
|
}
|