on master help sign in
Move insert mode commands to a separate namespace.
4996f1bBlaž Hrastnik
helix-term/src/editor.rs+1−1
@@ -330,7 +330,7 @@ impl Editor {
330 ..
330 ..
331 } = event
331 } = event
332 {
332 {
333− commands::insert_char(view, c);
333+ commands::insert::insert_char(view, c);
334 }
334 }
335 view.ensure_cursor_in_view();
335 view.ensure_cursor_in_view();
336
336
helix-view/src/commands.rs+38−38
@@ -368,7 +368,6 @@ pub fn open_below(view: &mut View, _count: usize) {
368 let transaction = Transaction::change(&view.state, changes).with_selection(selection);
368 let transaction = Transaction::change(&view.state, changes).with_selection(selection);
369
369
370 transaction.apply(&mut view.state);
370 transaction.apply(&mut view.state);
371− // TODO: need to store into history if successful
372}
371}
373
372
374// O inserts a new line before each line with a selection
373// O inserts a new line before each line with a selection
@@ -420,48 +419,49 @@ pub fn goto_mode(view: &mut View, _count: usize) {
420 view.state.mode = Mode::Goto;
419 view.state.mode = Mode::Goto;
421}
420}
422
421
422+// NOTE: Transactions in this module get appended to history when we switch back to normal mode.
423+pub mod insert {
424+ use super::*;
423−// TODO: insert means add text just before cursor, on exit we should be on the last letter.
425+ // TODO: insert means add text just before cursor, on exit we should be on the last letter.
424−pub fn insert_char(view: &mut View, c: char) {
426+ pub fn insert_char(view: &mut View, c: char) {
425− let c = Tendril::from_char(c);
427+ let c = Tendril::from_char(c);
426− let transaction = Transaction::insert(&view.state, c);
428+ let transaction = Transaction::insert(&view.state, c);
427
429
428− transaction.apply(&mut view.state);
430+ transaction.apply(&mut view.state);
429− // TODO: need to store into history if successful
430−}
431+ }
431
432
432−pub fn insert_tab(view: &mut View, _count: usize) {
433+ pub fn insert_tab(view: &mut View, _count: usize) {
433− insert_char(view, '\t');
434+ insert_char(view, '\t');
434−}
435+ }
435
436
436−pub fn insert_newline(view: &mut View, _count: usize) {
437+ pub fn insert_newline(view: &mut View, _count: usize) {
437− insert_char(view, '\n');
438+ insert_char(view, '\n');
438−}
439+ }
439
440
440−// TODO: handle indent-aware delete
441+ // TODO: handle indent-aware delete
441−pub fn delete_char_backward(view: &mut View, count: usize) {
442+ pub fn delete_char_backward(view: &mut View, count: usize) {
442− let text = &view.state.doc.slice(..);
443+ let text = &view.state.doc.slice(..);
443− let transaction = Transaction::change_by_selection(&view.state, |range| {
444+ let transaction = Transaction::change_by_selection(&view.state, |range| {
444− (
445+ (
445− graphemes::nth_prev_grapheme_boundary(text, range.head, count),
446+ graphemes::nth_prev_grapheme_boundary(text, range.head, count),
446− range.head,
447+ range.head,
447− None,
448+ None,
448− )
449+ )
449− });
450+ });
450− transaction.apply(&mut view.state);
451+ transaction.apply(&mut view.state);
451− // TODO: need to store into history if successful
452−}
452+ }
453
453
454−pub fn delete_char_forward(view: &mut View, count: usize) {
454+ pub fn delete_char_forward(view: &mut View, count: usize) {
455− let text = &view.state.doc.slice(..);
455+ let text = &view.state.doc.slice(..);
456− let transaction = Transaction::change_by_selection(&view.state, |range| {
456+ let transaction = Transaction::change_by_selection(&view.state, |range| {
457− (
457+ (
458− range.head,
458+ range.head,
459− graphemes::nth_next_grapheme_boundary(text, range.head, count),
459+ graphemes::nth_next_grapheme_boundary(text, range.head, count),
460− None,
460+ None,
461− )
461+ )
462− });
462+ });
463− transaction.apply(&mut view.state);
463+ transaction.apply(&mut view.state);
464− // TODO: need to store into history if successful
464+ }
465}
465}
466
466
467// Undo / Redo
467// Undo / Redo
helix-view/src/keymap.rs+4−4
@@ -186,19 +186,19 @@ pub fn default() -> Keymaps {
186 vec![Key {
186 vec![Key {
187 code: KeyCode::Backspace,
187 code: KeyCode::Backspace,
188 modifiers: Modifiers::NONE
188 modifiers: Modifiers::NONE
189− }] => commands::delete_char_backward,
189+ }] => commands::insert::delete_char_backward,
190 vec![Key {
190 vec![Key {
191 code: KeyCode::Delete,
191 code: KeyCode::Delete,
192 modifiers: Modifiers::NONE
192 modifiers: Modifiers::NONE
193− }] => commands::delete_char_forward,
193+ }] => commands::insert::delete_char_forward,
194 vec![Key {
194 vec![Key {
195 code: KeyCode::Enter,
195 code: KeyCode::Enter,
196 modifiers: Modifiers::NONE
196 modifiers: Modifiers::NONE
197− }] => commands::insert_newline,
197+ }] => commands::insert::insert_newline,
198 vec![Key {
198 vec![Key {
199 code: KeyCode::Tab,
199 code: KeyCode::Tab,
200 modifiers: Modifiers::NONE
200 modifiers: Modifiers::NONE
201− }] => commands::insert_tab,
201+ }] => commands::insert::insert_tab,
202 ),
202 ),
203 state::Mode::Goto => hashmap!(
203 state::Mode::Goto => hashmap!(
204 vec![Key {
204 vec![Key {