mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-22 17:36:19 +04:00
Refactor handling of mouse events (#2893)
- Simplified match statements by destructuring MouseEvent struct at the top and then matching on event.kind. - Extracted out closures for calculating (1) position and view of mouse click and (2) gutter coordinates and view of mouse click.
This commit is contained in:
parent
23ce5f1837
commit
3108a11d35
@ -1051,22 +1051,33 @@ fn handle_mouse_event(
|
||||
cxt: &mut commands::Context,
|
||||
) -> EventResult {
|
||||
let config = cxt.editor.config();
|
||||
match event {
|
||||
MouseEvent {
|
||||
kind: MouseEventKind::Down(MouseButton::Left),
|
||||
let MouseEvent {
|
||||
kind,
|
||||
row,
|
||||
column,
|
||||
modifiers,
|
||||
..
|
||||
} => {
|
||||
let editor = &mut cxt.editor;
|
||||
} = event;
|
||||
|
||||
let result = editor.tree.views().find_map(|(view, _focus)| {
|
||||
let pos_and_view = |editor: &Editor, row, column| {
|
||||
editor.tree.views().find_map(|(view, _focus)| {
|
||||
view.pos_at_screen_coords(&editor.documents[&view.doc], row, column)
|
||||
.map(|pos| (pos, view.id))
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
if let Some((pos, view_id)) = result {
|
||||
let gutter_coords_and_view = |editor: &Editor, row, column| {
|
||||
editor.tree.views().find_map(|(view, _focus)| {
|
||||
view.gutter_coords_at_screen_coords(row, column)
|
||||
.map(|coords| (coords, view.id))
|
||||
})
|
||||
};
|
||||
|
||||
match kind {
|
||||
MouseEventKind::Down(MouseButton::Left) => {
|
||||
let editor = &mut cxt.editor;
|
||||
|
||||
if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
|
||||
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
|
||||
|
||||
if modifiers == crossterm::event::KeyModifiers::ALT {
|
||||
@ -1081,12 +1092,7 @@ fn handle_mouse_event(
|
||||
return EventResult::Consumed(None);
|
||||
}
|
||||
|
||||
let result = editor.tree.views().find_map(|(view, _focus)| {
|
||||
view.gutter_coords_at_screen_coords(row, column)
|
||||
.map(|coords| (coords, view.id))
|
||||
});
|
||||
|
||||
if let Some((coords, view_id)) = result {
|
||||
if let Some((coords, view_id)) = gutter_coords_and_view(editor, row, column) {
|
||||
editor.tree.focus = view_id;
|
||||
|
||||
let view = editor.tree.get(view_id);
|
||||
@ -1107,12 +1113,7 @@ fn handle_mouse_event(
|
||||
EventResult::Ignored(None)
|
||||
}
|
||||
|
||||
MouseEvent {
|
||||
kind: MouseEventKind::Drag(MouseButton::Left),
|
||||
row,
|
||||
column,
|
||||
..
|
||||
} => {
|
||||
MouseEventKind::Drag(MouseButton::Left) => {
|
||||
let (view, doc) = current!(cxt.editor);
|
||||
|
||||
let pos = match view.pos_at_screen_coords(doc, row, column) {
|
||||
@ -1124,15 +1125,11 @@ fn handle_mouse_event(
|
||||
let primary = selection.primary_mut();
|
||||
*primary = primary.put_cursor(doc.text().slice(..), pos, true);
|
||||
doc.set_selection(view.id, selection);
|
||||
|
||||
EventResult::Consumed(None)
|
||||
}
|
||||
|
||||
MouseEvent {
|
||||
kind: MouseEventKind::ScrollUp | MouseEventKind::ScrollDown,
|
||||
row,
|
||||
column,
|
||||
..
|
||||
} => {
|
||||
MouseEventKind::ScrollUp | MouseEventKind::ScrollDown => {
|
||||
let current_view = cxt.editor.tree.focus;
|
||||
|
||||
let direction = match event.kind {
|
||||
@ -1141,13 +1138,8 @@ fn handle_mouse_event(
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let result = cxt.editor.tree.views().find_map(|(view, _focus)| {
|
||||
view.pos_at_screen_coords(&cxt.editor.documents[&view.doc], row, column)
|
||||
.map(|_| view.id)
|
||||
});
|
||||
|
||||
match result {
|
||||
Some(view_id) => cxt.editor.tree.focus = view_id,
|
||||
match pos_and_view(cxt.editor, row, column) {
|
||||
Some((_, view_id)) => cxt.editor.tree.focus = view_id,
|
||||
None => return EventResult::Ignored(None),
|
||||
}
|
||||
|
||||
@ -1159,10 +1151,7 @@ fn handle_mouse_event(
|
||||
EventResult::Consumed(None)
|
||||
}
|
||||
|
||||
MouseEvent {
|
||||
kind: MouseEventKind::Up(MouseButton::Left),
|
||||
..
|
||||
} => {
|
||||
MouseEventKind::Up(MouseButton::Left) => {
|
||||
if !config.middle_click_paste {
|
||||
return EventResult::Ignored(None);
|
||||
}
|
||||
@ -1184,19 +1173,8 @@ fn handle_mouse_event(
|
||||
EventResult::Consumed(None)
|
||||
}
|
||||
|
||||
MouseEvent {
|
||||
kind: MouseEventKind::Up(MouseButton::Right),
|
||||
row,
|
||||
column,
|
||||
modifiers,
|
||||
..
|
||||
} => {
|
||||
let result = cxt.editor.tree.views().find_map(|(view, _focus)| {
|
||||
view.gutter_coords_at_screen_coords(row, column)
|
||||
.map(|coords| (coords, view.id))
|
||||
});
|
||||
|
||||
if let Some((coords, view_id)) = result {
|
||||
MouseEventKind::Up(MouseButton::Right) => {
|
||||
if let Some((coords, view_id)) = gutter_coords_and_view(cxt.editor, row, column) {
|
||||
cxt.editor.tree.focus = view_id;
|
||||
|
||||
let view = cxt.editor.tree.get(view_id);
|
||||
@ -1213,16 +1191,11 @@ fn handle_mouse_event(
|
||||
return EventResult::Consumed(None);
|
||||
}
|
||||
}
|
||||
|
||||
EventResult::Ignored(None)
|
||||
}
|
||||
|
||||
MouseEvent {
|
||||
kind: MouseEventKind::Up(MouseButton::Middle),
|
||||
row,
|
||||
column,
|
||||
modifiers,
|
||||
..
|
||||
} => {
|
||||
MouseEventKind::Up(MouseButton::Middle) => {
|
||||
let editor = &mut cxt.editor;
|
||||
if !config.middle_click_paste {
|
||||
return EventResult::Ignored(None);
|
||||
@ -1235,16 +1208,12 @@ fn handle_mouse_event(
|
||||
return EventResult::Consumed(None);
|
||||
}
|
||||
|
||||
let result = editor.tree.views().find_map(|(view, _focus)| {
|
||||
view.pos_at_screen_coords(&editor.documents[&view.doc], row, column)
|
||||
.map(|pos| (pos, view.id))
|
||||
});
|
||||
|
||||
if let Some((pos, view_id)) = result {
|
||||
if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
|
||||
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
|
||||
doc.set_selection(view_id, Selection::point(pos));
|
||||
editor.tree.focus = view_id;
|
||||
commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
|
||||
|
||||
return EventResult::Consumed(None);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user