From 9746f4f267a5f726fa6dab9992758dee409a736f Mon Sep 17 00:00:00 2001 From: Bennet Bo Fenner <53836821+bennetbo@users.noreply.github.com> Date: Mon, 29 Apr 2024 15:54:40 +0200 Subject: [PATCH] Fix relative line numbers (#11161) Closes #11105 Release Notes: - Fixed rendering of relative line numbers in editor ([#11105](https://github.com/zed-industries/zed/issues/11105)). Co-authored-by: Thorsten --- crates/editor/src/element.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index c384f13bf0..203f33b44b 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1375,7 +1375,7 @@ impl EditorElement { fn calculate_relative_line_numbers( &self, - buffer_rows: Vec>, + snapshot: &EditorSnapshot, rows: &Range, relative_to: Option, ) -> HashMap { @@ -1385,6 +1385,12 @@ impl EditorElement { }; let start = rows.start.min(relative_to); + let end = rows.end.max(relative_to); + + let buffer_rows = snapshot + .buffer_rows(start) + .take(1 + (end - start) as usize) + .collect::>(); let head_idx = relative_to - start; let mut delta = 1; @@ -1459,9 +1465,7 @@ impl EditorElement { None }; - let buffer_rows = buffer_rows.collect::>(); - let relative_rows = - self.calculate_relative_line_numbers(buffer_rows.clone(), &rows, relative_to); + let relative_rows = self.calculate_relative_line_numbers(snapshot, &rows, relative_to); for (ix, row) in buffer_rows.into_iter().enumerate() { let display_row = rows.start + ix as u32; @@ -4578,8 +4582,12 @@ mod tests { .unwrap(); assert_eq!(layouts.len(), 6); - let relative_rows = - element.calculate_relative_line_numbers((0..6).map(Some).collect(), &(0..6), Some(3)); + let relative_rows = window + .update(cx, |editor, cx| { + let snapshot = editor.snapshot(cx); + element.calculate_relative_line_numbers(&snapshot, &(0..6), Some(3)) + }) + .unwrap(); assert_eq!(relative_rows[&0], 3); assert_eq!(relative_rows[&1], 2); assert_eq!(relative_rows[&2], 1); @@ -4588,16 +4596,24 @@ mod tests { assert_eq!(relative_rows[&5], 2); // works if cursor is before screen - let relative_rows = - element.calculate_relative_line_numbers((0..6).map(Some).collect(), &(3..6), Some(1)); + let relative_rows = window + .update(cx, |editor, cx| { + let snapshot = editor.snapshot(cx); + element.calculate_relative_line_numbers(&snapshot, &(3..6), Some(1)) + }) + .unwrap(); assert_eq!(relative_rows.len(), 3); assert_eq!(relative_rows[&3], 2); assert_eq!(relative_rows[&4], 3); assert_eq!(relative_rows[&5], 4); // works if cursor is after screen - let relative_rows = - element.calculate_relative_line_numbers((0..6).map(Some).collect(), &(0..3), Some(6)); + let relative_rows = window + .update(cx, |editor, cx| { + let snapshot = editor.snapshot(cx); + element.calculate_relative_line_numbers(&snapshot, &(0..3), Some(6)) + }) + .unwrap(); assert_eq!(relative_rows.len(), 3); assert_eq!(relative_rows[&0], 5); assert_eq!(relative_rows[&1], 4);