From 13ecd16685dccb701a3b76a4b59013f4c32467d5 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 21 Dec 2021 12:36:46 -0800 Subject: [PATCH] Index max buffer row on MultiBuffer --- crates/editor/src/display_map.rs | 4 ++-- crates/editor/src/element.rs | 2 +- crates/editor/src/multi_buffer.rs | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index 469dac5802cf59f616b6873d5fa926ad0e3d6de4..a3c95ad4bfa9ef89b9025495cd382cb72aca8aed 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -198,8 +198,8 @@ impl DisplaySnapshot { self.blocks_snapshot.buffer_rows(start_row) } - pub fn buffer_row_count(&self) -> u32 { - self.buffer_snapshot.max_point().row + 1 + pub fn max_buffer_row(&self) -> u32 { + self.buffer_snapshot.max_buffer_row() } pub fn prev_row_boundary(&self, input_display_point: DisplayPoint) -> (DisplayPoint, Point) { diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index e790973c8c9e15cefcd8b191fc58b1bf6c9f65be..9c45207bb01ac5cf02c6f2d0deeaf37ed954ec43 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -436,7 +436,7 @@ impl EditorElement { } fn max_line_number_width(&self, snapshot: &EditorSnapshot, cx: &LayoutContext) -> f32 { - let digit_count = (snapshot.buffer_row_count() as f32).log10().floor() as usize + 1; + let digit_count = (snapshot.max_buffer_row() as f32).log10().floor() as usize + 1; let style = &self.settings.style; cx.text_layout_cache diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index f66ed061ad47fb7537a0962372295fab201c076b..f4c55f2b8601e5748eb5f3251047b09ede80a7e3 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -97,6 +97,7 @@ struct Excerpt { buffer_id: usize, buffer: BufferSnapshot, range: Range, + max_buffer_row: u32, text_summary: TextSummary, has_trailing_newline: bool, } @@ -104,6 +105,7 @@ struct Excerpt { #[derive(Clone, Debug, Default)] struct ExcerptSummary { excerpt_id: ExcerptId, + max_buffer_row: u32, text: TextSummary, } @@ -884,6 +886,10 @@ impl MultiBufferSnapshot { self.excerpts.summary().text.bytes } + pub fn max_buffer_row(&self) -> u32 { + self.excerpts.summary().max_buffer_row + } + pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize { let mut cursor = self.excerpts.cursor::(); cursor.seek(&offset, Bias::Right, &()); @@ -1565,6 +1571,7 @@ impl Excerpt { ) -> Self { Excerpt { id, + max_buffer_row: range.end.to_point(&buffer).row, text_summary: buffer.text_summary_for_range::(range.to_offset(&buffer)), buffer_id, buffer, @@ -1660,6 +1667,7 @@ impl sum_tree::Item for Excerpt { } ExcerptSummary { excerpt_id: self.id.clone(), + max_buffer_row: self.max_buffer_row, text, } } @@ -1672,6 +1680,7 @@ impl sum_tree::Summary for ExcerptSummary { debug_assert!(summary.excerpt_id > self.excerpt_id); self.excerpt_id = summary.excerpt_id.clone(); self.text.add_summary(&summary.text, &()); + self.max_buffer_row = cmp::max(self.max_buffer_row, summary.max_buffer_row); } } @@ -2266,6 +2275,15 @@ mod tests { ); } + assert_eq!( + snapshot.max_buffer_row(), + expected_buffer_rows + .into_iter() + .filter_map(|r| r) + .max() + .unwrap() + ); + let mut excerpt_starts = excerpt_starts.into_iter(); for (buffer, range) in &expected_excerpts { let buffer_id = buffer.id();