Move handling of modifier changes to new View hook

This commit is contained in:
Nathan Sobo 2022-10-16 12:47:48 -06:00
parent 6e363e464c
commit b9308ad80d
4 changed files with 57 additions and 77 deletions

View file

@ -46,7 +46,9 @@ use language::{
DiagnosticSeverity, IndentKind, IndentSize, Language, OffsetRangeExt, OffsetUtf16, Point, DiagnosticSeverity, IndentKind, IndentSize, Language, OffsetRangeExt, OffsetUtf16, Point,
Selection, SelectionGoal, TransactionId, Selection, SelectionGoal, TransactionId,
}; };
use link_go_to_definition::{hide_link_definition, LinkGoToDefinitionState}; use link_go_to_definition::{
hide_link_definition, show_link_definition, LinkDefinitionKind, LinkGoToDefinitionState,
};
pub use multi_buffer::{ pub use multi_buffer::{
Anchor, AnchorRangeExt, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, ToOffset, Anchor, AnchorRangeExt, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, ToOffset,
ToPoint, ToPoint,
@ -6510,6 +6512,44 @@ impl View for Editor {
cx.notify(); cx.notify();
} }
fn modifiers_changed(
&mut self,
event: &gpui::ModifiersChangedEvent,
cx: &mut ViewContext<Self>,
) -> bool {
let pending_selection = self.has_pending_selection();
if let Some(point) = self.link_go_to_definition_state.last_mouse_location.clone() {
if event.cmd && !pending_selection {
let snapshot = self.snapshot(cx);
let kind = if event.shift {
LinkDefinitionKind::Type
} else {
LinkDefinitionKind::Symbol
};
show_link_definition(kind, self, point, snapshot, cx);
return false;
}
}
{
if self.link_go_to_definition_state.symbol_range.is_some()
|| !self.link_go_to_definition_state.definitions.is_empty()
{
self.link_go_to_definition_state.symbol_range.take();
self.link_go_to_definition_state.definitions.clear();
cx.notify();
}
self.link_go_to_definition_state.task = None;
self.clear_text_highlights::<LinkGoToDefinitionState>(cx);
}
false
}
fn keymap_context(&self, _: &AppContext) -> gpui::keymap::Context { fn keymap_context(&self, _: &AppContext) -> gpui::keymap::Context {
let mut context = Self::default_keymap_context(); let mut context = Self::default_keymap_context();
let mode = match self.mode { let mode = match self.mode {

View file

@ -9,7 +9,7 @@ use crate::{
HoverAt, HOVER_POPOVER_GAP, MIN_POPOVER_CHARACTER_WIDTH, MIN_POPOVER_LINE_HEIGHT, HoverAt, HOVER_POPOVER_GAP, MIN_POPOVER_CHARACTER_WIDTH, MIN_POPOVER_LINE_HEIGHT,
}, },
link_go_to_definition::{ link_go_to_definition::{
CmdShiftChanged, GoToFetchedDefinition, GoToFetchedTypeDefinition, UpdateGoToDefinitionLink, GoToFetchedDefinition, GoToFetchedTypeDefinition, UpdateGoToDefinitionLink,
}, },
mouse_context_menu::DeployMouseContextMenu, mouse_context_menu::DeployMouseContextMenu,
EditorStyle, EditorStyle,
@ -30,9 +30,8 @@ use gpui::{
platform::CursorStyle, platform::CursorStyle,
text_layout::{self, Line, RunStyle, TextLayoutCache}, text_layout::{self, Line, RunStyle, TextLayoutCache},
AppContext, Axis, Border, CursorRegion, Element, ElementBox, Event, EventContext, AppContext, Axis, Border, CursorRegion, Element, ElementBox, Event, EventContext,
LayoutContext, ModifiersChangedEvent, MouseButton, MouseButtonEvent, MouseMovedEvent, LayoutContext, MouseButton, MouseButtonEvent, MouseMovedEvent, MouseRegion, MutableAppContext,
MouseRegion, MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle,
WeakViewHandle,
}; };
use json::json; use json::json;
use language::{Bias, DiagnosticSeverity, OffsetUtf16, Selection}; use language::{Bias, DiagnosticSeverity, OffsetUtf16, Selection};
@ -408,14 +407,6 @@ impl EditorElement {
true true
} }
fn modifiers_changed(&self, event: ModifiersChangedEvent, cx: &mut EventContext) -> bool {
cx.dispatch_action(CmdShiftChanged {
cmd_down: event.cmd,
shift_down: event.shift,
});
false
}
fn scroll( fn scroll(
position: Vector2F, position: Vector2F,
mut delta: Vector2F, mut delta: Vector2F,
@ -1889,17 +1880,13 @@ impl Element for EditorElement {
fn dispatch_event( fn dispatch_event(
&mut self, &mut self,
event: &Event, _: &Event,
_: RectF, _: RectF,
_: RectF, _: RectF,
_: &mut LayoutState, _: &mut LayoutState,
_: &mut (), _: &mut (),
cx: &mut EventContext, _: &mut EventContext,
) -> bool { ) -> bool {
if let Event::ModifiersChanged(event) = event {
self.modifiers_changed(*event, cx);
}
false false
} }

View file

@ -19,12 +19,6 @@ pub struct UpdateGoToDefinitionLink {
pub shift_held: bool, pub shift_held: bool,
} }
#[derive(Clone, PartialEq)]
pub struct CmdShiftChanged {
pub cmd_down: bool,
pub shift_down: bool,
}
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct GoToFetchedDefinition { pub struct GoToFetchedDefinition {
pub point: DisplayPoint, pub point: DisplayPoint,
@ -39,7 +33,6 @@ impl_internal_actions!(
editor, editor,
[ [
UpdateGoToDefinitionLink, UpdateGoToDefinitionLink,
CmdShiftChanged,
GoToFetchedDefinition, GoToFetchedDefinition,
GoToFetchedTypeDefinition GoToFetchedTypeDefinition
] ]
@ -47,7 +40,6 @@ impl_internal_actions!(
pub fn init(cx: &mut MutableAppContext) { pub fn init(cx: &mut MutableAppContext) {
cx.add_action(update_go_to_definition_link); cx.add_action(update_go_to_definition_link);
cx.add_action(cmd_shift_changed);
cx.add_action(go_to_fetched_definition); cx.add_action(go_to_fetched_definition);
cx.add_action(go_to_fetched_type_definition); cx.add_action(go_to_fetched_type_definition);
} }
@ -113,37 +105,6 @@ pub fn update_go_to_definition_link(
hide_link_definition(editor, cx); hide_link_definition(editor, cx);
} }
pub fn cmd_shift_changed(
editor: &mut Editor,
&CmdShiftChanged {
cmd_down,
shift_down,
}: &CmdShiftChanged,
cx: &mut ViewContext<Editor>,
) {
let pending_selection = editor.has_pending_selection();
if let Some(point) = editor
.link_go_to_definition_state
.last_mouse_location
.clone()
{
if cmd_down && !pending_selection {
let snapshot = editor.snapshot(cx);
let kind = if shift_down {
LinkDefinitionKind::Type
} else {
LinkDefinitionKind::Symbol
};
show_link_definition(kind, editor, point, snapshot, cx);
return;
}
}
hide_link_definition(editor, cx)
}
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum LinkDefinitionKind { pub enum LinkDefinitionKind {
Symbol, Symbol,
@ -397,6 +358,7 @@ fn go_to_fetched_definition_of_kind(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use futures::StreamExt; use futures::StreamExt;
use gpui::{ModifiersChangedEvent, View};
use indoc::indoc; use indoc::indoc;
use lsp::request::{GotoDefinition, GotoTypeDefinition}; use lsp::request::{GotoDefinition, GotoTypeDefinition};
@ -467,11 +429,10 @@ mod tests {
// Unpress shift causes highlight to go away (normal goto-definition is not valid here) // Unpress shift causes highlight to go away (normal goto-definition is not valid here)
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
cmd_shift_changed( editor.modifiers_changed(
editor, &gpui::ModifiersChangedEvent {
&CmdShiftChanged { cmd: true,
cmd_down: true, ..Default::default()
shift_down: false,
}, },
cx, cx,
); );
@ -581,14 +542,7 @@ mod tests {
// Unpress cmd causes highlight to go away // Unpress cmd causes highlight to go away
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
cmd_shift_changed( editor.modifiers_changed(&Default::default(), cx);
editor,
&CmdShiftChanged {
cmd_down: false,
shift_down: false,
},
cx,
);
}); });
// Assert no link highlights // Assert no link highlights
@ -704,11 +658,10 @@ mod tests {
]))) ])))
}); });
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
cmd_shift_changed( editor.modifiers_changed(
editor, &ModifiersChangedEvent {
&CmdShiftChanged { cmd: true,
cmd_down: true, ..Default::default()
shift_down: false,
}, },
cx, cx,
); );

View file

@ -11,7 +11,7 @@ pub struct KeyUpEvent {
pub keystroke: Keystroke, pub keystroke: Keystroke,
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, Default)]
pub struct ModifiersChangedEvent { pub struct ModifiersChangedEvent {
pub ctrl: bool, pub ctrl: bool,
pub alt: bool, pub alt: bool,