More bug fixes from the refactor.

This commit is contained in:
Nathan Vegdahl 2020-02-16 18:09:49 +09:00
parent e5cac79829
commit d7d7b7cd2b
2 changed files with 39 additions and 50 deletions

View File

@ -299,33 +299,27 @@ impl Editor {
/// Moves the editor's view the minimum amount to show the cursor /// Moves the editor's view the minimum amount to show the cursor
pub fn move_view_to_cursor(&mut self) { 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 // // Adjust the view depending on where the cursor is
// there are no cursors currently in view, and should jump to // if self.cursors[0].range.0 < c_first {
// the closest cursor. // self.view_pos.0 = self.cursors[0].range.0;
// } else if self.cursors[0].range.0 > c_last {
// Find the first and last char index visible within the editor. // self.view_pos.0 = self.formatter.offset_vertical(
let c_first = self // &self.buffer,
.formatter // self.cursors[0].range.0,
.set_horizontal(&self.buffer, self.view_pos.0, 0); // -(self.view_dim.0 as isize),
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),
);
}
} }
pub fn insert_text_at_cursor(&mut self, text: &str) { pub fn insert_text_at_cursor(&mut self, text: &str) {
@ -551,9 +545,9 @@ impl Editor {
let mut temp_index = self let mut temp_index = self
.formatter .formatter
.offset_vertical(&self.buffer, c.range.0, vmove); .offset_vertical(&self.buffer, c.range.0, vmove);
temp_index = self // temp_index = self
.formatter // .formatter
.set_horizontal(&self.buffer, temp_index, c.vis_start); // .set_horizontal(&self.buffer, temp_index, c.vis_start);
if !self.buffer.is_grapheme(temp_index) { if !self.buffer.is_grapheme(temp_index) {
temp_index = self.buffer.nth_prev_grapheme(temp_index, 1); temp_index = self.buffer.nth_prev_grapheme(temp_index, 1);
@ -581,9 +575,9 @@ impl Editor {
let mut temp_index = self let mut temp_index = self
.formatter .formatter
.offset_vertical(&self.buffer, c.range.0, vmove); .offset_vertical(&self.buffer, c.range.0, vmove);
temp_index = self // temp_index = self
.formatter // .formatter
.set_horizontal(&self.buffer, temp_index, c.vis_start); // .set_horizontal(&self.buffer, temp_index, c.vis_start);
if !self.buffer.is_grapheme(temp_index) { if !self.buffer.is_grapheme(temp_index) {
temp_index = self.buffer.nth_prev_grapheme(temp_index, 1); temp_index = self.buffer.nth_prev_grapheme(temp_index, 1);

View File

@ -158,24 +158,17 @@ impl LineFormatter {
// and handle appropriately. // and handle appropriately.
if offset_char_v_pos < 0 { if offset_char_v_pos < 0 {
// If we're off the start of the block. // 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 { if char_idx == 0 {
// We reached the start of the whole buffer.
break; 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 { } else if offset_char_v_pos >= block_v_dim as isize {
// If we're off the end of the block. // If we're off the end of the block.
if char_idx >= buf.text.len_chars() { char_idx = (char_idx + block.len_chars() - char_offset).min(buf.text.len_chars());
// We reached the end of the whole buffer. v_offset -= block_v_dim as isize - char_v_pos as isize;
char_idx = buf.text.len_chars(); if char_idx == buf.text.len_chars() {
break; 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 { } else {
// If the vertical offset is within this block, calculate an // If the vertical offset is within this block, calculate an
@ -187,7 +180,8 @@ impl LineFormatter {
} }
i += char_count(&g); i += char_count(&g);
} }
char_idx += block.len_chars() - char_offset + i; char_idx -= char_offset;
char_idx += i;
v_offset = 0; v_offset = 0;
} }
} }
@ -233,12 +227,13 @@ impl LineFormatter {
buf: &'b Buffer, buf: &'b Buffer,
char_idx: usize, char_idx: usize,
) -> (RopeSlice<'b>, BlockVisIter<'b>, usize) { ) -> (RopeSlice<'b>, BlockVisIter<'b>, usize) {
let (line_i, col_i) = buf.index_to_line_col(char_idx); let line_i = buf.text.char_to_line(char_idx);
let line = buf.get_line(line_i); 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 // 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 (block_index, block_range) = block_index_and_range(&line, char_idx - line_start);
let col_i_adjusted = col_i - block_range.0;
// Get the right block and an iter into it. // Get the right block and an iter into it.
let block = line.slice(block_range.0..block_range.1); let block = line.slice(block_range.0..block_range.1);
@ -258,7 +253,7 @@ impl LineFormatter {
self.wrap_extra_indent, self.wrap_extra_indent,
); );
(block, vis_iter, col_i_adjusted) (block, vis_iter, char_idx - (line_start + block_range.0))
} }
} }