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,
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::{
Anchor, AnchorRangeExt, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, ToOffset,
ToPoint,
@ -6510,6 +6512,44 @@ impl View for Editor {
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 {
let mut context = Self::default_keymap_context();
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,
},
link_go_to_definition::{
CmdShiftChanged, GoToFetchedDefinition, GoToFetchedTypeDefinition, UpdateGoToDefinitionLink,
GoToFetchedDefinition, GoToFetchedTypeDefinition, UpdateGoToDefinitionLink,
},
mouse_context_menu::DeployMouseContextMenu,
EditorStyle,
@ -30,9 +30,8 @@ use gpui::{
platform::CursorStyle,
text_layout::{self, Line, RunStyle, TextLayoutCache},
AppContext, Axis, Border, CursorRegion, Element, ElementBox, Event, EventContext,
LayoutContext, ModifiersChangedEvent, MouseButton, MouseButtonEvent, MouseMovedEvent,
MouseRegion, MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext,
WeakViewHandle,
LayoutContext, MouseButton, MouseButtonEvent, MouseMovedEvent, MouseRegion, MutableAppContext,
PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle,
};
use json::json;
use language::{Bias, DiagnosticSeverity, OffsetUtf16, Selection};
@ -408,14 +407,6 @@ impl EditorElement {
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(
position: Vector2F,
mut delta: Vector2F,
@ -1889,17 +1880,13 @@ impl Element for EditorElement {
fn dispatch_event(
&mut self,
event: &Event,
_: &Event,
_: RectF,
_: RectF,
_: &mut LayoutState,
_: &mut (),
cx: &mut EventContext,
_: &mut EventContext,
) -> bool {
if let Event::ModifiersChanged(event) = event {
self.modifiers_changed(*event, cx);
}
false
}

View file

@ -19,12 +19,6 @@ pub struct UpdateGoToDefinitionLink {
pub shift_held: bool,
}
#[derive(Clone, PartialEq)]
pub struct CmdShiftChanged {
pub cmd_down: bool,
pub shift_down: bool,
}
#[derive(Clone, PartialEq)]
pub struct GoToFetchedDefinition {
pub point: DisplayPoint,
@ -39,7 +33,6 @@ impl_internal_actions!(
editor,
[
UpdateGoToDefinitionLink,
CmdShiftChanged,
GoToFetchedDefinition,
GoToFetchedTypeDefinition
]
@ -47,7 +40,6 @@ impl_internal_actions!(
pub fn init(cx: &mut MutableAppContext) {
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_type_definition);
}
@ -113,37 +105,6 @@ pub fn update_go_to_definition_link(
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)]
pub enum LinkDefinitionKind {
Symbol,
@ -397,6 +358,7 @@ fn go_to_fetched_definition_of_kind(
#[cfg(test)]
mod tests {
use futures::StreamExt;
use gpui::{ModifiersChangedEvent, View};
use indoc::indoc;
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)
cx.update_editor(|editor, cx| {
cmd_shift_changed(
editor,
&CmdShiftChanged {
cmd_down: true,
shift_down: false,
editor.modifiers_changed(
&gpui::ModifiersChangedEvent {
cmd: true,
..Default::default()
},
cx,
);
@ -581,14 +542,7 @@ mod tests {
// Unpress cmd causes highlight to go away
cx.update_editor(|editor, cx| {
cmd_shift_changed(
editor,
&CmdShiftChanged {
cmd_down: false,
shift_down: false,
},
cx,
);
editor.modifiers_changed(&Default::default(), cx);
});
// Assert no link highlights
@ -704,11 +658,10 @@ mod tests {
])))
});
cx.update_editor(|editor, cx| {
cmd_shift_changed(
editor,
&CmdShiftChanged {
cmd_down: true,
shift_down: false,
editor.modifiers_changed(
&ModifiersChangedEvent {
cmd: true,
..Default::default()
},
cx,
);

View file

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