Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix relative line number calculation when the first row is wrapped (#53759)
./danieleda2b62c917f5
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 70b4911e17c..dbc177e7a93 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -11626,30 +11626,50 @@ impl EditorSnapshot {current_selection_head: DisplayRow,count_wrapped_lines: bool,) -> HashMap<DisplayRow, u32> {- let initial_offset =- self.relative_line_delta(current_selection_head, rows.start, count_wrapped_lines);-- self.row_infos(rows.start)+ let mut row_infos = self+ .row_infos(rows.start).take(rows.len()).enumerate()- .map(|(i, row_info)| (DisplayRow(rows.start.0 + i as u32), row_info))+ .map(|(index, row_info)| (DisplayRow(rows.start.0 + index as u32), row_info)).filter(|(_row, row_info)| {row_info.buffer_row.is_some()|| (count_wrapped_lines && row_info.wrapped_buffer_row.is_some())})- .enumerate()- .filter_map(|(i, (row, row_info))| {+ .peekable();++ // We find the first row that actually passes the filter and calculate its+ // delta independently. This ensures accuracy when scrolling, as the first+ // visible row in `rows` might be a wrap part that is filtered out, which+ // would otherwise offset the counter for subsequent lines if we used+ // `rows.start` as the base for enumeration.+ let Some((first_row, _)) = row_infos.peek() else {+ return HashMap::default();+ };++ let mut current_delta =+ self.relative_line_delta(current_selection_head, *first_row, count_wrapped_lines);++ row_infos+ .filter_map(|(row, row_info)| {+ let is_deleted = row_info+ .diff_status+ .is_some_and(|status| status.is_deleted());++ if !self.number_deleted_lines && is_deleted {+ // Even if we don't number this line, it still counts as a unit+ // of distance for the relative numbers of lines below it.+ current_delta += 1;+ return None;+ }+// We want to ensure here that the current line has absolute// numbering, even if we are in a soft-wrapped line. With the// exception that if we are in a deleted line, we should number this// relative with 0, as otherwise it would have no line number at all- let relative_line_number = (initial_offset + i as i64).unsigned_abs() as u32;+ let relative_line_number = current_delta.unsigned_abs() as u32;+ current_delta += 1;- (relative_line_number != 0- || row_info- .diff_status- .is_some_and(|status| status.is_deleted()))- .then_some((row, relative_line_number))+ (relative_line_number != 0 || is_deleted).then_some((row, relative_line_number))}).collect()}diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rsindex 0e4cb930dcb..80f6bf7fab9 100644--- a/crates/editor/src/element.rs+++ b/crates/editor/src/element.rs@@ -11280,6 +11280,57 @@ mod tests {assert_eq!(relative_rows[&DisplayRow(5)], 2);}+ #[gpui::test]+ async fn test_relative_line_numbers_after_scrolling_wrapped_line(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let window = cx.add_window(|window, cx| {+ let buffer = MultiBuffer::build_simple("", cx);+ Editor::new(EditorMode::full(), buffer, None, window, cx)+ });++ update_test_language_settings(+ cx,+ &|settings: &mut settings::AllLanguageSettingsContent| {+ settings.defaults.soft_wrap = Some(language_settings::SoftWrap::Bounded);+ settings.defaults.preferred_line_length = Some(10);+ },+ );++ window+ .update(cx, |editor, _window, cx| {+ let text = format!("{}\nshort line", "a".repeat(100));+ editor.buffer.update(cx, |buffer, cx| {+ buffer.edit([(Point::default()..Point::default(), text)], None, cx);+ });+ })+ .unwrap();+ cx.run_until_parked();++ window+ .update(cx, |editor, _window, cx| {+ let snapshot = editor.snapshot(_window, cx);++ let line_1_display_row = Point::new(1, 0).to_display_point(&snapshot).row();+ assert!(+ line_1_display_row.0 > 1,+ "Line 0 should wrap into multiple rows"+ );++ let start_row = DisplayRow(1);+ let relative_rows = snapshot.calculate_relative_line_numbers(+ &(start_row..line_1_display_row.next_row()),+ line_1_display_row,+ false,+ );++ // If the bug exists, line_1_display_row would have a non-zero relative number+ // and would be included in the map. It should be 0 (and thus omitted).+ assert!(!relative_rows.contains_key(&line_1_display_row));+ })+ .unwrap();+ }+#[gpui::test]async fn test_vim_visual_selections(cx: &mut TestAppContext) {init_test(cx, |_| {});