Skip to content

Commit

Permalink
feat: adding scope aware newline above
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 21, 2024
1 parent 0df8461 commit a64a2ec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
27 changes: 16 additions & 11 deletions reqtui/src/text_object/text_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,23 +356,17 @@ impl TextObject<Write> {
}

pub fn insert_line_below(&mut self, cursor: &Cursor, tree: Option<&Tree>) {
let indentation = if let Some(tree) = tree {
let line_byte_idx = self.content.line_to_byte(cursor.row());
let cursor_byte_idx = line_byte_idx.add(cursor.col());
let indentation_level = Highlighter::find_indentation_level(tree, cursor_byte_idx);
tracing::debug!("{indentation_level}");
" ".repeat(indentation_level)
} else {
String::new()
};
let indentation = self.get_scope_aware_indentation(cursor, tree);
let next_line = self.content.line_to_char(cursor.row().add(1));
let line_with_indentation = format!("{}{}", indentation, &self.line_break.to_string());
self.content.insert(next_line, &line_with_indentation);
}

pub fn insert_line_above(&mut self, cursor: &Cursor) {
pub fn insert_line_above(&mut self, cursor: &Cursor, tree: Option<&Tree>) {
let indentation = self.get_scope_aware_indentation(cursor, tree);
let curr_line = self.content.line_to_char(cursor.row());
self.content.insert(curr_line, &self.line_break.to_string());
let line_with_indentation = format!("{}{}", indentation, &self.line_break.to_string());
self.content.insert(curr_line, &line_with_indentation);
}

pub fn find_oposing_token(&mut self, cursor: &Cursor) -> (usize, usize) {
Expand Down Expand Up @@ -456,6 +450,17 @@ impl TextObject<Write> {
(curr_col, curr_row)
}
}

fn get_scope_aware_indentation(&self, cursor: &Cursor, tree: Option<&Tree>) -> String {
if let Some(tree) = tree {
let line_byte_idx = self.content.line_to_byte(cursor.row());
let cursor_byte_idx = line_byte_idx.add(cursor.col());
let indentation_level = Highlighter::find_indentation_level(tree, cursor_byte_idx);
" ".repeat(indentation_level)
} else {
String::new()
}
}
}

impl<State> std::fmt::Display for TextObject<State> {
Expand Down
3 changes: 2 additions & 1 deletion tui/src/components/api_explorer/req_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ impl<'re> ReqEditor<'re> {
}

fn insert_line_above(&mut self) {
self.body.insert_line_above(&self.cursor);
self.body
.insert_line_above(&self.cursor, self.tree.as_ref());
self.maybe_scroll_view();
let line_len = self.body.line_len(self.cursor.row());
self.cursor.maybe_snap_to_col(line_len);
Expand Down

0 comments on commit a64a2ec

Please sign in to comment.