Skip to content

Commit

Permalink
feat: implementing delete line (dd)
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 10, 2024
1 parent cdef434 commit 2292db1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
51 changes: 39 additions & 12 deletions reqtui/src/text_object/text_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ pub enum LineBreak {
Crlf,
}

impl From<LineBreak> for usize {
fn from(value: LineBreak) -> usize {
match value {
LineBreak::Lf => 1,
LineBreak::Crlf => 2,
}
}
}

impl std::fmt::Display for LineBreak {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Lf => f.write_str("\n"),
Self::Crlf => f.write_str("\r\n"),
}
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct Readonly;
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -66,10 +84,8 @@ impl TextObject<Write> {
pub fn insert_newline(&mut self, cursor: &Cursor) {
let line = self.content.line_to_char(cursor.row());
let col_offset = line + cursor.col();
match self.line_break {
LineBreak::Lf => self.content.insert_char(col_offset, '\n'),
LineBreak::Crlf => self.content.insert(col_offset, "\r\n"),
}
self.content
.insert(col_offset, &self.line_break.to_string());
}

pub fn erase_backwards_up_to_line_start(&mut self, cursor: &Cursor) {
Expand Down Expand Up @@ -101,15 +117,20 @@ impl TextObject<Write> {
self.content.line(cursor.row()).as_str()
}

pub fn line_len_with_linebreak(&self, line: usize) -> usize {
self.content
.line(line)
.as_str()
.map(|line| line.len())
.unwrap_or_default()
}

pub fn line_len(&self, line: usize) -> usize {
let mut line_len = 0;
if let Some(line) = self.content.line(line).as_str() {
match self.line_break {
LineBreak::Lf => line_len = line.len().saturating_sub(1),
LineBreak::Crlf => line_len = line.len().saturating_sub(2),
}
}
line_len
self.content
.line(line)
.as_str()
.map(|line| line.len().saturating_sub(self.line_break.clone().into()))
.unwrap_or_default()
}

pub fn erase_until_eol(&mut self, cursor: &Cursor) {
Expand Down Expand Up @@ -194,6 +215,12 @@ impl TextObject<Write> {
pub fn len_lines(&self) -> usize {
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));
self.content.try_remove(start..end).ok();
}
}

impl<State> std::fmt::Display for TextObject<State> {
Expand Down
17 changes: 15 additions & 2 deletions tui/src/components/api_explorer/req_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,15 @@ impl<'re> ReqEditor<'re> {
Action::MoveAfterWhitespaceReverse => self.move_after_whitespace_reverse(),
Action::MoveAfterWhitespace => self.move_after_whitespace(),
Action::DeletePreviousNonWrapping => self.erase_backwards_up_to_line_start(),
Action::MoveToTop => self.move_to_top(),
Action::DeleteLine => self.delete_current_line(),
Action::Undo => todo!(),
Action::FindNext => todo!(),
Action::FindPrevious => todo!(),
Action::PreviousWord => todo!(),
Action::MoveToTop => self.move_to_top(),
Action::PageDown => todo!(),
Action::PageUp => todo!(),
Action::DeleteWord => todo!(),
Action::DeleteLine => todo!(),
Action::DeleteBack => todo!(),
Action::InsertLineBelow => todo!(),
Action::InsertLineAbove => todo!(),
Expand Down Expand Up @@ -355,6 +355,16 @@ impl<'re> ReqEditor<'re> {
self.cursor.move_right(1);
}

fn delete_current_line(&mut self) {
self.body.delete_line(&self.cursor);
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 erase_until_eol(&mut self) {
self.body.erase_until_eol(&self.cursor);
}
Expand Down Expand Up @@ -534,6 +544,9 @@ impl Eventful for ReqEditor<'_> {
_ => self.keymap_buffer = None,
}

self.tree = HIGHLIGHTER.write().unwrap().parse(&self.body.to_string());
self.styled_display =
build_styled_content(&self.body.to_string(), self.tree.as_ref(), self.colors);
return Ok(None);
}

Expand Down

0 comments on commit 2292db1

Please sign in to comment.