From cd3773531c9e323ff0fda4eafdf14be4e4b3e255 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 8 Nov 2023 13:28:20 +0100 Subject: [PATCH 01/11] uncomment mouse moved --- crates/editor2/src/element.rs | 108 ++++--- crates/editor2/src/link_go_to_definition.rs | 329 ++++++++++---------- 2 files changed, 225 insertions(+), 212 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index 569b6581ce1b2f0d3b3145d3773a548f2352a05e..0a590fe2fdb8ec8d017461fe36dfd490c714ce49 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -2,6 +2,10 @@ use crate::{ display_map::{BlockStyle, DisplaySnapshot, FoldStatus, HighlightedChunk, ToDisplayPoint}, editor_settings::ShowScrollbar, git::{diff_hunk_to_display, DisplayDiffHunk}, + hover_popover::hover_at, + link_go_to_definition::{ + update_go_to_definition_link, update_inlay_link_and_hover_points, GoToDefinitionTrigger, + }, scroll::scroll_amount::ScrollAmount, CursorShape, DisplayPoint, Editor, EditorMode, EditorSettings, EditorSnapshot, EditorStyle, HalfPageDown, HalfPageUp, LineDown, LineUp, MoveDown, PageDown, PageUp, Point, Selection, @@ -12,9 +16,9 @@ use collections::{BTreeMap, HashMap}; use gpui::{ black, hsla, point, px, relative, size, transparent_black, Action, AnyElement, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchContext, DispatchPhase, - Edges, Element, ElementId, Entity, Hsla, KeyDownEvent, KeyListener, KeyMatch, Line, Pixels, - ScrollWheelEvent, ShapedGlyph, Size, StatefulInteraction, Style, TextRun, TextStyle, - TextSystem, ViewContext, WindowContext, + Edges, Element, ElementId, Entity, Hsla, KeyDownEvent, KeyListener, KeyMatch, Line, Modifiers, + MouseMoveEvent, Pixels, ScrollWheelEvent, ShapedGlyph, Size, StatefulInteraction, Style, + TextRun, TextStyle, TextSystem, ViewContext, WindowContext, }; use itertools::Itertools; use language::language_settings::ShowWhitespaceSetting; @@ -423,50 +427,50 @@ impl EditorElement { // } // } - // fn mouse_moved( - // editor: &mut Editor, - // MouseMovedEvent { - // modifiers: Modifiers { shift, cmd, .. }, - // position, - // .. - // }: MouseMovedEvent, - // position_map: &PositionMap, - // text_bounds: Bounds, - // cx: &mut ViewContext, - // ) -> bool { - // // This will be handled more correctly once https://github.com/zed-industries/zed/issues/1218 is completed - // // Don't trigger hover popover if mouse is hovering over context menu - // if text_bounds.contains_point(position) { - // let point_for_position = position_map.point_for_position(text_bounds, position); - // match point_for_position.as_valid() { - // Some(point) => { - // update_go_to_definition_link( - // editor, - // Some(GoToDefinitionTrigger::Text(point)), - // cmd, - // shift, - // cx, - // ); - // hover_at(editor, Some(point), cx); - // } - // None => { - // update_inlay_link_and_hover_points( - // &position_map.snapshot, - // point_for_position, - // editor, - // cmd, - // shift, - // cx, - // ); - // } - // } - // } else { - // update_go_to_definition_link(editor, None, cmd, shift, cx); - // hover_at(editor, None, cx); - // } + fn mouse_moved( + editor: &mut Editor, + MouseMoveEvent { + modifiers: Modifiers { shift, command, .. }, + position, + .. + }: &MouseMoveEvent, + position_map: &PositionMap, + text_bounds: Bounds, + cx: &mut ViewContext, + ) -> bool { + // This will be handled more correctly once https://github.com/zed-industries/zed/issues/1218 is completed + // Don't trigger hover popover if mouse is hovering over context menu + if text_bounds.contains_point(position) { + let point_for_position = position_map.point_for_position(text_bounds, position.clone()); + match point_for_position.as_valid() { + Some(point) => { + update_go_to_definition_link( + editor, + Some(GoToDefinitionTrigger::Text(point)), + *command, + *shift, + cx, + ); + hover_at(editor, Some(point), cx); + } + None => { + update_inlay_link_and_hover_points( + &position_map.snapshot, + point_for_position, + editor, + *command, + *shift, + cx, + ); + } + } + } else { + update_go_to_definition_link(editor, None, *command, *shift, cx); + hover_at(editor, None, cx); + } - // true - // } + true + } fn scroll( editor: &mut Editor, @@ -2772,6 +2776,18 @@ impl Element for EditorElement { } } }); + cx.on_mouse_event({ + let position_map = layout.position_map.clone(); + move |editor, event: &MouseMoveEvent, phase, cx| { + if phase != DispatchPhase::Bubble { + return; + } + + if Self::mouse_moved(editor, event, &position_map, bounds, cx) { + cx.stop_propagation() + } + } + }); if editor.focus_handle.is_focused(cx) { cx.handle_text_input(); diff --git a/crates/editor2/src/link_go_to_definition.rs b/crates/editor2/src/link_go_to_definition.rs index 9db4061e5096ac3f4b3a221a3bc22f45114b26d2..d36762f3955e126200d7025002d543e9744bce79 100644 --- a/crates/editor2/src/link_go_to_definition.rs +++ b/crates/editor2/src/link_go_to_definition.rs @@ -171,173 +171,170 @@ pub fn update_inlay_link_and_hover_points( shift_held: bool, cx: &mut ViewContext<'_, Editor>, ) { - todo!("old implementation below") -} -// ) { -// let hovered_offset = if point_for_position.column_overshoot_after_line_end == 0 { -// Some(snapshot.display_point_to_inlay_offset(point_for_position.exact_unclipped, Bias::Left)) -// } else { -// None -// }; -// let mut go_to_definition_updated = false; -// let mut hover_updated = false; -// if let Some(hovered_offset) = hovered_offset { -// let buffer_snapshot = editor.buffer().read(cx).snapshot(cx); -// let previous_valid_anchor = buffer_snapshot.anchor_at( -// point_for_position.previous_valid.to_point(snapshot), -// Bias::Left, -// ); -// let next_valid_anchor = buffer_snapshot.anchor_at( -// point_for_position.next_valid.to_point(snapshot), -// Bias::Right, -// ); -// if let Some(hovered_hint) = editor -// .visible_inlay_hints(cx) -// .into_iter() -// .skip_while(|hint| { -// hint.position -// .cmp(&previous_valid_anchor, &buffer_snapshot) -// .is_lt() -// }) -// .take_while(|hint| { -// hint.position -// .cmp(&next_valid_anchor, &buffer_snapshot) -// .is_le() -// }) -// .max_by_key(|hint| hint.id) -// { -// let inlay_hint_cache = editor.inlay_hint_cache(); -// let excerpt_id = previous_valid_anchor.excerpt_id; -// if let Some(cached_hint) = inlay_hint_cache.hint_by_id(excerpt_id, hovered_hint.id) { -// match cached_hint.resolve_state { -// ResolveState::CanResolve(_, _) => { -// if let Some(buffer_id) = previous_valid_anchor.buffer_id { -// inlay_hint_cache.spawn_hint_resolve( -// buffer_id, -// excerpt_id, -// hovered_hint.id, -// cx, -// ); -// } -// } -// ResolveState::Resolved => { -// let mut extra_shift_left = 0; -// let mut extra_shift_right = 0; -// if cached_hint.padding_left { -// extra_shift_left += 1; -// extra_shift_right += 1; -// } -// if cached_hint.padding_right { -// extra_shift_right += 1; -// } -// match cached_hint.label { -// project::InlayHintLabel::String(_) => { -// if let Some(tooltip) = cached_hint.tooltip { -// hover_popover::hover_at_inlay( -// editor, -// InlayHover { -// excerpt: excerpt_id, -// tooltip: match tooltip { -// InlayHintTooltip::String(text) => HoverBlock { -// text, -// kind: HoverBlockKind::PlainText, -// }, -// InlayHintTooltip::MarkupContent(content) => { -// HoverBlock { -// text: content.value, -// kind: content.kind, -// } -// } -// }, -// range: InlayHighlight { -// inlay: hovered_hint.id, -// inlay_position: hovered_hint.position, -// range: extra_shift_left -// ..hovered_hint.text.len() + extra_shift_right, -// }, -// }, -// cx, -// ); -// hover_updated = true; -// } -// } -// project::InlayHintLabel::LabelParts(label_parts) => { -// let hint_start = -// snapshot.anchor_to_inlay_offset(hovered_hint.position); -// if let Some((hovered_hint_part, part_range)) = -// hover_popover::find_hovered_hint_part( -// label_parts, -// hint_start, -// hovered_offset, -// ) -// { -// let highlight_start = -// (part_range.start - hint_start).0 + extra_shift_left; -// let highlight_end = -// (part_range.end - hint_start).0 + extra_shift_right; -// let highlight = InlayHighlight { -// inlay: hovered_hint.id, -// inlay_position: hovered_hint.position, -// range: highlight_start..highlight_end, -// }; -// if let Some(tooltip) = hovered_hint_part.tooltip { -// hover_popover::hover_at_inlay( -// editor, -// InlayHover { -// excerpt: excerpt_id, -// tooltip: match tooltip { -// InlayHintLabelPartTooltip::String(text) => { -// HoverBlock { -// text, -// kind: HoverBlockKind::PlainText, -// } -// } -// InlayHintLabelPartTooltip::MarkupContent( -// content, -// ) => HoverBlock { -// text: content.value, -// kind: content.kind, -// }, -// }, -// range: highlight.clone(), -// }, -// cx, -// ); -// hover_updated = true; -// } -// if let Some((language_server_id, location)) = -// hovered_hint_part.location -// { -// go_to_definition_updated = true; -// update_go_to_definition_link( -// editor, -// Some(GoToDefinitionTrigger::InlayHint( -// highlight, -// location, -// language_server_id, -// )), -// cmd_held, -// shift_held, -// cx, -// ); -// } -// } -// } -// }; -// } -// ResolveState::Resolving => {} -// } -// } -// } -// } + let hovered_offset = if point_for_position.column_overshoot_after_line_end == 0 { + Some(snapshot.display_point_to_inlay_offset(point_for_position.exact_unclipped, Bias::Left)) + } else { + None + }; + let mut go_to_definition_updated = false; + let mut hover_updated = false; + if let Some(hovered_offset) = hovered_offset { + let buffer_snapshot = editor.buffer().read(cx).snapshot(cx); + let previous_valid_anchor = buffer_snapshot.anchor_at( + point_for_position.previous_valid.to_point(snapshot), + Bias::Left, + ); + let next_valid_anchor = buffer_snapshot.anchor_at( + point_for_position.next_valid.to_point(snapshot), + Bias::Right, + ); + if let Some(hovered_hint) = editor + .visible_inlay_hints(cx) + .into_iter() + .skip_while(|hint| { + hint.position + .cmp(&previous_valid_anchor, &buffer_snapshot) + .is_lt() + }) + .take_while(|hint| { + hint.position + .cmp(&next_valid_anchor, &buffer_snapshot) + .is_le() + }) + .max_by_key(|hint| hint.id) + { + let inlay_hint_cache = editor.inlay_hint_cache(); + let excerpt_id = previous_valid_anchor.excerpt_id; + if let Some(cached_hint) = inlay_hint_cache.hint_by_id(excerpt_id, hovered_hint.id) { + match cached_hint.resolve_state { + ResolveState::CanResolve(_, _) => { + if let Some(buffer_id) = previous_valid_anchor.buffer_id { + inlay_hint_cache.spawn_hint_resolve( + buffer_id, + excerpt_id, + hovered_hint.id, + cx, + ); + } + } + ResolveState::Resolved => { + let mut extra_shift_left = 0; + let mut extra_shift_right = 0; + if cached_hint.padding_left { + extra_shift_left += 1; + extra_shift_right += 1; + } + if cached_hint.padding_right { + extra_shift_right += 1; + } + match cached_hint.label { + project::InlayHintLabel::String(_) => { + if let Some(tooltip) = cached_hint.tooltip { + hover_popover::hover_at_inlay( + editor, + InlayHover { + excerpt: excerpt_id, + tooltip: match tooltip { + InlayHintTooltip::String(text) => HoverBlock { + text, + kind: HoverBlockKind::PlainText, + }, + InlayHintTooltip::MarkupContent(content) => { + HoverBlock { + text: content.value, + kind: content.kind, + } + } + }, + range: InlayHighlight { + inlay: hovered_hint.id, + inlay_position: hovered_hint.position, + range: extra_shift_left + ..hovered_hint.text.len() + extra_shift_right, + }, + }, + cx, + ); + hover_updated = true; + } + } + project::InlayHintLabel::LabelParts(label_parts) => { + let hint_start = + snapshot.anchor_to_inlay_offset(hovered_hint.position); + if let Some((hovered_hint_part, part_range)) = + hover_popover::find_hovered_hint_part( + label_parts, + hint_start, + hovered_offset, + ) + { + let highlight_start = + (part_range.start - hint_start).0 + extra_shift_left; + let highlight_end = + (part_range.end - hint_start).0 + extra_shift_right; + let highlight = InlayHighlight { + inlay: hovered_hint.id, + inlay_position: hovered_hint.position, + range: highlight_start..highlight_end, + }; + if let Some(tooltip) = hovered_hint_part.tooltip { + hover_popover::hover_at_inlay( + editor, + InlayHover { + excerpt: excerpt_id, + tooltip: match tooltip { + InlayHintLabelPartTooltip::String(text) => { + HoverBlock { + text, + kind: HoverBlockKind::PlainText, + } + } + InlayHintLabelPartTooltip::MarkupContent( + content, + ) => HoverBlock { + text: content.value, + kind: content.kind, + }, + }, + range: highlight.clone(), + }, + cx, + ); + hover_updated = true; + } + if let Some((language_server_id, location)) = + hovered_hint_part.location + { + go_to_definition_updated = true; + update_go_to_definition_link( + editor, + Some(GoToDefinitionTrigger::InlayHint( + highlight, + location, + language_server_id, + )), + cmd_held, + shift_held, + cx, + ); + } + } + } + }; + } + ResolveState::Resolving => {} + } + } + } + } -// if !go_to_definition_updated { -// update_go_to_definition_link(editor, None, cmd_held, shift_held, cx); -// } -// if !hover_updated { -// hover_popover::hover_at(editor, None, cx); -// } -// } + if !go_to_definition_updated { + update_go_to_definition_link(editor, None, cmd_held, shift_held, cx); + } + if !hover_updated { + hover_popover::hover_at(editor, None, cx); + } +} #[derive(Debug, Clone, Copy, PartialEq)] pub enum LinkDefinitionKind { From 334829f868494cdcb3ea29756707b1c93126d9af Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 8 Nov 2023 13:41:38 +0100 Subject: [PATCH 02/11] uncomment show_hover --- crates/editor2/src/hover_popover.rs | 33 ++++++++++++++--------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/crates/editor2/src/hover_popover.rs b/crates/editor2/src/hover_popover.rs index 9eddd259af022d03da94a0ceec83b052c1ef684c..bee95994e4b858bb2b693d033d5e12ef76a3a273 100644 --- a/crates/editor2/src/hover_popover.rs +++ b/crates/editor2/src/hover_popover.rs @@ -325,23 +325,22 @@ fn show_hover( }; this.update(&mut cx, |this, cx| { - todo!(); - // if let Some(symbol_range) = hover_popover - // .as_ref() - // .and_then(|hover_popover| hover_popover.symbol_range.as_text_range()) - // { - // // Highlight the selected symbol using a background highlight - // this.highlight_background::( - // vec![symbol_range], - // |theme| theme.editor.hover_popover.highlight, - // cx, - // ); - // } else { - // this.clear_background_highlights::(cx); - // } - // - // this.hover_state.info_popover = hover_popover; - // cx.notify(); + if let Some(symbol_range) = hover_popover + .as_ref() + .and_then(|hover_popover| hover_popover.symbol_range.as_text_range()) + { + // Highlight the selected symbol using a background highlight + this.highlight_background::( + vec![symbol_range], + |theme| theme.element_hover, // todo! update theme + cx, + ); + } else { + this.clear_background_highlights::(cx); + } + + this.hover_state.info_popover = hover_popover; + cx.notify(); })?; Ok::<_, anyhow::Error>(()) From e30449e61a712377c81873edde203def7fd0f24e Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:28:16 +0100 Subject: [PATCH 03/11] Pass text_bounds instead of full editor bounds into mouse_moved. Co-authored-by: Antonio --- crates/editor2/src/element.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index 0a590fe2fdb8ec8d017461fe36dfd490c714ce49..24277521ffb43ee8913b37b649f9935115e6f246 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -2763,6 +2763,14 @@ impl Element for EditorElement { cx: &mut gpui::ViewContext, ) { let layout = self.compute_layout(editor, cx, bounds); + let gutter_bounds = Bounds { + origin: bounds.origin, + size: layout.gutter_size, + }; + let text_bounds = Bounds { + origin: gutter_bounds.upper_right(), + size: layout.text_size, + }; cx.on_mouse_event({ let position_map = layout.position_map.clone(); @@ -2783,7 +2791,7 @@ impl Element for EditorElement { return; } - if Self::mouse_moved(editor, event, &position_map, bounds, cx) { + if Self::mouse_moved(editor, event, &position_map, text_bounds, cx) { cx.stop_propagation() } } @@ -2794,15 +2802,6 @@ impl Element for EditorElement { } cx.with_content_mask(ContentMask { bounds }, |cx| { - let gutter_bounds = Bounds { - origin: bounds.origin, - size: layout.gutter_size, - }; - let text_bounds = Bounds { - origin: gutter_bounds.upper_right(), - size: layout.text_size, - }; - self.paint_background(gutter_bounds, text_bounds, &layout, cx); if layout.gutter_size.width > Pixels::ZERO { self.paint_gutter(gutter_bounds, &layout, editor, cx); From dd20032eab368aceedc8efc20468acaa8192d039 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:36:05 +0100 Subject: [PATCH 04/11] Hook up mouse_down function handler Co-authored-by: Antonio --- crates/editor2/src/element.rs | 121 +++++++++++++++++----------------- 1 file changed, 62 insertions(+), 59 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index 24277521ffb43ee8913b37b649f9935115e6f246..f83ec86106fd8aeee70910c4a66ec0d78813c6ef 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -8,8 +8,8 @@ use crate::{ }, scroll::scroll_amount::ScrollAmount, CursorShape, DisplayPoint, Editor, EditorMode, EditorSettings, EditorSnapshot, EditorStyle, - HalfPageDown, HalfPageUp, LineDown, LineUp, MoveDown, PageDown, PageUp, Point, Selection, - SoftWrap, ToPoint, MAX_LINE_LEN, + HalfPageDown, HalfPageUp, LineDown, LineUp, MoveDown, PageDown, PageUp, Point, SelectPhase, + Selection, SoftWrap, ToPoint, MAX_LINE_LEN, }; use anyhow::Result; use collections::{BTreeMap, HashMap}; @@ -17,8 +17,8 @@ use gpui::{ black, hsla, point, px, relative, size, transparent_black, Action, AnyElement, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchContext, DispatchPhase, Edges, Element, ElementId, Entity, Hsla, KeyDownEvent, KeyListener, KeyMatch, Line, Modifiers, - MouseMoveEvent, Pixels, ScrollWheelEvent, ShapedGlyph, Size, StatefulInteraction, Style, - TextRun, TextStyle, TextSystem, ViewContext, WindowContext, + MouseDownEvent, MouseMoveEvent, Pixels, ScrollWheelEvent, ShapedGlyph, Size, + StatefulInteraction, Style, TextRun, TextStyle, TextSystem, ViewContext, WindowContext, }; use itertools::Itertools; use language::language_settings::ShowWhitespaceSetting; @@ -242,63 +242,54 @@ impl EditorElement { // ) // } - // fn mouse_down( - // editor: &mut Editor, - // MouseButtonEvent { - // position, - // modifiers: - // Modifiers { - // shift, - // ctrl, - // alt, - // cmd, - // .. - // }, - // mut click_count, - // .. - // }: MouseButtonEvent, - // position_map: &PositionMap, - // text_bounds: Bounds, - // gutter_bounds: Bounds, - // cx: &mut EventContext, - // ) -> bool { - // if gutter_bounds.contains_point(position) { - // click_count = 3; // Simulate triple-click when clicking the gutter to select lines - // } else if !text_bounds.contains_point(position) { - // return false; - // } + fn mouse_down( + editor: &mut Editor, + event: &MouseDownEvent, + position_map: &PositionMap, + text_bounds: Bounds, + gutter_bounds: Bounds, + cx: &mut ViewContext, + ) -> bool { + let mut click_count = event.click_count; + let modifiers = event.modifiers; - // let point_for_position = position_map.point_for_position(text_bounds, position); - // let position = point_for_position.previous_valid; - // if shift && alt { - // editor.select( - // SelectPhase::BeginColumnar { - // position, - // goal_column: point_for_position.exact_unclipped.column(), - // }, - // cx, - // ); - // } else if shift && !ctrl && !alt && !cmd { - // editor.select( - // SelectPhase::Extend { - // position, - // click_count, - // }, - // cx, - // ); - // } else { - // editor.select( - // SelectPhase::Begin { - // position, - // add: alt, - // click_count, - // }, - // cx, - // ); - // } + if gutter_bounds.contains_point(&event.position) { + click_count = 3; // Simulate triple-click when clicking the gutter to select lines + } else if !text_bounds.contains_point(&event.position) { + return false; + } - // true - // } + let point_for_position = position_map.point_for_position(text_bounds, event.position); + let position = point_for_position.previous_valid; + if modifiers.shift && modifiers.alt { + editor.select( + SelectPhase::BeginColumnar { + position, + goal_column: point_for_position.exact_unclipped.column(), + }, + cx, + ); + } else if modifiers.shift && !modifiers.control && !modifiers.alt && !modifiers.command { + editor.select( + SelectPhase::Extend { + position, + click_count, + }, + cx, + ); + } else { + editor.select( + SelectPhase::Begin { + position, + add: modifiers.alt, + click_count, + }, + cx, + ); + } + + true + } // fn mouse_right_down( // editor: &mut Editor, @@ -2796,6 +2787,18 @@ impl Element for EditorElement { } } }); + cx.on_mouse_event({ + let position_map = layout.position_map.clone(); + move |editor, event: &MouseDownEvent, phase, cx| { + if phase != DispatchPhase::Bubble { + return; + } + + if Self::mouse_down(editor, event, &position_map, text_bounds, gutter_bounds, cx) { + cx.stop_propagation() + } + } + }); if editor.focus_handle.is_focused(cx) { cx.handle_text_input(); From 1b085bd61800083a61089c6396a20356bcbd6277 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:40:08 +0100 Subject: [PATCH 05/11] Extract mouse event handlers into paint_mouse_listeners Co-authored-by: Antonio --- crates/editor2/src/element.rs | 90 +++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index f83ec86106fd8aeee70910c4a66ec0d78813c6ef..0cda0d38771e9e2a83fae45aa84694e743f280ff 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -2335,6 +2335,52 @@ impl EditorElement { // blocks, // ) // } + + fn paint_mouse_listeners( + &mut self, + bounds: Bounds, + text_bounds: Bounds, + gutter_bounds: Bounds, + position_map: &Arc, + cx: &mut ViewContext, + ) { + cx.on_mouse_event({ + let position_map = position_map.clone(); + move |editor, event: &ScrollWheelEvent, phase, cx| { + if phase != DispatchPhase::Bubble { + return; + } + + if Self::scroll(editor, event, &position_map, bounds, cx) { + cx.stop_propagation(); + } + } + }); + cx.on_mouse_event({ + let position_map = position_map.clone(); + move |editor, event: &MouseMoveEvent, phase, cx| { + if phase != DispatchPhase::Bubble { + return; + } + + if Self::mouse_moved(editor, event, &position_map, text_bounds, cx) { + cx.stop_propagation() + } + } + }); + cx.on_mouse_event({ + let position_map = position_map.clone(); + move |editor, event: &MouseDownEvent, phase, cx| { + if phase != DispatchPhase::Bubble { + return; + } + + if Self::mouse_down(editor, event, &position_map, text_bounds, gutter_bounds, cx) { + cx.stop_propagation() + } + } + }); + } } #[derive(Debug)] @@ -2763,48 +2809,18 @@ impl Element for EditorElement { size: layout.text_size, }; - cx.on_mouse_event({ - let position_map = layout.position_map.clone(); - move |editor, event: &ScrollWheelEvent, phase, cx| { - if phase != DispatchPhase::Bubble { - return; - } - - if Self::scroll(editor, event, &position_map, bounds, cx) { - cx.stop_propagation(); - } - } - }); - cx.on_mouse_event({ - let position_map = layout.position_map.clone(); - move |editor, event: &MouseMoveEvent, phase, cx| { - if phase != DispatchPhase::Bubble { - return; - } - - if Self::mouse_moved(editor, event, &position_map, text_bounds, cx) { - cx.stop_propagation() - } - } - }); - cx.on_mouse_event({ - let position_map = layout.position_map.clone(); - move |editor, event: &MouseDownEvent, phase, cx| { - if phase != DispatchPhase::Bubble { - return; - } - - if Self::mouse_down(editor, event, &position_map, text_bounds, gutter_bounds, cx) { - cx.stop_propagation() - } - } - }); - if editor.focus_handle.is_focused(cx) { cx.handle_text_input(); } cx.with_content_mask(ContentMask { bounds }, |cx| { + self.paint_mouse_listeners( + bounds, + gutter_bounds, + text_bounds, + &layout.position_map, + cx, + ); self.paint_background(gutter_bounds, text_bounds, &layout, cx); if layout.gutter_size.width > Pixels::ZERO { self.paint_gutter(gutter_bounds, &layout, editor, cx); From e5f78ec12292bd55dc33c4cc61d608fb264e6316 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Wed, 8 Nov 2023 15:02:35 +0100 Subject: [PATCH 06/11] Uncomment mouse_dragged event Co-authored-by: Antonio --- crates/editor2/src/element.rs | 143 ++++++++++++---------------------- 1 file changed, 50 insertions(+), 93 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index 0cda0d38771e9e2a83fae45aa84694e743f280ff..fce2634e2bc22178385b65c9ad94352afc630ee7 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -17,7 +17,7 @@ use gpui::{ black, hsla, point, px, relative, size, transparent_black, Action, AnyElement, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchContext, DispatchPhase, Edges, Element, ElementId, Entity, Hsla, KeyDownEvent, KeyListener, KeyMatch, Line, Modifiers, - MouseDownEvent, MouseMoveEvent, Pixels, ScrollWheelEvent, ShapedGlyph, Size, + MouseButton, MouseDownEvent, MouseMoveEvent, Pixels, ScrollWheelEvent, ShapedGlyph, Size, StatefulInteraction, Style, TextRun, TextStyle, TextSystem, ViewContext, WindowContext, }; use itertools::Itertools; @@ -343,103 +343,60 @@ impl EditorElement { // end_selection // } - // fn mouse_dragged( - // editor: &mut Editor, - // MouseMovedEvent { - // modifiers: Modifiers { cmd, shift, .. }, - // position, - // .. - // }: MouseMovedEvent, - // position_map: &PositionMap, - // text_bounds: Bounds, - // cx: &mut EventContext, - // ) -> bool { - // // This will be handled more correctly once https://github.com/zed-industries/zed/issues/1218 is completed - // // Don't trigger hover popover if mouse is hovering over context menu - // let point = if text_bounds.contains_point(position) { - // position_map - // .point_for_position(text_bounds, position) - // .as_valid() - // } else { - // None - // }; - - // update_go_to_definition_link( - // editor, - // point.map(GoToDefinitionTrigger::Text), - // cmd, - // shift, - // cx, - // ); - - // if editor.has_pending_selection() { - // let mut scroll_delta = gpui::Point::::zero(); - - // let vertical_margin = position_map.line_height.min(text_bounds.height() / 3.0); - // let top = text_bounds.origin.y + vertical_margin; - // let bottom = text_bounds.lower_left().y - vertical_margin; - // if position.y < top { - // scroll_delta.set_y(-scale_vertical_mouse_autoscroll_delta(top - position.y)) - // } - // if position.y > bottom { - // scroll_delta.set_y(scale_vertical_mouse_autoscroll_delta(position.y - bottom)) - // } - - // let horizontal_margin = position_map.line_height.min(text_bounds.width() / 3.0); - // let left = text_bounds.origin.x + horizontal_margin; - // let right = text_bounds.upper_right().x - horizontal_margin; - // if position.x < left { - // scroll_delta.set_x(-scale_horizontal_mouse_autoscroll_delta( - // left - position.x, - // )) - // } - // if position.x > right { - // scroll_delta.set_x(scale_horizontal_mouse_autoscroll_delta( - // position.x - right, - // )) - // } - - // let point_for_position = position_map.point_for_position(text_bounds, position); - - // editor.select( - // SelectPhase::Update { - // position: point_for_position.previous_valid, - // goal_column: point_for_position.exact_unclipped.column(), - // scroll_position: (position_map.snapshot.scroll_position() + scroll_delta) - // .clamp(gpui::Point::::zero(), position_map.scroll_max), - // }, - // cx, - // ); - // hover_at(editor, point, cx); - // true - // } else { - // hover_at(editor, point, cx); - // false - // } - // } - fn mouse_moved( editor: &mut Editor, - MouseMoveEvent { - modifiers: Modifiers { shift, command, .. }, - position, - .. - }: &MouseMoveEvent, + event: &MouseMoveEvent, position_map: &PositionMap, text_bounds: Bounds, cx: &mut ViewContext, ) -> bool { + let modifiers = event.modifiers; + if editor.has_pending_selection() && event.pressed_button == Some(MouseButton::Left) { + let point_for_position = position_map.point_for_position(text_bounds, event.position); + let mut scroll_delta = gpui::Point::::zero(); + let vertical_margin = position_map.line_height.min(text_bounds.size.height / 3.0); + let top = text_bounds.origin.y + vertical_margin; + let bottom = text_bounds.lower_left().y - vertical_margin; + if event.position.y < top { + scroll_delta.y = -scale_vertical_mouse_autoscroll_delta(top - event.position.y); + } + if event.position.y > bottom { + scroll_delta.y = scale_vertical_mouse_autoscroll_delta(event.position.y - bottom); + } + + let horizontal_margin = position_map.line_height.min(text_bounds.size.width / 3.0); + let left = text_bounds.origin.x + horizontal_margin; + let right = text_bounds.upper_right().x - horizontal_margin; + if event.position.x < left { + scroll_delta.x = -scale_horizontal_mouse_autoscroll_delta(left - event.position.x); + } + if event.position.x > right { + scroll_delta.x = scale_horizontal_mouse_autoscroll_delta(event.position.x - right); + } + + editor.select( + SelectPhase::Update { + position: point_for_position.previous_valid, + goal_column: point_for_position.exact_unclipped.column(), + scroll_position: (position_map.snapshot.scroll_position() + scroll_delta) + .clamp(&gpui::Point::zero(), &position_map.scroll_max), + }, + cx, + ); + } + // This will be handled more correctly once https://github.com/zed-industries/zed/issues/1218 is completed // Don't trigger hover popover if mouse is hovering over context menu - if text_bounds.contains_point(position) { - let point_for_position = position_map.point_for_position(text_bounds, position.clone()); + if text_bounds.contains_point(&event.position) { + let point_for_position = position_map.point_for_position(text_bounds, event.position); + match point_for_position.as_valid() { Some(point) => { update_go_to_definition_link( editor, Some(GoToDefinitionTrigger::Text(point)), - *command, - *shift, + modifiers.command, + modifiers.shift, cx, ); hover_at(editor, Some(point), cx); @@ -449,14 +406,14 @@ impl EditorElement { &position_map.snapshot, point_for_position, editor, - *command, - *shift, + modifiers.command, + modifiers.shift, cx, ); } } } else { - update_go_to_definition_link(editor, None, *command, *shift, cx); + update_go_to_definition_link(editor, None, modifiers.command, modifiers.shift, cx); hover_at(editor, None, cx); } @@ -2339,8 +2296,8 @@ impl EditorElement { fn paint_mouse_listeners( &mut self, bounds: Bounds, - text_bounds: Bounds, gutter_bounds: Bounds, + text_bounds: Bounds, position_map: &Arc, cx: &mut ViewContext, ) { @@ -3849,12 +3806,12 @@ impl HighlightedRange { // bounds.into_iter() // } -pub fn scale_vertical_mouse_autoscroll_delta(delta: f32) -> f32 { - delta.powf(1.5) / 100.0 +pub fn scale_vertical_mouse_autoscroll_delta(delta: Pixels) -> f32 { + (delta.pow(1.5) / 100.0).into() } -fn scale_horizontal_mouse_autoscroll_delta(delta: f32) -> f32 { - delta.powf(1.2) / 300.0 +fn scale_horizontal_mouse_autoscroll_delta(delta: Pixels) -> f32 { + (delta.pow(1.2) / 300.0).into() } // #[cfg(test)] From bef3b80bd458539661eb9cd4a5b366337fd8c826 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 8 Nov 2023 15:08:54 +0100 Subject: [PATCH 07/11] Clear hover background highlights Co-Authored-By: Piotr --- crates/editor2/src/element.rs | 5 +++-- crates/editor2/src/hover_popover.rs | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index fce2634e2bc22178385b65c9ad94352afc630ee7..2e29793670dd9407649f65777f3ad18f58a62c3a 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -412,12 +412,13 @@ impl EditorElement { ); } } + + true } else { update_go_to_definition_link(editor, None, modifiers.command, modifiers.shift, cx); hover_at(editor, None, cx); + false } - - true } fn scroll( diff --git a/crates/editor2/src/hover_popover.rs b/crates/editor2/src/hover_popover.rs index bee95994e4b858bb2b693d033d5e12ef76a3a273..5c8f403d4f7d768d764f5665f45e31863b00e733 100644 --- a/crates/editor2/src/hover_popover.rs +++ b/crates/editor2/src/hover_popover.rs @@ -144,8 +144,7 @@ pub fn hide_hover(editor: &mut Editor, cx: &mut ViewContext) -> bool { editor.hover_state.info_task = None; editor.hover_state.triggered_from = None; - // todo!() - // editor.clear_background_highlights::(cx); + editor.clear_background_highlights::(cx); if did_hide { cx.notify(); From e500c05880dab35a0348ece16b172fb94d5988e9 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 8 Nov 2023 15:12:54 +0100 Subject: [PATCH 08/11] Move building of key listeners outside of EditorElement::initialize --- crates/editor2/src/element.rs | 354 +++++++++++++++++----------------- 1 file changed, 180 insertions(+), 174 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index 2e29793670dd9407649f65777f3ad18f58a62c3a..f197734a0d4f8d52394aef599dce544e3733a04f 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -16,9 +16,10 @@ use collections::{BTreeMap, HashMap}; use gpui::{ black, hsla, point, px, relative, size, transparent_black, Action, AnyElement, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchContext, DispatchPhase, - Edges, Element, ElementId, Entity, Hsla, KeyDownEvent, KeyListener, KeyMatch, Line, Modifiers, - MouseButton, MouseDownEvent, MouseMoveEvent, Pixels, ScrollWheelEvent, ShapedGlyph, Size, - StatefulInteraction, Style, TextRun, TextStyle, TextSystem, ViewContext, WindowContext, + Edges, Element, ElementId, Entity, GlobalElementId, Hsla, KeyDownEvent, KeyListener, KeyMatch, + Line, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, Pixels, ScrollWheelEvent, + ShapedGlyph, Size, StatefulInteraction, Style, TextRun, TextStyle, TextSystem, ViewContext, + WindowContext, }; use itertools::Itertools; use language::language_settings::ShowWhitespaceSetting; @@ -2558,177 +2559,9 @@ impl Element for EditorElement { let dispatch_context = editor.dispatch_context(cx); cx.with_element_id(cx.view().entity_id(), |global_id, cx| { cx.with_key_dispatch_context(dispatch_context, |cx| { - cx.with_key_listeners( - [ - build_action_listener(Editor::move_left), - build_action_listener(Editor::move_right), - build_action_listener(Editor::move_down), - build_action_listener(Editor::move_up), - // build_action_listener(Editor::new_file), todo!() - // build_action_listener(Editor::new_file_in_direction), todo!() - build_action_listener(Editor::cancel), - build_action_listener(Editor::newline), - build_action_listener(Editor::newline_above), - build_action_listener(Editor::newline_below), - build_action_listener(Editor::backspace), - build_action_listener(Editor::delete), - build_action_listener(Editor::tab), - build_action_listener(Editor::tab_prev), - build_action_listener(Editor::indent), - build_action_listener(Editor::outdent), - build_action_listener(Editor::delete_line), - build_action_listener(Editor::join_lines), - build_action_listener(Editor::sort_lines_case_sensitive), - build_action_listener(Editor::sort_lines_case_insensitive), - build_action_listener(Editor::reverse_lines), - build_action_listener(Editor::shuffle_lines), - build_action_listener(Editor::convert_to_upper_case), - build_action_listener(Editor::convert_to_lower_case), - build_action_listener(Editor::convert_to_title_case), - build_action_listener(Editor::convert_to_snake_case), - build_action_listener(Editor::convert_to_kebab_case), - build_action_listener(Editor::convert_to_upper_camel_case), - build_action_listener(Editor::convert_to_lower_camel_case), - build_action_listener(Editor::delete_to_previous_word_start), - build_action_listener(Editor::delete_to_previous_subword_start), - build_action_listener(Editor::delete_to_next_word_end), - build_action_listener(Editor::delete_to_next_subword_end), - build_action_listener(Editor::delete_to_beginning_of_line), - build_action_listener(Editor::delete_to_end_of_line), - build_action_listener(Editor::cut_to_end_of_line), - build_action_listener(Editor::duplicate_line), - build_action_listener(Editor::move_line_up), - build_action_listener(Editor::move_line_down), - build_action_listener(Editor::transpose), - build_action_listener(Editor::cut), - build_action_listener(Editor::copy), - build_action_listener(Editor::paste), - build_action_listener(Editor::undo), - build_action_listener(Editor::redo), - build_action_listener(Editor::move_page_up), - build_action_listener(Editor::move_page_down), - build_action_listener(Editor::next_screen), - build_action_listener(Editor::scroll_cursor_top), - build_action_listener(Editor::scroll_cursor_center), - build_action_listener(Editor::scroll_cursor_bottom), - build_action_listener(|editor, _: &LineDown, cx| { - editor.scroll_screen(&ScrollAmount::Line(1.), cx) - }), - build_action_listener(|editor, _: &LineUp, cx| { - editor.scroll_screen(&ScrollAmount::Line(-1.), cx) - }), - build_action_listener(|editor, _: &HalfPageDown, cx| { - editor.scroll_screen(&ScrollAmount::Page(0.5), cx) - }), - build_action_listener(|editor, _: &HalfPageUp, cx| { - editor.scroll_screen(&ScrollAmount::Page(-0.5), cx) - }), - build_action_listener(|editor, _: &PageDown, cx| { - editor.scroll_screen(&ScrollAmount::Page(1.), cx) - }), - build_action_listener(|editor, _: &PageUp, cx| { - editor.scroll_screen(&ScrollAmount::Page(-1.), cx) - }), - build_action_listener(Editor::move_to_previous_word_start), - build_action_listener(Editor::move_to_previous_subword_start), - build_action_listener(Editor::move_to_next_word_end), - build_action_listener(Editor::move_to_next_subword_end), - build_action_listener(Editor::move_to_beginning_of_line), - build_action_listener(Editor::move_to_end_of_line), - build_action_listener(Editor::move_to_start_of_paragraph), - build_action_listener(Editor::move_to_end_of_paragraph), - build_action_listener(Editor::move_to_beginning), - build_action_listener(Editor::move_to_end), - build_action_listener(Editor::select_up), - build_action_listener(Editor::select_down), - build_action_listener(Editor::select_left), - build_action_listener(Editor::select_right), - build_action_listener(Editor::select_to_previous_word_start), - build_action_listener(Editor::select_to_previous_subword_start), - build_action_listener(Editor::select_to_next_word_end), - build_action_listener(Editor::select_to_next_subword_end), - build_action_listener(Editor::select_to_beginning_of_line), - build_action_listener(Editor::select_to_end_of_line), - build_action_listener(Editor::select_to_start_of_paragraph), - build_action_listener(Editor::select_to_end_of_paragraph), - build_action_listener(Editor::select_to_beginning), - build_action_listener(Editor::select_to_end), - build_action_listener(Editor::select_all), - build_action_listener(|editor, action, cx| { - editor.select_all_matches(action, cx).log_err(); - }), - build_action_listener(Editor::select_line), - build_action_listener(Editor::split_selection_into_lines), - build_action_listener(Editor::add_selection_above), - build_action_listener(Editor::add_selection_below), - build_action_listener(|editor, action, cx| { - editor.select_next(action, cx).log_err(); - }), - build_action_listener(|editor, action, cx| { - editor.select_previous(action, cx).log_err(); - }), - build_action_listener(Editor::toggle_comments), - build_action_listener(Editor::select_larger_syntax_node), - build_action_listener(Editor::select_smaller_syntax_node), - build_action_listener(Editor::move_to_enclosing_bracket), - build_action_listener(Editor::undo_selection), - build_action_listener(Editor::redo_selection), - build_action_listener(Editor::go_to_diagnostic), - build_action_listener(Editor::go_to_prev_diagnostic), - build_action_listener(Editor::go_to_hunk), - build_action_listener(Editor::go_to_prev_hunk), - build_action_listener(Editor::go_to_definition), - build_action_listener(Editor::go_to_definition_split), - build_action_listener(Editor::go_to_type_definition), - build_action_listener(Editor::go_to_type_definition_split), - build_action_listener(Editor::fold), - build_action_listener(Editor::fold_at), - build_action_listener(Editor::unfold_lines), - build_action_listener(Editor::unfold_at), - // build_action_listener(Editor::gutter_hover), todo!() - build_action_listener(Editor::fold_selected_ranges), - build_action_listener(Editor::show_completions), - // build_action_listener(Editor::toggle_code_actions), todo!() - // build_action_listener(Editor::open_excerpts), todo!() - build_action_listener(Editor::toggle_soft_wrap), - build_action_listener(Editor::toggle_inlay_hints), - build_action_listener(Editor::reveal_in_finder), - build_action_listener(Editor::copy_path), - build_action_listener(Editor::copy_relative_path), - build_action_listener(Editor::copy_highlight_json), - build_action_listener(|editor, action, cx| { - editor - .format(action, cx) - .map(|task| task.detach_and_log_err(cx)); - }), - build_action_listener(Editor::restart_language_server), - build_action_listener(Editor::show_character_palette), - // build_action_listener(Editor::confirm_completion), todo!() - // build_action_listener(Editor::confirm_code_action), todo!() - // build_action_listener(Editor::rename), todo!() - // build_action_listener(Editor::confirm_rename), todo!() - // build_action_listener(Editor::find_all_references), todo!() - build_action_listener(Editor::next_copilot_suggestion), - build_action_listener(Editor::previous_copilot_suggestion), - build_action_listener(Editor::copilot_suggest), - build_key_listener( - move |editor, key_down: &KeyDownEvent, dispatch_context, phase, cx| { - if phase == DispatchPhase::Bubble { - if let KeyMatch::Some(action) = cx.match_keystroke( - &global_id, - &key_down.keystroke, - dispatch_context, - ) { - return Some(action); - } - } - - None - }, - ), - ], - |cx| cx.with_focus(editor.focus_handle.clone(), |_| {}), - ); + cx.with_key_listeners(build_key_listeners(global_id), |cx| { + cx.with_focus(editor.focus_handle.clone(), |_| {}) + }); }) }); } @@ -4258,6 +4091,179 @@ fn scale_horizontal_mouse_autoscroll_delta(delta: Pixels) -> f32 { // } // } +fn build_key_listeners( + global_element_id: GlobalElementId, +) -> impl IntoIterator)> { + [ + build_action_listener(Editor::move_left), + build_action_listener(Editor::move_right), + build_action_listener(Editor::move_down), + build_action_listener(Editor::move_up), + // build_action_listener(Editor::new_file), todo!() + // build_action_listener(Editor::new_file_in_direction), todo!() + build_action_listener(Editor::cancel), + build_action_listener(Editor::newline), + build_action_listener(Editor::newline_above), + build_action_listener(Editor::newline_below), + build_action_listener(Editor::backspace), + build_action_listener(Editor::delete), + build_action_listener(Editor::tab), + build_action_listener(Editor::tab_prev), + build_action_listener(Editor::indent), + build_action_listener(Editor::outdent), + build_action_listener(Editor::delete_line), + build_action_listener(Editor::join_lines), + build_action_listener(Editor::sort_lines_case_sensitive), + build_action_listener(Editor::sort_lines_case_insensitive), + build_action_listener(Editor::reverse_lines), + build_action_listener(Editor::shuffle_lines), + build_action_listener(Editor::convert_to_upper_case), + build_action_listener(Editor::convert_to_lower_case), + build_action_listener(Editor::convert_to_title_case), + build_action_listener(Editor::convert_to_snake_case), + build_action_listener(Editor::convert_to_kebab_case), + build_action_listener(Editor::convert_to_upper_camel_case), + build_action_listener(Editor::convert_to_lower_camel_case), + build_action_listener(Editor::delete_to_previous_word_start), + build_action_listener(Editor::delete_to_previous_subword_start), + build_action_listener(Editor::delete_to_next_word_end), + build_action_listener(Editor::delete_to_next_subword_end), + build_action_listener(Editor::delete_to_beginning_of_line), + build_action_listener(Editor::delete_to_end_of_line), + build_action_listener(Editor::cut_to_end_of_line), + build_action_listener(Editor::duplicate_line), + build_action_listener(Editor::move_line_up), + build_action_listener(Editor::move_line_down), + build_action_listener(Editor::transpose), + build_action_listener(Editor::cut), + build_action_listener(Editor::copy), + build_action_listener(Editor::paste), + build_action_listener(Editor::undo), + build_action_listener(Editor::redo), + build_action_listener(Editor::move_page_up), + build_action_listener(Editor::move_page_down), + build_action_listener(Editor::next_screen), + build_action_listener(Editor::scroll_cursor_top), + build_action_listener(Editor::scroll_cursor_center), + build_action_listener(Editor::scroll_cursor_bottom), + build_action_listener(|editor, _: &LineDown, cx| { + editor.scroll_screen(&ScrollAmount::Line(1.), cx) + }), + build_action_listener(|editor, _: &LineUp, cx| { + editor.scroll_screen(&ScrollAmount::Line(-1.), cx) + }), + build_action_listener(|editor, _: &HalfPageDown, cx| { + editor.scroll_screen(&ScrollAmount::Page(0.5), cx) + }), + build_action_listener(|editor, _: &HalfPageUp, cx| { + editor.scroll_screen(&ScrollAmount::Page(-0.5), cx) + }), + build_action_listener(|editor, _: &PageDown, cx| { + editor.scroll_screen(&ScrollAmount::Page(1.), cx) + }), + build_action_listener(|editor, _: &PageUp, cx| { + editor.scroll_screen(&ScrollAmount::Page(-1.), cx) + }), + build_action_listener(Editor::move_to_previous_word_start), + build_action_listener(Editor::move_to_previous_subword_start), + build_action_listener(Editor::move_to_next_word_end), + build_action_listener(Editor::move_to_next_subword_end), + build_action_listener(Editor::move_to_beginning_of_line), + build_action_listener(Editor::move_to_end_of_line), + build_action_listener(Editor::move_to_start_of_paragraph), + build_action_listener(Editor::move_to_end_of_paragraph), + build_action_listener(Editor::move_to_beginning), + build_action_listener(Editor::move_to_end), + build_action_listener(Editor::select_up), + build_action_listener(Editor::select_down), + build_action_listener(Editor::select_left), + build_action_listener(Editor::select_right), + build_action_listener(Editor::select_to_previous_word_start), + build_action_listener(Editor::select_to_previous_subword_start), + build_action_listener(Editor::select_to_next_word_end), + build_action_listener(Editor::select_to_next_subword_end), + build_action_listener(Editor::select_to_beginning_of_line), + build_action_listener(Editor::select_to_end_of_line), + build_action_listener(Editor::select_to_start_of_paragraph), + build_action_listener(Editor::select_to_end_of_paragraph), + build_action_listener(Editor::select_to_beginning), + build_action_listener(Editor::select_to_end), + build_action_listener(Editor::select_all), + build_action_listener(|editor, action, cx| { + editor.select_all_matches(action, cx).log_err(); + }), + build_action_listener(Editor::select_line), + build_action_listener(Editor::split_selection_into_lines), + build_action_listener(Editor::add_selection_above), + build_action_listener(Editor::add_selection_below), + build_action_listener(|editor, action, cx| { + editor.select_next(action, cx).log_err(); + }), + build_action_listener(|editor, action, cx| { + editor.select_previous(action, cx).log_err(); + }), + build_action_listener(Editor::toggle_comments), + build_action_listener(Editor::select_larger_syntax_node), + build_action_listener(Editor::select_smaller_syntax_node), + build_action_listener(Editor::move_to_enclosing_bracket), + build_action_listener(Editor::undo_selection), + build_action_listener(Editor::redo_selection), + build_action_listener(Editor::go_to_diagnostic), + build_action_listener(Editor::go_to_prev_diagnostic), + build_action_listener(Editor::go_to_hunk), + build_action_listener(Editor::go_to_prev_hunk), + build_action_listener(Editor::go_to_definition), + build_action_listener(Editor::go_to_definition_split), + build_action_listener(Editor::go_to_type_definition), + build_action_listener(Editor::go_to_type_definition_split), + build_action_listener(Editor::fold), + build_action_listener(Editor::fold_at), + build_action_listener(Editor::unfold_lines), + build_action_listener(Editor::unfold_at), + // build_action_listener(Editor::gutter_hover), todo!() + build_action_listener(Editor::fold_selected_ranges), + build_action_listener(Editor::show_completions), + // build_action_listener(Editor::toggle_code_actions), todo!() + // build_action_listener(Editor::open_excerpts), todo!() + build_action_listener(Editor::toggle_soft_wrap), + build_action_listener(Editor::toggle_inlay_hints), + build_action_listener(Editor::reveal_in_finder), + build_action_listener(Editor::copy_path), + build_action_listener(Editor::copy_relative_path), + build_action_listener(Editor::copy_highlight_json), + build_action_listener(|editor, action, cx| { + editor + .format(action, cx) + .map(|task| task.detach_and_log_err(cx)); + }), + build_action_listener(Editor::restart_language_server), + build_action_listener(Editor::show_character_palette), + // build_action_listener(Editor::confirm_completion), todo!() + // build_action_listener(Editor::confirm_code_action), todo!() + // build_action_listener(Editor::rename), todo!() + // build_action_listener(Editor::confirm_rename), todo!() + // build_action_listener(Editor::find_all_references), todo!() + build_action_listener(Editor::next_copilot_suggestion), + build_action_listener(Editor::previous_copilot_suggestion), + build_action_listener(Editor::copilot_suggest), + build_key_listener( + move |editor, key_down: &KeyDownEvent, dispatch_context, phase, cx| { + if phase == DispatchPhase::Bubble { + if let KeyMatch::Some(action) = cx.match_keystroke( + &global_element_id, + &key_down.keystroke, + dispatch_context, + ) { + return Some(action); + } + } + + None + }, + ), + ] +} + fn build_key_listener( listener: impl Fn( &mut Editor, From dfc536b4f5794ec71218868d33b9522ca296ad28 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 8 Nov 2023 15:17:30 +0100 Subject: [PATCH 09/11] Handle MouseUpEvent in editor2 --- crates/editor2/src/element.rs | 85 ++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index f197734a0d4f8d52394aef599dce544e3733a04f..c2ffaad6381b740b29239b9510e96a15e7740b79 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -4,7 +4,8 @@ use crate::{ git::{diff_hunk_to_display, DisplayDiffHunk}, hover_popover::hover_at, link_go_to_definition::{ - update_go_to_definition_link, update_inlay_link_and_hover_points, GoToDefinitionTrigger, + go_to_fetched_definition, go_to_fetched_type_definition, update_go_to_definition_link, + update_inlay_link_and_hover_points, GoToDefinitionTrigger, }, scroll::scroll_amount::ScrollAmount, CursorShape, DisplayPoint, Editor, EditorMode, EditorSettings, EditorSnapshot, EditorStyle, @@ -17,9 +18,9 @@ use gpui::{ black, hsla, point, px, relative, size, transparent_black, Action, AnyElement, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchContext, DispatchPhase, Edges, Element, ElementId, Entity, GlobalElementId, Hsla, KeyDownEvent, KeyListener, KeyMatch, - Line, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, Pixels, ScrollWheelEvent, - ShapedGlyph, Size, StatefulInteraction, Style, TextRun, TextStyle, TextSystem, ViewContext, - WindowContext, + Line, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, + ScrollWheelEvent, ShapedGlyph, Size, StatefulInteraction, Style, TextRun, TextStyle, + TextSystem, ViewContext, WindowContext, }; use itertools::Itertools; use language::language_settings::ShowWhitespaceSetting; @@ -312,37 +313,38 @@ impl EditorElement { // true // } - // fn mouse_up( - // editor: &mut Editor, - // position: gpui::Point, - // cmd: bool, - // shift: bool, - // alt: bool, - // position_map: &PositionMap, - // text_bounds: Bounds, - // cx: &mut EventContext, - // ) -> bool { - // let end_selection = editor.has_pending_selection(); - // let pending_nonempty_selections = editor.has_pending_nonempty_selection(); + fn mouse_up( + editor: &mut Editor, + event: &MouseUpEvent, + position_map: &PositionMap, + text_bounds: Bounds, + cx: &mut ViewContext, + ) -> bool { + let end_selection = editor.has_pending_selection(); + let pending_nonempty_selections = editor.has_pending_nonempty_selection(); - // if end_selection { - // editor.select(SelectPhase::End, cx); - // } + if end_selection { + editor.select(SelectPhase::End, cx); + } - // if !pending_nonempty_selections && cmd && text_bounds.contains_point(position) { - // let point = position_map.point_for_position(text_bounds, position); - // let could_be_inlay = point.as_valid().is_none(); - // if shift || could_be_inlay { - // go_to_fetched_type_definition(editor, point, alt, cx); - // } else { - // go_to_fetched_definition(editor, point, alt, cx); - // } + if !pending_nonempty_selections + && event.modifiers.command + && text_bounds.contains_point(&event.position) + { + let point = position_map.point_for_position(text_bounds, event.position); + let could_be_inlay = point.as_valid().is_none(); + let split = event.modifiers.alt; + if event.modifiers.shift || could_be_inlay { + go_to_fetched_type_definition(editor, point, split, cx); + } else { + go_to_fetched_definition(editor, point, split, cx); + } - // return true; - // } + return true; + } - // end_selection - // } + end_selection + } fn mouse_moved( editor: &mut Editor, @@ -2317,24 +2319,37 @@ impl EditorElement { }); cx.on_mouse_event({ let position_map = position_map.clone(); - move |editor, event: &MouseMoveEvent, phase, cx| { + move |editor, event: &MouseDownEvent, phase, cx| { if phase != DispatchPhase::Bubble { return; } - if Self::mouse_moved(editor, event, &position_map, text_bounds, cx) { + if Self::mouse_down(editor, event, &position_map, text_bounds, gutter_bounds, cx) { cx.stop_propagation() } } }); cx.on_mouse_event({ let position_map = position_map.clone(); - move |editor, event: &MouseDownEvent, phase, cx| { + move |editor, event: &MouseUpEvent, phase, cx| { if phase != DispatchPhase::Bubble { return; } - if Self::mouse_down(editor, event, &position_map, text_bounds, gutter_bounds, cx) { + if Self::mouse_up(editor, event, &position_map, text_bounds, cx) { + cx.stop_propagation() + } + } + }); + + cx.on_mouse_event({ + let position_map = position_map.clone(); + move |editor, event: &MouseMoveEvent, phase, cx| { + if phase != DispatchPhase::Bubble { + return; + } + + if Self::mouse_moved(editor, event, &position_map, text_bounds, cx) { cx.stop_propagation() } } From 15d40d6df5f2cc523b62718da3f28ba7cb443fb0 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 8 Nov 2023 15:30:41 +0100 Subject: [PATCH 10/11] Determine whether the gutter was hovered --- crates/editor2/src/editor.rs | 20 ++++++-------------- crates/editor2/src/element.rs | 12 ++++++++---- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/crates/editor2/src/editor.rs b/crates/editor2/src/editor.rs index cfa615c321e869fdf326bfc723e881b218f6d0dc..8c691b33509bd5930f8b3da6deafc29f3c03e0b9 100644 --- a/crates/editor2/src/editor.rs +++ b/crates/editor2/src/editor.rs @@ -249,11 +249,6 @@ pub struct UnfoldAt { pub buffer_row: u32, } -#[action] -pub struct GutterHover { - pub hovered: bool, -} - #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum InlayId { Suggestion(usize), @@ -8309,15 +8304,12 @@ impl Editor { } } - // todo!() - // pub fn gutter_hover( - // &mut self, - // GutterHover { hovered }: &GutterHover, - // cx: &mut ViewContext, - // ) { - // self.gutter_hovered = *hovered; - // cx.notify(); - // } + pub fn set_gutter_hovered(&mut self, hovered: bool, cx: &mut ViewContext) { + if hovered != self.gutter_hovered { + self.gutter_hovered = hovered; + cx.notify(); + } + } pub fn insert_blocks( &mut self, diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index c2ffaad6381b740b29239b9510e96a15e7740b79..fc2a4ccb92f11b42225729c5f23383a7a4c5e4d3 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -351,6 +351,7 @@ impl EditorElement { event: &MouseMoveEvent, position_map: &PositionMap, text_bounds: Bounds, + gutter_bounds: Bounds, cx: &mut ViewContext, ) -> bool { let modifiers = event.modifiers; @@ -388,9 +389,12 @@ impl EditorElement { ); } - // This will be handled more correctly once https://github.com/zed-industries/zed/issues/1218 is completed + let text_hovered = text_bounds.contains_point(&event.position); + let gutter_hovered = gutter_bounds.contains_point(&event.position); + editor.set_gutter_hovered(gutter_hovered, cx); + // Don't trigger hover popover if mouse is hovering over context menu - if text_bounds.contains_point(&event.position) { + if text_hovered { let point_for_position = position_map.point_for_position(text_bounds, event.position); match point_for_position.as_valid() { @@ -420,7 +424,7 @@ impl EditorElement { } else { update_go_to_definition_link(editor, None, modifiers.command, modifiers.shift, cx); hover_at(editor, None, cx); - false + gutter_hovered } } @@ -2349,7 +2353,7 @@ impl EditorElement { return; } - if Self::mouse_moved(editor, event, &position_map, text_bounds, cx) { + if Self::mouse_moved(editor, event, &position_map, text_bounds, gutter_bounds, cx) { cx.stop_propagation() } } From 55dca1e3e1bae49227b8acc86476a72a74871f76 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 8 Nov 2023 15:33:11 +0100 Subject: [PATCH 11/11] :lipstick: --- crates/editor2/src/element.rs | 148 ++++------------------------------ 1 file changed, 15 insertions(+), 133 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index fc2a4ccb92f11b42225729c5f23383a7a4c5e4d3..34ce39fd8a39404e308784a0162a399332a984f7 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -113,137 +113,6 @@ impl EditorElement { Self { style } } - // fn attach_mouse_handlers( - // position_map: &Arc, - // has_popovers: bool, - // visible_bounds: Bounds, - // text_bounds: Bounds, - // gutter_bounds: Bounds, - // bounds: Bounds, - // cx: &mut ViewContext, - // ) { - // enum EditorElementMouseHandlers {} - // let view_id = cx.view_id(); - // cx.scene().push_mouse_region( - // MouseRegion::new::(view_id, view_id, visible_bounds) - // .on_down(MouseButton::Left, { - // let position_map = position_map.clone(); - // move |event, editor, cx| { - // if !Self::mouse_down( - // editor, - // event.platform_event, - // position_map.as_ref(), - // text_bounds, - // gutter_bounds, - // cx, - // ) { - // cx.propagate_event(); - // } - // } - // }) - // .on_down(MouseButton::Right, { - // let position_map = position_map.clone(); - // move |event, editor, cx| { - // if !Self::mouse_right_down( - // editor, - // event.position, - // position_map.as_ref(), - // text_bounds, - // cx, - // ) { - // cx.propagate_event(); - // } - // } - // }) - // .on_up(MouseButton::Left, { - // let position_map = position_map.clone(); - // move |event, editor, cx| { - // if !Self::mouse_up( - // editor, - // event.position, - // event.cmd, - // event.shift, - // event.alt, - // position_map.as_ref(), - // text_bounds, - // cx, - // ) { - // cx.propagate_event() - // } - // } - // }) - // .on_drag(MouseButton::Left, { - // let position_map = position_map.clone(); - // move |event, editor, cx| { - // if event.end { - // return; - // } - - // if !Self::mouse_dragged( - // editor, - // event.platform_event, - // position_map.as_ref(), - // text_bounds, - // cx, - // ) { - // cx.propagate_event() - // } - // } - // }) - // .on_move({ - // let position_map = position_map.clone(); - // move |event, editor, cx| { - // if !Self::mouse_moved( - // editor, - // event.platform_event, - // &position_map, - // text_bounds, - // cx, - // ) { - // cx.propagate_event() - // } - // } - // }) - // .on_move_out(move |_, editor: &mut Editor, cx| { - // if has_popovers { - // hide_hover(editor, cx); - // } - // }) - // .on_scroll({ - // let position_map = position_map.clone(); - // move |event, editor, cx| { - // if !Self::scroll( - // editor, - // event.position, - // *event.delta.raw(), - // event.delta.precise(), - // &position_map, - // bounds, - // cx, - // ) { - // cx.propagate_event() - // } - // } - // }), - // ); - - // enum GutterHandlers {} - // let view_id = cx.view_id(); - // let region_id = cx.view_id() + 1; - // cx.scene().push_mouse_region( - // MouseRegion::new::(view_id, region_id, gutter_bounds).on_hover( - // |hover, editor: &mut Editor, cx| { - // editor.gutter_hover( - // &GutterHover { - // hovered: hover.started, - // }, - // cx, - // ); - // }, - // ), - // ) - // } - fn mouse_down( editor: &mut Editor, event: &MouseDownEvent, @@ -2345,7 +2214,21 @@ impl EditorElement { } } }); - + // todo!() + // on_down(MouseButton::Right, { + // let position_map = position_map.clone(); + // move |event, editor, cx| { + // if !Self::mouse_right_down( + // editor, + // event.position, + // position_map.as_ref(), + // text_bounds, + // cx, + // ) { + // cx.propagate_event(); + // } + // } + // }); cx.on_mouse_event({ let position_map = position_map.clone(); move |editor, event: &MouseMoveEvent, phase, cx| { @@ -4239,7 +4122,6 @@ fn build_key_listeners( build_action_listener(Editor::fold_at), build_action_listener(Editor::unfold_lines), build_action_listener(Editor::unfold_at), - // build_action_listener(Editor::gutter_hover), todo!() build_action_listener(Editor::fold_selected_ranges), build_action_listener(Editor::show_completions), // build_action_listener(Editor::toggle_code_actions), todo!()