diff --git a/src/editor/mod.rs b/src/editor/mod.rs index c2a6580..7d06dc8 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -299,27 +299,27 @@ impl Editor { /// Moves the editor's view the minimum amount to show the cursor pub fn move_view_to_cursor(&mut self) { - // // Find the first and last char index visible within the editor. - // let c_first = self - // .formatter - // .set_horizontal(&self.buffer, self.view_pos.0, 0); - // let mut c_last = - // self.formatter - // .offset_vertical(&self.buffer, c_first, self.view_dim.0 as isize); - // c_last = self - // .formatter - // .set_horizontal(&self.buffer, c_last, self.view_dim.1); + // Find the first and last char index visible within the editor. + let c_first = self + .formatter + .set_horizontal(&self.buffer, self.view_pos.0, 0); + let mut c_last = + self.formatter + .offset_vertical(&self.buffer, c_first, self.view_dim.0 as isize); + c_last = self + .formatter + .set_horizontal(&self.buffer, c_last, self.view_dim.1); - // // Adjust the view depending on where the cursor is - // if self.cursors[0].range.0 < c_first { - // self.view_pos.0 = self.cursors[0].range.0; - // } else if self.cursors[0].range.0 > c_last { - // self.view_pos.0 = self.formatter.offset_vertical( - // &self.buffer, - // self.cursors[0].range.0, - // -(self.view_dim.0 as isize), - // ); - // } + // Adjust the view depending on where the cursor is + if self.cursors[0].range.0 < c_first { + self.view_pos.0 = self.cursors[0].range.0; + } else if self.cursors[0].range.0 > c_last { + self.view_pos.0 = self.formatter.offset_vertical( + &self.buffer, + self.cursors[0].range.0, + -(self.view_dim.0 as isize), + ); + } } pub fn insert_text_at_cursor(&mut self, text: &str) { @@ -545,9 +545,9 @@ impl Editor { let mut temp_index = self .formatter .offset_vertical(&self.buffer, c.range.0, vmove); - // temp_index = self - // .formatter - // .set_horizontal(&self.buffer, temp_index, c.vis_start); + temp_index = self + .formatter + .set_horizontal(&self.buffer, temp_index, c.vis_start); if !self.buffer.is_grapheme(temp_index) { temp_index = self.buffer.nth_prev_grapheme(temp_index, 1); @@ -575,9 +575,9 @@ impl Editor { let mut temp_index = self .formatter .offset_vertical(&self.buffer, c.range.0, vmove); - // temp_index = self - // .formatter - // .set_horizontal(&self.buffer, temp_index, c.vis_start); + temp_index = self + .formatter + .set_horizontal(&self.buffer, temp_index, c.vis_start); if !self.buffer.is_grapheme(temp_index) { temp_index = self.buffer.nth_prev_grapheme(temp_index, 1); diff --git a/src/formatter.rs b/src/formatter.rs index 536fb44..01de571 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -107,7 +107,7 @@ impl LineFormatter { // wasn't long enough, so return the index of the last grapheme // of the previous line. if i > char_offset { - return last_i; + return char_idx - char_offset + last_i; } // Otherwise reset and keep going. @@ -123,7 +123,7 @@ impl LineFormatter { // Check if we've found the horizontal position _and_ the passed // char_idx on the same line, and return if so. if i >= char_offset && hpos_char_idx != None { - return hpos_char_idx.unwrap(); + return char_idx - char_offset + hpos_char_idx.unwrap(); } last_pos = pos; @@ -132,7 +132,13 @@ impl LineFormatter { } // If we reached the end of the text, return the last char index. - return i; + let end_i = char_idx - char_offset + i; + let end_last_i = char_idx - char_offset + last_i; + if buf.text.len_chars() == end_i { + return end_i; + } else { + return end_last_i; + } } /// Takes a char index and a visual vertical offset, and returns the char