From 6a0757ebf9535c45df121201e71656ed9fa39974 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 20 May 2021 12:52:58 +0200 Subject: [PATCH] Don't store rightmost row/char-column as a Point --- zed/src/editor/buffer/mod.rs | 15 ++++++++++----- zed/src/editor/buffer/rope.rs | 24 +++++++++++++++--------- zed/src/editor/display_map/fold_map.rs | 5 +++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/zed/src/editor/buffer/mod.rs b/zed/src/editor/buffer/mod.rs index ab91ef83894a691c0ad4d588d0c42312d96204da..ddf40a983293df16dee765f718753cf73563ac0f 100644 --- a/zed/src/editor/buffer/mod.rs +++ b/zed/src/editor/buffer/mod.rs @@ -2525,7 +2525,8 @@ mod tests { lines: Point::new(1, 0), first_line_chars: 1, last_line_chars: 0, - rightmost_point: Point::new(0, 1), + rightmost_row: 0, + rightmost_row_chars: 1, } ); assert_eq!( @@ -2535,7 +2536,8 @@ mod tests { lines: Point::new(3, 0), first_line_chars: 1, last_line_chars: 0, - rightmost_point: Point::new(2, 4), + rightmost_row: 2, + rightmost_row_chars: 4, } ); assert_eq!( @@ -2545,7 +2547,8 @@ mod tests { lines: Point::new(4, 1), first_line_chars: 2, last_line_chars: 1, - rightmost_point: Point::new(3, 6), + rightmost_row: 3, + rightmost_row_chars: 6, } ); assert_eq!( @@ -2555,7 +2558,8 @@ mod tests { lines: Point::new(4, 3), first_line_chars: 2, last_line_chars: 3, - rightmost_point: Point::new(3, 6), + rightmost_row: 3, + rightmost_row_chars: 6, } ); assert_eq!( @@ -2565,7 +2569,8 @@ mod tests { lines: Point::new(2, 3), first_line_chars: 4, last_line_chars: 3, - rightmost_point: Point::new(1, 6), + rightmost_row: 1, + rightmost_row_chars: 6, } ); buffer diff --git a/zed/src/editor/buffer/rope.rs b/zed/src/editor/buffer/rope.rs index d03afaf2a72fe57838a26a038eb1b1cf3da8870a..83c5ee4d5dec7707393f41d572562384529d160d 100644 --- a/zed/src/editor/buffer/rope.rs +++ b/zed/src/editor/buffer/rope.rs @@ -392,7 +392,8 @@ pub struct TextSummary { pub lines: Point, pub first_line_chars: u32, pub last_line_chars: u32, - pub rightmost_point: Point, + pub rightmost_row: u32, + pub rightmost_row_chars: u32, } impl<'a> From<&'a str> for TextSummary { @@ -400,7 +401,8 @@ impl<'a> From<&'a str> for TextSummary { let mut lines = Point::new(0, 0); let mut first_line_chars = 0; let mut last_line_chars = 0; - let mut rightmost_point = Point::new(0, 0); + let mut rightmost_row = 0; + let mut rightmost_row_chars = 0; for c in text.chars() { if c == '\n' { lines.row += 1; @@ -415,8 +417,9 @@ impl<'a> From<&'a str> for TextSummary { first_line_chars = last_line_chars; } - if last_line_chars > rightmost_point.column { - rightmost_point = Point::new(lines.row, last_line_chars); + if last_line_chars > rightmost_row_chars { + rightmost_row = lines.row; + rightmost_row_chars = last_line_chars; } } @@ -425,7 +428,8 @@ impl<'a> From<&'a str> for TextSummary { lines, first_line_chars, last_line_chars, - rightmost_point, + rightmost_row, + rightmost_row_chars, } } } @@ -441,11 +445,13 @@ impl sum_tree::Summary for TextSummary { impl<'a> std::ops::AddAssign<&'a Self> for TextSummary { fn add_assign(&mut self, other: &'a Self) { let joined_chars = self.last_line_chars + other.first_line_chars; - if joined_chars > self.rightmost_point.column { - self.rightmost_point = Point::new(self.lines.row, joined_chars); + if joined_chars > self.rightmost_row_chars { + self.rightmost_row = self.lines.row; + self.rightmost_row_chars = joined_chars; } - if other.rightmost_point.column > self.rightmost_point.column { - self.rightmost_point = self.lines + other.rightmost_point; + if other.rightmost_row_chars > self.rightmost_row_chars { + self.rightmost_row = self.lines.row + other.rightmost_row; + self.rightmost_row_chars = other.rightmost_row_chars; } if self.lines.row == 0 { diff --git a/zed/src/editor/display_map/fold_map.rs b/zed/src/editor/display_map/fold_map.rs index 121909cba71cebeb47aa5cf022bed30434434fd8..5494d61c9a51fd064c2374f80916fec005b6e1ea 100644 --- a/zed/src/editor/display_map/fold_map.rs +++ b/zed/src/editor/display_map/fold_map.rs @@ -68,7 +68,7 @@ impl FoldMap { } pub fn rightmost_row(&self, ctx: &AppContext) -> u32 { - self.sync(ctx).summary().display.rightmost_point.row + self.sync(ctx).summary().display.rightmost_row } pub fn folds_in_range<'a, T>( @@ -340,7 +340,8 @@ impl FoldMap { lines, first_line_chars: chars, last_line_chars: chars, - rightmost_point: Point::new(0, chars), + rightmost_row: 0, + rightmost_row_chars: chars, }, buffer: buffer.text_summary_for_range(fold.start..fold.end), },