Skip to content

Commit

Permalink
feat: insert line above and below
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 11, 2024
1 parent b82a9e8 commit f3f6922
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 38 deletions.
102 changes: 68 additions & 34 deletions reqtui/src/text_object/text_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,21 @@ impl TextObject<Write> {
pub fn find_char_after_whitespace(&self, cursor: &Cursor) -> (usize, usize) {
let line = self.content.line_to_char(cursor.row());
let col_offset = line + cursor.col();
let mut walked = 0;
let mut end_idx = 0;
let mut found = false;

for char in self.content.chars_at(col_offset) {
match (char, found) {
(c, false) if c.is_whitespace() => {
found = true;
walked = walked.add(1);
end_idx = end_idx.add(1);
}
(c, true) if !c.is_whitespace() => break,
_ => walked = walked.add(1),
_ => end_idx = end_idx.add(1),
}
}
let curr_idx = col_offset.add(walked);
let curr_row = self.content.char_to_line(col_offset.add(walked));
let curr_idx = col_offset.add(end_idx);
let curr_row = self.content.char_to_line(col_offset.add(end_idx));
let curr_row_start = self.content.line_to_char(curr_row);
let curr_col = curr_idx.sub(curr_row_start);
(curr_col, curr_row)
Expand Down Expand Up @@ -189,26 +189,67 @@ impl TextObject<Write> {
}

pub fn find_char_after_separator(&self, cursor: &Cursor) -> (usize, usize) {
let line = self.content.line_to_char(cursor.row());
let col_offset = line + cursor.col();
let mut walked = 0;
let mut found = false;
let start_idx = self.content.line_to_char(cursor.row()).add(cursor.col());
let mut end_idx = 0;
let mut found_newline = false;

for char in self.content.chars_at(col_offset) {
match (char, found) {
(c, false) if !c.is_alphanumeric() => {
found = true;
walked = walked.add(1);
if let Some(initial_char) = self.content.get_char(start_idx) {
for char in self.content.chars_at(start_idx) {
match (
initial_char.is_alphanumeric(),
char.is_alphanumeric(),
found_newline,
) {
(_, _, true) if !char.is_whitespace() => break,
(false, true, _) => break,
(true, false, _) => break,
_ if char.is_whitespace() => {
found_newline = true;
end_idx = end_idx.add(1);
}
_ => end_idx = end_idx.add(1),
}
(c, true) if c.is_alphanumeric() => break,
_ => walked = walked.add(1),
}
}

let curr_idx = col_offset.add(walked);
let curr_row = self.content.char_to_line(col_offset.add(walked));
let curr_idx = start_idx.add(end_idx);
let curr_row = self.content.char_to_line(curr_idx);
let curr_row_start = self.content.line_to_char(curr_row);
let curr_col = curr_idx.sub(curr_row_start);

(curr_col, curr_row)
}

pub fn find_char_before_separator(&self, cursor: &Cursor) -> (usize, usize) {
let start_idx = self.content.line_to_char(cursor.row()).add(cursor.col());
let mut end_idx = start_idx;
let mut found_newline = false;

if let Some(initial_char) = self.content.get_char(start_idx) {
for _ in (0..start_idx.saturating_sub(1)).rev() {
let char = self.content.char(end_idx);

match (
initial_char.is_alphanumeric(),
char.is_alphanumeric(),
found_newline,
) {
(_, _, true) if !self.line_break.to_string().contains(char) => break,
(false, true, _) => break,
(true, false, _) => break,
_ if self.line_break.to_string().contains(char) => {
found_newline = true;
end_idx = end_idx.saturating_sub(1);
}
_ => end_idx = end_idx.saturating_sub(1),
}
}
};

let curr_row = self.content.char_to_line(end_idx);
let curr_row_start = self.content.line_to_char(curr_row);
let curr_col = end_idx.sub(curr_row_start);

(curr_col, curr_row)
}

Expand Down Expand Up @@ -277,23 +318,16 @@ impl TextObject<Write> {
};

end_idx.sub(start_idx)
}

pub fn insert_line_below(&mut self, cursor: &Cursor) {
let next_line = self.content.line_to_char(cursor.row().add(1));
self.content.insert(next_line, &self.line_break.to_string());
}

// for char in self.content.chars_at(start_idx) {
// match (initial_char.is_alphanumeric(), char.is_alphanumeric()) {
// (false, _) if self.line_break.to_string().contains(char) => break,
// (false, true) => break,
// (true, false) => break,
// _ => end_idx = end_idx.add(1),
// }
//
// self.content.try_remove(start_idx..end_idx).ok();
//
//
// let curr_row = self.content.char_to_line(index);
// let curr_row_start = self.content.line_to_char(curr_row);
// let curr_col = index - curr_row_start;
//
// (curr_col, curr_row)
pub fn insert_line_above(&mut self, cursor: &Cursor) {
let curr_line = self.content.line_to_char(cursor.row());
self.content.insert(curr_line, &self.line_break.to_string());
}
}

Expand Down
33 changes: 29 additions & 4 deletions tui/src/components/api_explorer/req_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ impl<'re> ReqEditor<'re> {
Action::MoveUp => self.move_up(),
Action::MoveRight => self.move_right(),
Action::DeleteCurrentChar => self.erase_current_char(),
Action::NextWord => self.move_to_next_word(),
Action::InsertAhead => self.insert_ahead(),
Action::MoveToBottom => self.move_to_bottom(),
Action::DeleteUntilEOL => self.erase_until_eol(),
Expand All @@ -314,12 +313,13 @@ impl<'re> ReqEditor<'re> {
Action::DeleteBack => self.delete_word_backwards(),
Action::PageDown => self.page_down(),
Action::PageUp => self.page_up(),
Action::NextWord => self.move_to_next_word(),
Action::PreviousWord => self.move_to_prev_word(),
Action::InsertLineBelow => self.insert_line_below(),
Action::InsertLineAbove => self.insert_line_above(),
Action::Undo => todo!(),
Action::FindNext => todo!(),
Action::FindPrevious => todo!(),
Action::PreviousWord => todo!(),
Action::InsertLineBelow => todo!(),
Action::InsertLineAbove => todo!(),
Action::PasteBelow => todo!(),
}
}
Expand Down Expand Up @@ -371,6 +371,21 @@ impl<'re> ReqEditor<'re> {
self.cursor.maybe_snap_to_col(line_len);
}

fn insert_line_below(&mut self) {
self.body.insert_line_below(&self.cursor);
self.cursor.move_down(1);
self.maybe_scroll_view();
let line_len = self.body.line_len(self.cursor.row());
self.cursor.maybe_snap_to_col(line_len);
}

fn insert_line_above(&mut self) {
self.body.insert_line_above(&self.cursor);
self.maybe_scroll_view();
let line_len = self.body.line_len(self.cursor.row());
self.cursor.maybe_snap_to_col(line_len);
}

fn delete_word(&mut self) {
self.body.delete_word(&self.cursor);
}
Expand Down Expand Up @@ -458,6 +473,16 @@ impl<'re> ReqEditor<'re> {
self.cursor.move_to_col(col);
let current_line_len = self.body.line_len(self.cursor.row());
self.cursor.maybe_snap_to_col(current_line_len);
self.maybe_scroll_view();
}

fn move_to_prev_word(&mut self) {
let (col, row) = self.body.find_char_before_separator(&self.cursor);
self.cursor.move_to_row(row);
self.cursor.move_to_col(col);
let current_line_len = self.body.line_len(self.cursor.row());
self.cursor.maybe_snap_to_col(current_line_len);
self.maybe_scroll_view();
}

fn move_after_whitespace(&mut self) {
Expand Down

0 comments on commit f3f6922

Please sign in to comment.