From 4979f587049c911c39c03c4cc4cee41128c17853 Mon Sep 17 00:00:00 2001 From: Taehoon Moon Date: Sun, 1 Dec 2024 17:08:56 +0900 Subject: [PATCH] Add dj & dl support - {n1}d{n2}{h | l} --- .../inner_state/editing_normal_mode.rs | 16 ++++++++++++ core/src/transition.rs | 1 + tui/src/transitions.rs | 26 ++++++++++++++----- tui/src/views/dialog/vim_keymap.rs | 4 +++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/core/src/state/notebook/inner_state/editing_normal_mode.rs b/core/src/state/notebook/inner_state/editing_normal_mode.rs index 4955623..5b6d69e 100644 --- a/core/src/state/notebook/inner_state/editing_normal_mode.rs +++ b/core/src/state/notebook/inner_state/editing_normal_mode.rs @@ -419,6 +419,14 @@ async fn consume_delete( state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle); DeleteWordEnd(n).into() } + Key(KeyEvent::H) => { + state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle); + DeleteCharsBack(n).into() + } + Key(KeyEvent::L) => { + state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle); + DeleteChars(n).into() + } Key(KeyEvent::DollarSign) => { state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle); @@ -473,6 +481,14 @@ async fn consume_delete2( DeleteInsideMode.into() } + Key(KeyEvent::H) => { + state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle); + DeleteCharsBack(n1 * n2).into() + } + Key(KeyEvent::L) => { + state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle); + DeleteChars(n1 * n2).into() + } Key(KeyEvent::Esc) => { state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle); diff --git a/core/src/transition.rs b/core/src/transition.rs index a5a502e..219392b 100644 --- a/core/src/transition.rs +++ b/core/src/transition.rs @@ -129,6 +129,7 @@ pub enum NormalModeTransition { InsertNewLineBelow, InsertNewLineAbove, DeleteChars(usize), + DeleteCharsBack(usize), DeleteLines(usize), DeleteLinesAndInsert(usize), DeleteWordEnd(usize), diff --git a/tui/src/transitions.rs b/tui/src/transitions.rs index b4d9792..f0fc09c 100644 --- a/tui/src/transitions.rs +++ b/tui/src/transitions.rs @@ -240,12 +240,7 @@ impl App { } MoveCursorBack(n) => { let editor = self.context.notebook.get_editor_mut(); - let (row, col) = editor.cursor(); - let cursor_move = if col < n { - CursorMove::Head - } else { - CursorMove::Jump(row as u16, (col - n) as u16) - }; + let cursor_move = cursor_move_back(editor, n); editor.move_cursor(cursor_move); } @@ -340,6 +335,16 @@ impl App { self.context.notebook.mark_dirty(); self.context.notebook.update_yank(); } + DeleteCharsBack(n) => { + let editor = self.context.notebook.get_editor_mut(); + editor.start_selection(); + let cursor_move = cursor_move_back(editor, n); + + editor.move_cursor(cursor_move); + editor.cut(); + self.context.notebook.mark_dirty(); + self.context.notebook.update_yank(); + } Paste => { let line_yanked = self.context.notebook.line_yanked; let editor = self.context.notebook.get_editor_mut(); @@ -665,6 +670,15 @@ fn cursor_move_forward(editor: &TextArea, n: usize) -> CursorMove { } } +fn cursor_move_back(editor: &TextArea, n: usize) -> CursorMove { + let (row, col) = editor.cursor(); + if col < n { + CursorMove::Head + } else { + CursorMove::Jump(row as u16, (col - n) as u16) + } +} + fn cursor_move_down(editor: &TextArea, n: usize) -> CursorMove { let num_lines = editor.lines().len(); let (row, col) = editor.cursor(); diff --git a/tui/src/views/dialog/vim_keymap.rs b/tui/src/views/dialog/vim_keymap.rs index e04602d..cca3b85 100644 --- a/tui/src/views/dialog/vim_keymap.rs +++ b/tui/src/views/dialog/vim_keymap.rs @@ -91,6 +91,8 @@ pub fn draw(frame: &mut Frame, keymap_kind: VimKeymapKind) { Line::raw("[b] Delete the word before the cursor."), Line::raw("[0] Delete to the beginning of the line"), Line::raw("[$] Delete to the end of the line, repeated by the specified number"), + Line::raw("[h] Delete the specified number of characters to the left"), + Line::raw("[l] Delete the specified number of characters to the right"), ]), VimKeymapKind::NormalDelete2 => ("VIM NORMAL MODE KEYMAP - DELETE NUMBERING", vec![ Line::from("EXTENDING NUMBERING MODE".white().on_dark_gray()), @@ -101,6 +103,8 @@ pub fn draw(frame: &mut Frame, keymap_kind: VimKeymapKind) { Line::raw(""), Line::from("DELETE TEXT".white().on_dark_gray()), Line::raw("[d] Delete the specified number of lines"), + Line::raw("[h] Delete the specified number of characters to the left"), + Line::raw("[l] Delete the specified number of characters to the right"), ]), VimKeymapKind::NormalChange => ("VIM NORMAL MODE KEYMAP - CHANGE", vec![ Line::from("TO CHANGE INSIDE MODE".white().on_dark_gray()),