@@ -9,6 +9,7 @@ use crate::{
9use helix_core::{
9use helix_core::{
10 coords_at_pos,
10 coords_at_pos,
11 graphemes::{ensure_grapheme_boundary_next, next_grapheme_boundary, prev_grapheme_boundary},
11 graphemes::{ensure_grapheme_boundary_next, next_grapheme_boundary, prev_grapheme_boundary},
12 syntax::{self, HighlightEvent},
13 syntax::{self, HighlightEvent},
13 unicode::segmentation::UnicodeSegmentation,
14 unicode::segmentation::UnicodeSegmentation,
14 unicode::width::UnicodeWidthStr,
15 unicode::width::UnicodeWidthStr,
@@ -694,8 +695,112 @@ impl EditorView {
699+ fn handle_mouse_event(
702+ cxt: &mut commands::Context,
706+ kind: MouseEventKind::Down(MouseButton::Left),
712+ let editor = &mut cxt.editor;
714+ let result = editor.tree.views().find_map(|(view, _focus)| {
715+ view.pos_at_screen_coords(&editor.documents[view.doc], row, column)
716+ .map(|pos| (pos, view.id))
719+ if let Some((pos, view_id)) = result {
720+ let doc = &mut editor.documents[editor.tree.get(view_id).doc];
722+ if modifiers == crossterm::event::KeyModifiers::ALT {
723+ let selection = doc.selection(view_id).clone();
724+ doc.set_selection(view_id, selection.push(Range::point(pos)));
726+ doc.set_selection(view_id, Selection::point(pos));
729+ editor.tree.focus = view_id;
731+ return EventResult::Consumed(None);
734+ EventResult::Ignored
738+ kind: MouseEventKind::Drag(MouseButton::Left),
743+ let (view, doc) = current!(cxt.editor);
745+ let pos = match view.pos_at_screen_coords(doc, row, column) {
747+ None => return EventResult::Ignored,
750+ let mut selection = doc.selection(view.id).clone();
751+ let primary = selection.primary_mut();
752+ *primary = Range::new(primary.anchor, pos);
753+ doc.set_selection(view.id, selection);
754+ EventResult::Consumed(None)
758+ kind: MouseEventKind::ScrollUp | MouseEventKind::ScrollDown,
763+ let current_view = cxt.editor.tree.focus;
765+ let direction = match event.kind {
766+ MouseEventKind::ScrollUp => Direction::Backward,
767+ MouseEventKind::ScrollDown => Direction::Forward,
768+ _ => unreachable!(),
771+ let result = cxt.editor.tree.views().find_map(|(view, _focus)| {
772+ view.pos_at_screen_coords(&cxt.editor.documents[view.doc], row, column)
777+ Some(view_id) => cxt.editor.tree.focus = view_id,
778+ None => return EventResult::Ignored,
781+ let offset = cxt.editor.config.scroll_lines.abs() as usize;
782+ commands::scroll(cxt, offset, direction);
784+ cxt.editor.tree.focus = current_view;
786+ EventResult::Consumed(None)
788+ _ => EventResult::Ignored,
697impl Component for EditorView {
793impl Component for EditorView {
698 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
794 fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
795+ let mut cxt = commands::Context {
796+ selected_register: helix_view::RegisterSelection::default(),
797+ editor: &mut cx.editor,
800+ on_next_key_callback: None,
699 match event {
804 match event {
700 Event::Resize(_width, _height) => {
805 Event::Resize(_width, _height) => {
701 // Ignore this event, we handle resizing just before rendering to screen.
806 // Ignore this event, we handle resizing just before rendering to screen.
@@ -706,20 +811,11 @@ impl Component for EditorView {
706 let mut key = KeyEvent::from(key);
811 let mut key = KeyEvent::from(key);
707 canonicalize_key(&mut key);
812 canonicalize_key(&mut key);
708 // clear status
813 // clear status
709− cx.editor.status_msg = None;
814+ cxt.editor.status_msg = None;
711− let (_, doc) = current!(cx.editor);
816+ let (_, doc) = current!(cxt.editor);
712 let mode = doc.mode();
817 let mode = doc.mode();
714− let mut cxt = commands::Context {
715− selected_register: helix_view::RegisterSelection::default(),
716− editor: &mut cx.editor,
719− on_next_key_callback: None,
723 if let Some(on_next_key) = self.on_next_key.take() {
819 if let Some(on_next_key) = self.on_next_key.take() {
724 // if there's a command waiting input, do that first
820 // if there's a command waiting input, do that first
725 on_next_key(&mut cxt, key);
821 on_next_key(&mut cxt, key);
@@ -773,12 +869,12 @@ impl Component for EditorView {
774 // if the command consumed the last view, skip the render.
870 // if the command consumed the last view, skip the render.
775 // on the next loop cycle the Application will then terminate.
871 // on the next loop cycle the Application will then terminate.
776− if cx.editor.should_close() {
872+ if cxt.editor.should_close() {
777 return EventResult::Ignored;
873 return EventResult::Ignored;
780− let (view, doc) = current!(cx.editor);
876+ let (view, doc) = current!(cxt.editor);
781− view.ensure_cursor_in_view(doc, cx.editor.config.scrolloff);
877+ view.ensure_cursor_in_view(doc, cxt.editor.config.scrolloff);
783 // mode transitions
879 // mode transitions
784 match (mode, doc.mode()) {
880 match (mode, doc.mode()) {
@@ -805,58 +901,8 @@ impl Component for EditorView {
806 EventResult::Consumed(callback)
902 EventResult::Consumed(callback)
808− Event::Mouse(MouseEvent {
809− kind: MouseEventKind::Down(MouseButton::Left),
815− let editor = &mut cx.editor;
817− let result = editor.tree.views().find_map(|(view, _focus)| {
818− view.pos_at_screen_coords(&editor.documents[view.doc], row, column)
819− .map(|pos| (pos, view.id))
822− if let Some((pos, view_id)) = result {
823− let doc = &mut editor.documents[editor.tree.get(view_id).doc];
825− if modifiers == crossterm::event::KeyModifiers::ALT {
826− let selection = doc.selection(view_id).clone();
827− doc.set_selection(view_id, selection.push(Range::point(pos)));
829− doc.set_selection(view_id, Selection::point(pos));
832− editor.tree.focus = view_id;
834− return EventResult::Consumed(None);
837− EventResult::Ignored
840− Event::Mouse(MouseEvent {
905+ Event::Mouse(event) => self.handle_mouse_event(event, &mut cxt),
841− kind: MouseEventKind::Drag(MouseButton::Left),
846− let (view, doc) = current!(cx.editor);
848− let pos = match view.pos_at_screen_coords(doc, row, column) {
850− None => return EventResult::Ignored,
853− let mut selection = doc.selection(view.id).clone();
854− let primary = selection.primary_mut();
855− *primary = Range::new(primary.anchor, pos);
856− doc.set_selection(view.id, selection);
857− EventResult::Consumed(None)
859− Event::Mouse(_) => EventResult::Ignored,