Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Fix `Open File` from multibuffer header jumping to wrong excerpt (#54652)
Neel6f84d9be2c64
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 7bc8eac0bfc..025cd4251ad 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -3,7 +3,7 @@ use crate::{JoinLines,code_context_menus::CodeContextMenu,edit_prediction_tests::FakeEditPredictionDelegate,- element::StickyHeader,+ element::{StickyHeader, header_jump_data},linked_editing_ranges::LinkedEditingRanges,runnables::RunnableTasks,scroll::scroll_amount::ScrollAmount,@@ -19335,6 +19335,112 @@ fn test_editing_disjoint_excerpts(cx: &mut TestAppContext) {});}+#[gpui::test]+fn test_header_jump_data_uses_selection_excerpt(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ // 25-line buffer so excerpts at rows 1, 10, and 20 (each a 1-line range,+ // expanded by 2 context lines) can't merge into a single excerpt.+ let buffer_text = (0..25)+ .map(|row| format!("line {row}"))+ .collect::<Vec<_>>()+ .join("\n");+ let buffer = cx.new(|cx| Buffer::local(buffer_text, cx));+ let buffer_id = buffer.read_with(cx, |buffer, _| buffer.remote_id());++ let multibuffer = cx.new(|cx| {+ let mut multibuffer = MultiBuffer::new(ReadWrite);+ multibuffer.set_excerpts_for_path(+ PathKey::sorted(0),+ buffer.clone(),+ [+ Point::new(1, 0)..Point::new(1, 0),+ Point::new(10, 0)..Point::new(10, 0),+ Point::new(20, 0)..Point::new(20, 0),+ ],+ 2,+ cx,+ );+ multibuffer+ });++ let (editor, cx) = cx.add_window_view(|window, cx| build_editor(multibuffer, window, cx));++ editor.update_in(cx, |editor, window, cx| {+ let snapshot = editor.snapshot(window, cx);+ let display_snapshot = editor.display_snapshot(cx);++ // Ensure the three ranges landed in three separate excerpts.+ let excerpts: Vec<_> = snapshot+ .buffer_snapshot()+ .excerpts_for_buffer(buffer_id)+ .collect();+ assert_eq!(excerpts.len(), 3);++ // Place the cursor at the start of the third excerpt, expressed in+ // terms of the underlying buffer.+ let selection_buffer_row = 20;+ let buffer_entity = editor.buffer().read(cx).buffer(buffer_id).unwrap();+ let selection_anchor = editor.buffer().update(cx, |multibuffer, cx| {+ multibuffer+ .buffer_point_to_anchor(&buffer_entity, Point::new(selection_buffer_row, 0), cx)+ .expect("buffer row 20 maps to a multibuffer anchor")+ });+ editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {+ s.select_anchor_ranges([selection_anchor..selection_anchor])+ });++ let mut latest_selection_anchors: HashMap<BufferId, Anchor> = HashMap::default();+ for selection in editor.selections.all_anchors(&display_snapshot).iter() {+ let head = selection.head();+ if let Some((text_anchor, _)) = snapshot.buffer_snapshot().anchor_to_buffer_anchor(head)+ {+ latest_selection_anchors.insert(text_anchor.buffer_id, head);+ }+ }++ // The sticky buffer header represents the FIRST excerpt of its buffer,+ // even when the cursor is in a later excerpt. That mismatch is the+ // precondition for the regression.+ let first_excerpt = snapshot+ .buffer_snapshot()+ .excerpt_boundaries_in_range(MultiBufferOffset(0)..snapshot.buffer_snapshot().len())+ .next()+ .expect("multibuffer has at least one excerpt")+ .next;++ let jump_data = header_jump_data(+ &snapshot,+ DisplayRow(0),+ FILE_HEADER_HEIGHT + MULTI_BUFFER_EXCERPT_HEADER_HEIGHT,+ &first_excerpt,+ &latest_selection_anchors,+ );++ match jump_data {+ JumpData::MultiBufferPoint {+ position,+ line_offset_from_top,+ ..+ } => {+ assert_eq!(+ position.row, selection_buffer_row,+ "jump should target the cursor's buffer row, not the first excerpt's row"+ );+ assert!(+ line_offset_from_top < selection_buffer_row,+ "line_offset_from_top ({line_offset_from_top}) should be measured from the \+ selection's excerpt, not the first excerpt; expected less than \+ selection_buffer_row ({selection_buffer_row})"+ );+ }+ JumpData::MultiBufferRow { .. } => {+ panic!("expected MultiBufferPoint jump data when a selection is present")+ }+ }+ });+}+#[gpui::test]async fn test_extra_newline_insertion(cx: &mut TestAppContext) {init_test(cx, |_| {});diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rsindex 9f2cba7dffd..de6c68c34cd 100644--- a/crates/editor/src/element.rs+++ b/crates/editor/src/element.rs@@ -8352,21 +8352,34 @@ pub(crate) fn header_jump_data() -> JumpData {let multibuffer_snapshot = editor_snapshot.buffer_snapshot();let buffer = first_excerpt.buffer(multibuffer_snapshot);- let (jump_anchor, jump_buffer) = if let Some(anchor) =+ let (jump_anchor, jump_buffer, excerpt_start) = if let Some(anchor) =latest_selection_anchors.get(&first_excerpt.buffer_id())&& let Some((jump_anchor, selection_buffer)) =multibuffer_snapshot.anchor_to_buffer_anchor(*anchor){- (jump_anchor, selection_buffer)+ let jump_offset = text::ToOffset::to_offset(&jump_anchor, selection_buffer);+ let selection_excerpt_start = multibuffer_snapshot+ .excerpts_for_buffer(jump_anchor.buffer_id)+ .find(|excerpt| {+ let start = text::ToOffset::to_offset(&excerpt.context.start, selection_buffer);+ let end = text::ToOffset::to_offset(&excerpt.context.end, selection_buffer);+ start <= jump_offset && jump_offset <= end+ })+ .map(|excerpt| excerpt.context.start)+ .unwrap_or(first_excerpt.range.context.start);+ (jump_anchor, selection_buffer, selection_excerpt_start)} else {- (first_excerpt.range.primary.start, buffer)+ (+ first_excerpt.range.primary.start,+ buffer,+ first_excerpt.range.context.start,+ )};- let excerpt_start = first_excerpt.range.context.start;let jump_position = language::ToPoint::to_point(&jump_anchor, jump_buffer);let rows_from_excerpt_start = if jump_anchor == excerpt_start {0} else {- let excerpt_start_point = language::ToPoint::to_point(&excerpt_start, buffer);+ let excerpt_start_point = language::ToPoint::to_point(&excerpt_start, jump_buffer);jump_position.row.saturating_sub(excerpt_start_point.row)};