diff options
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/application.rs | 7 | ||||
-rw-r--r-- | helix-term/src/config.rs | 14 | ||||
-rw-r--r-- | helix-term/src/ui/editor.rs | 68 |
3 files changed, 86 insertions, 3 deletions
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index c55d4c98..5f350671 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -15,7 +15,7 @@ use std::{ use anyhow::Error; use crossterm::{ - event::{Event, EventStream}, + event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream}, execute, terminal, }; @@ -449,6 +449,9 @@ impl Application { let mut stdout = stdout(); execute!(stdout, terminal::EnterAlternateScreen)?; + if self.config.terminal.mouse { + execute!(stdout, EnableMouseCapture)?; + } // Exit the alternate screen and disable raw mode before panicking let hook = std::panic::take_hook(); @@ -456,6 +459,7 @@ impl Application { // We can't handle errors properly inside this closure. And it's // probably not a good idea to `unwrap()` inside a panic handler. // So we just ignore the `Result`s. + let _ = execute!(std::io::stdout(), DisableMouseCapture); let _ = execute!(std::io::stdout(), terminal::LeaveAlternateScreen); let _ = terminal::disable_raw_mode(); hook(info); @@ -468,6 +472,7 @@ impl Application { // reset cursor shape write!(stdout, "\x1B[2 q")?; + execute!(stdout, DisableMouseCapture)?; execute!(stdout, terminal::LeaveAlternateScreen)?; terminal::disable_raw_mode()?; diff --git a/helix-term/src/config.rs b/helix-term/src/config.rs index f3f0ba53..38cd3bfb 100644 --- a/helix-term/src/config.rs +++ b/helix-term/src/config.rs @@ -9,6 +9,8 @@ pub struct Config { pub lsp: LspConfig, #[serde(default)] pub keys: Keymaps, + #[serde(default)] + pub terminal: TerminalConfig, } #[derive(Debug, Default, Clone, PartialEq, Deserialize)] @@ -17,6 +19,18 @@ pub struct LspConfig { pub display_messages: bool, } +#[derive(Debug, Clone, PartialEq, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct TerminalConfig { + pub mouse: bool, +} + +impl Default for TerminalConfig { + fn default() -> Self { + Self { mouse: true } + } +} + #[test] fn parsing_keymaps_config_file() { use crate::keymap; diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index d5c907b8..ec5687bd 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -12,7 +12,7 @@ use helix_core::{ syntax::{self, HighlightEvent}, unicode::segmentation::UnicodeSegmentation, unicode::width::UnicodeWidthStr, - LineEnding, Position, Range, + LineEnding, Position, Range, Selection, }; use helix_view::{ document::Mode, @@ -24,7 +24,7 @@ use helix_view::{ }; use std::borrow::Cow; -use crossterm::event::Event; +use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind}; use tui::buffer::Buffer as Surface; pub struct EditorView { @@ -805,6 +805,70 @@ impl Component for EditorView { EventResult::Consumed(callback) } + Event::Mouse(MouseEvent { + kind: MouseEventKind::Down(MouseButton::Left), + row, + column, + modifiers, + .. + }) => { + let editor = &mut cx.editor; + + let result = editor.tree.views().find_map(|(view, _focus)| { + view.pos_at_screen_coords( + &editor.documents[view.doc], + row as usize, + column as usize, + ) + .map(|pos| (pos, view.id)) + }); + + if let Some((pos, id)) = result { + let doc = &mut editor.documents[editor.tree.get(id).doc]; + let jump = (doc.id(), doc.selection(id).clone()); + editor.tree.get_mut(id).jumps.push(jump); + + if modifiers == crossterm::event::KeyModifiers::ALT { + let selection = doc.selection(id).clone(); + doc.set_selection(id, selection.push(Range::point(pos))); + } else { + doc.set_selection(id, Selection::point(pos)); + } + + editor.tree.focus = id; + + return EventResult::Consumed(None); + } + + EventResult::Ignored + } + + Event::Mouse(MouseEvent { + kind: MouseEventKind::Drag(MouseButton::Left), + row, + column, + .. + }) => { + let (view, doc) = current!(cx.editor); + + let pos = view.pos_at_screen_coords(doc, row as usize, column as usize); + + if pos == None { + return EventResult::Ignored; + } + + let selection = doc.selection(view.id).clone(); + let primary_anchor = selection.primary().anchor; + let new_selection = selection.transform(|range| -> Range { + if range.anchor == primary_anchor { + return Range::new(primary_anchor, pos.unwrap()); + } + range + }); + + doc.set_selection(view.id, new_selection); + EventResult::Consumed(None) + } Event::Mouse(_) => EventResult::Ignored, } } |