From 8f94b3295337eb929cddcf6551a675c654e6f9aa Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sat, 7 Feb 2015 22:57:25 -0800 Subject: [PATCH] Have view scrolling nearly working again. Just doesn't *quite* work with wrapped lines. Need to investigate. --- src/editor/mod.rs | 19 +++++++++++++------ src/formatter.rs | 7 ++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 420a7d2..a7689f0 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -296,17 +296,24 @@ 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. + // TODO: handle multiple cursors properly. Should only move if // there are no cursors currently in view, and should jump to // the closest cursor. - //let gi = self.cursors[0].range.0; - //let vho = self.cursors[0].vis_start; - - //self.view_pos.0 = gi; + // Find the first and last grapheme index visible within the editor. + let g_first = self.formatter.index_set_horizontal_v2d(&self.buffer, self.view_pos.0, 0, Floor); + let mut g_last = self.formatter.index_offset_vertical_v2d(&self.buffer, g_first, (self.view_dim.0 - 1) as isize, (Floor, Floor)); + g_last = self.formatter.index_set_horizontal_v2d(&self.buffer, g_last, self.view_dim.1, Floor); - // TODO: horizontal offset - //self.view_pos.1 = vho; + // Adjust the view depending on where the cursor is + if self.cursors[0].range.0 < g_first { + self.view_pos.0 = self.cursors[0].range.0; + } + else if self.cursors[0].range.0 > g_last { + self.view_pos.0 = self.formatter.index_offset_vertical_v2d(&self.buffer, self.cursors[0].range.0, -((self.view_dim.0 - 1) as isize), (Floor, Floor)); + } } diff --git a/src/formatter.rs b/src/formatter.rs index 3f1a90c..1b4e637 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -94,7 +94,12 @@ pub trait LineFormatter { let (v, _) = self.index_to_v2d(line, col_i); let mut new_col_i = self.v2d_to_index(line, (v, horizontal), (RoundingBehavior::Floor, rounding)); - if new_col_i >= line.grapheme_count() && line.grapheme_count() > 0 { + + // Make sure we're not pushing the index off the end of the line + if (line_i + 1) < buf.line_count() + && new_col_i >= line.grapheme_count() + && line.grapheme_count() > 0 + { new_col_i = line.grapheme_count() - 1; }