From d7d7b7cd2bc308ae550b5b467c1fb57e7f851c6b Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sun, 16 Feb 2020 18:09:49 +0900 Subject: [PATCH] More bug fixes from the refactor. --- src/editor/mod.rs | 58 +++++++++++++++++++++-------------------------- src/formatter.rs | 31 +++++++++++-------------- 2 files changed, 39 insertions(+), 50 deletions(-) diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 9ce3e00..c2a6580 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -299,33 +299,27 @@ impl Editor { /// Moves the editor's view the minimum amount to show the cursor pub fn move_view_to_cursor(&mut self) { - // TODO: account for the horizontal offset of the editor view. + // // 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); - // TODO: handle multiple cursors properly. Should only move if - // there are no cursors currently in view, and should jump to - // the closest cursor. - - // 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) { @@ -551,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); @@ -581,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 a598402..536fb44 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -158,24 +158,17 @@ impl LineFormatter { // and handle appropriately. if offset_char_v_pos < 0 { // If we're off the start of the block. + char_idx = char_idx.saturating_sub(char_offset + 1); + v_offset += char_v_pos as isize + 1; if char_idx == 0 { - // We reached the start of the whole buffer. break; - } else { - // Set our variables appropriately for the next iteration. - char_idx -= char_offset + 1; - v_offset += char_v_pos as isize + 1; } } else if offset_char_v_pos >= block_v_dim as isize { // If we're off the end of the block. - if char_idx >= buf.text.len_chars() { - // We reached the end of the whole buffer. - char_idx = buf.text.len_chars(); + char_idx = (char_idx + block.len_chars() - char_offset).min(buf.text.len_chars()); + v_offset -= block_v_dim as isize - char_v_pos as isize; + if char_idx == buf.text.len_chars() { break; - } else { - // Set our variables appropriately for the next iteration. - char_idx += block.len_chars() - char_offset; - v_offset -= block_v_dim as isize - char_v_pos as isize; } } else { // If the vertical offset is within this block, calculate an @@ -187,7 +180,8 @@ impl LineFormatter { } i += char_count(&g); } - char_idx += block.len_chars() - char_offset + i; + char_idx -= char_offset; + char_idx += i; v_offset = 0; } } @@ -233,12 +227,13 @@ impl LineFormatter { buf: &'b Buffer, char_idx: usize, ) -> (RopeSlice<'b>, BlockVisIter<'b>, usize) { - let (line_i, col_i) = buf.index_to_line_col(char_idx); - let line = buf.get_line(line_i); + let line_i = buf.text.char_to_line(char_idx); + let line_start = buf.text.line_to_char(line_i); + let line_end = buf.text.line_to_char(line_i + 1); + let line = buf.text.slice(line_start..line_end); // Find the right block in the line, and the index within that block - let (block_index, block_range) = block_index_and_range(&line, col_i); - let col_i_adjusted = col_i - block_range.0; + let (block_index, block_range) = block_index_and_range(&line, char_idx - line_start); // Get the right block and an iter into it. let block = line.slice(block_range.0..block_range.1); @@ -258,7 +253,7 @@ impl LineFormatter { self.wrap_extra_indent, ); - (block, vis_iter, col_i_adjusted) + (block, vis_iter, char_idx - (line_start + block_range.0)) } }