Skip to content

Commit

Permalink
feat: adding delete line below and above (dj/dk)
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 10, 2024
1 parent 2292db1 commit 7a8eb8b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub enum Action {
MoveAfterWhitespaceReverse,
MoveAfterWhitespace,
DeletePreviousNonWrapping,

DeleteCurrAndBelow,
DeleteCurrAndAbove,
InsertChar(char),
InsertTab,
InsertLine,
Expand Down
2 changes: 1 addition & 1 deletion config/src/default_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions reqtui/src/text_object/text_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ impl TextObject<Write> {
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();
}
}
Expand Down
31 changes: 27 additions & 4 deletions tui/src/components/api_explorer/req_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7a8eb8b

Please sign in to comment.