From 8ae69036b8a4b4f31da805b8929ffae706b9394d Mon Sep 17 00:00:00 2001 From: Finn Evers Date: Tue, 21 Oct 2025 10:38:40 +0200 Subject: [PATCH] editor: Toggle diff hunk based on current mouse position (#40773) This fixes an issue where we would search for the hovered diff hunk based on the mouse hit test computed during (or prior) editor paint instead of the mouse hit test computed prior to the mouse event invocation. That in turn could lead to cases where moving the mouse from the editor to the project panel and then clicking a file shortly after would expand a diff hunk when actually nothing should happen in that case. Release Notes: - Fixed an issue where diff hunks would sometimes erroneously toggle upon mouse clicks. --- crates/editor/src/element.rs | 37 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 054240ab73ba5ac36499e1a1f2e66f8dae06503c..bb294d822714f179898ffdf49a6b1961526e1691 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -651,7 +651,6 @@ impl EditorElement { fn mouse_left_down( editor: &mut Editor, event: &MouseDownEvent, - hovered_hunk: Option>, position_map: &PositionMap, line_numbers: &HashMap, window: &mut Window, @@ -667,7 +666,20 @@ impl EditorElement { let mut click_count = event.click_count; let mut modifiers = event.modifiers; - if let Some(hovered_hunk) = hovered_hunk { + if let Some(hovered_hunk) = + position_map + .display_hunks + .iter() + .find_map(|(hunk, hunk_hitbox)| match hunk { + DisplayDiffHunk::Folded { .. } => None, + DisplayDiffHunk::Unfolded { + multi_buffer_range, .. + } => hunk_hitbox + .as_ref() + .is_some_and(|hitbox| hitbox.is_hovered(window)) + .then(|| multi_buffer_range.clone()), + }) + { editor.toggle_single_diff_hunk(hovered_hunk, cx); cx.notify(); return; @@ -7247,26 +7259,6 @@ impl EditorElement { window.on_mouse_event({ let position_map = layout.position_map.clone(); let editor = self.editor.clone(); - let diff_hunk_range = - layout - .display_hunks - .iter() - .find_map(|(hunk, hunk_hitbox)| match hunk { - DisplayDiffHunk::Folded { .. } => None, - DisplayDiffHunk::Unfolded { - multi_buffer_range, .. - } => { - if hunk_hitbox - .as_ref() - .map(|hitbox| hitbox.is_hovered(window)) - .unwrap_or(false) - { - Some(multi_buffer_range.clone()) - } else { - None - } - } - }); let line_numbers = layout.line_numbers.clone(); move |event: &MouseDownEvent, phase, window, cx| { @@ -7283,7 +7275,6 @@ impl EditorElement { Self::mouse_left_down( editor, event, - diff_hunk_range.clone(), &position_map, line_numbers.as_ref(), window,