From 7a8eb8bc02c751c3a8c1f7b5b420aa6b03db983b Mon Sep 17 00:00:00 2001 From: Willians Faria Date: Fri, 10 May 2024 19:12:09 -0300 Subject: [PATCH] feat: adding delete line below and above (dj/dk) --- config/src/config.rs | 3 +- config/src/default_config.rs | 2 +- reqtui/src/text_object/text_object.rs | 6 ++-- tui/src/components/api_explorer/req_editor.rs | 31 ++++++++++++++++--- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/config/src/config.rs b/config/src/config.rs index cb378a9..df6106a 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -36,7 +36,8 @@ pub enum Action { MoveAfterWhitespaceReverse, MoveAfterWhitespace, DeletePreviousNonWrapping, - + DeleteCurrAndBelow, + DeleteCurrAndAbove, InsertChar(char), InsertTab, InsertLine, diff --git a/config/src/default_config.rs b/config/src/default_config.rs index dc7d37e..a55543a 100644 --- a/config/src/default_config.rs +++ b/config/src/default_config.rs @@ -21,7 +21,7 @@ pub static DEFAULT_CONFIG: &str = r##" "0" = "MoveToLineStart" "C-d" = "PageDown" "C-u" = "PageUp" -"d" = { "w" = "DeleteWord", "d" = "DeleteLine", "b" = "DeleteBack" } +"d" = { "w" = "DeleteWord", "d" = "DeleteLine", "b" = "DeleteBack", "j" = "DeleteCurrAndBelow", "k" = "DeleteCurrAndAbove", "l" = "DeleteCurrentChar", "h" = "DeletePreviousChar" } "S-D" = "DeleteUntilEOL" "x" = "DeleteCurrentChar" "o" = "InsertLineBelow" diff --git a/reqtui/src/text_object/text_object.rs b/reqtui/src/text_object/text_object.rs index 8400744..fab8afe 100644 --- a/reqtui/src/text_object/text_object.rs +++ b/reqtui/src/text_object/text_object.rs @@ -216,9 +216,9 @@ impl TextObject { self.content.len_lines() } - pub fn delete_line(&mut self, cursor: &Cursor) { - let start = self.content.line_to_char(cursor.row()); - let end = self.content.line_to_char(cursor.row().add(1)); + pub fn delete_line(&mut self, line: usize) { + let start = self.content.line_to_char(line); + let end = self.content.line_to_char(line.add(1)); self.content.try_remove(start..end).ok(); } } diff --git a/tui/src/components/api_explorer/req_editor.rs b/tui/src/components/api_explorer/req_editor.rs index 95851fb..fd5daf3 100644 --- a/tui/src/components/api_explorer/req_editor.rs +++ b/tui/src/components/api_explorer/req_editor.rs @@ -307,6 +307,8 @@ impl<'re> ReqEditor<'re> { Action::DeletePreviousNonWrapping => self.erase_backwards_up_to_line_start(), Action::MoveToTop => self.move_to_top(), Action::DeleteLine => self.delete_current_line(), + Action::DeleteCurrAndBelow => self.delete_curr_line_and_below(), + Action::DeleteCurrAndAbove => self.delete_curr_line_and_above(), Action::Undo => todo!(), Action::FindNext => todo!(), Action::FindPrevious => todo!(), @@ -355,14 +357,35 @@ impl<'re> ReqEditor<'re> { self.cursor.move_right(1); } - fn delete_current_line(&mut self) { - self.body.delete_line(&self.cursor); + fn delete_line(&mut self, line: usize) { + self.body.delete_line(line); let len_lines = self.body.len_lines(); if self.cursor.row().ge(&len_lines.saturating_sub(1)) { self.cursor.move_to_row(len_lines.saturating_sub(1)); } - let line_len = self.body.line_len(self.cursor.row()); - self.cursor.move_to_line_end(line_len); + } + + fn delete_current_line(&mut self) { + self.delete_line(self.cursor.row()); + } + + fn delete_curr_line_and_below(&mut self) { + let last_line = self.body.len_lines().saturating_sub(1); + self.cursor + .row() + .ne(&last_line) + .then(|| self.delete_line(self.cursor.row().add(1))); + self.move_down(); + self.delete_line(self.cursor.row()); + } + + fn delete_curr_line_and_above(&mut self) { + self.cursor + .row() + .ne(&0) + .then(|| self.delete_line(self.cursor.row().sub(1))); + self.move_up(); + self.delete_line(self.cursor.row()); } fn erase_until_eol(&mut self) {