Move insert mode commands to a separate namespace.

This commit is contained in:
Blaž Hrastnik 2020-10-14 12:09:55 +09:00
parent 77ff51cef9
commit 4996f1b4d3
3 changed files with 43 additions and 43 deletions

View file

@ -330,7 +330,7 @@ impl Editor {
.. ..
} = event } = event
{ {
commands::insert_char(view, c); commands::insert::insert_char(view, c);
} }
view.ensure_cursor_in_view(); view.ensure_cursor_in_view();

View file

@ -368,7 +368,6 @@ pub fn open_below(view: &mut View, _count: usize) {
let transaction = Transaction::change(&view.state, changes).with_selection(selection); let transaction = Transaction::change(&view.state, changes).with_selection(selection);
transaction.apply(&mut view.state); transaction.apply(&mut view.state);
// TODO: need to store into history if successful
} }
// O inserts a new line before each line with a selection // O inserts a new line before each line with a selection
@ -420,48 +419,49 @@ pub fn goto_mode(view: &mut View, _count: usize) {
view.state.mode = Mode::Goto; view.state.mode = Mode::Goto;
} }
// TODO: insert means add text just before cursor, on exit we should be on the last letter. // NOTE: Transactions in this module get appended to history when we switch back to normal mode.
pub fn insert_char(view: &mut View, c: char) { pub mod insert {
let c = Tendril::from_char(c); use super::*;
let transaction = Transaction::insert(&view.state, c); // TODO: insert means add text just before cursor, on exit we should be on the last letter.
pub fn insert_char(view: &mut View, c: char) {
let c = Tendril::from_char(c);
let transaction = Transaction::insert(&view.state, c);
transaction.apply(&mut view.state); transaction.apply(&mut view.state);
// TODO: need to store into history if successful }
}
pub fn insert_tab(view: &mut View, _count: usize) { pub fn insert_tab(view: &mut View, _count: usize) {
insert_char(view, '\t'); insert_char(view, '\t');
} }
pub fn insert_newline(view: &mut View, _count: usize) { pub fn insert_newline(view: &mut View, _count: usize) {
insert_char(view, '\n'); insert_char(view, '\n');
} }
// TODO: handle indent-aware delete // TODO: handle indent-aware delete
pub fn delete_char_backward(view: &mut View, count: usize) { pub fn delete_char_backward(view: &mut View, count: usize) {
let text = &view.state.doc.slice(..); let text = &view.state.doc.slice(..);
let transaction = Transaction::change_by_selection(&view.state, |range| { let transaction = Transaction::change_by_selection(&view.state, |range| {
( (
graphemes::nth_prev_grapheme_boundary(text, range.head, count), graphemes::nth_prev_grapheme_boundary(text, range.head, count),
range.head, range.head,
None, None,
) )
}); });
transaction.apply(&mut view.state); transaction.apply(&mut view.state);
// TODO: need to store into history if successful }
}
pub fn delete_char_forward(view: &mut View, count: usize) { pub fn delete_char_forward(view: &mut View, count: usize) {
let text = &view.state.doc.slice(..); let text = &view.state.doc.slice(..);
let transaction = Transaction::change_by_selection(&view.state, |range| { let transaction = Transaction::change_by_selection(&view.state, |range| {
( (
range.head, range.head,
graphemes::nth_next_grapheme_boundary(text, range.head, count), graphemes::nth_next_grapheme_boundary(text, range.head, count),
None, None,
) )
}); });
transaction.apply(&mut view.state); transaction.apply(&mut view.state);
// TODO: need to store into history if successful }
} }
// Undo / Redo // Undo / Redo

View file

@ -186,19 +186,19 @@ pub fn default() -> Keymaps {
vec![Key { vec![Key {
code: KeyCode::Backspace, code: KeyCode::Backspace,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::delete_char_backward, }] => commands::insert::delete_char_backward,
vec![Key { vec![Key {
code: KeyCode::Delete, code: KeyCode::Delete,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::delete_char_forward, }] => commands::insert::delete_char_forward,
vec![Key { vec![Key {
code: KeyCode::Enter, code: KeyCode::Enter,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::insert_newline, }] => commands::insert::insert_newline,
vec![Key { vec![Key {
code: KeyCode::Tab, code: KeyCode::Tab,
modifiers: Modifiers::NONE modifiers: Modifiers::NONE
}] => commands::insert_tab, }] => commands::insert::insert_tab,
), ),
state::Mode::Goto => hashmap!( state::Mode::Goto => hashmap!(
vec![Key { vec![Key {