Skip to main content

ratatui_code_editor/
editor_crossterm.rs

1use crate::actions::*;
2use crate::editor::Editor;
3use crate::selection::SelectionSnap;
4use anyhow::Result;
5use crossterm::event::{KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
6use ratatui_core::layout::Rect;
7
8impl Editor {
9    pub fn input(&mut self, key: KeyEvent, area: &Rect) -> Result<()> {
10        use crossterm::event::KeyCode;
11
12        let shift = key.modifiers.contains(KeyModifiers::SHIFT);
13        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
14        let _alt = key.modifiers.contains(KeyModifiers::ALT);
15
16        match key.code {
17            KeyCode::Char('รท') => self.apply(ToggleComment {}),
18            KeyCode::Char('z') if ctrl => self.apply(Undo {}),
19            KeyCode::Char('y') if ctrl => self.apply(Redo {}),
20            KeyCode::Char('c') if ctrl => self.apply(Copy {}),
21            KeyCode::Char('v') if ctrl => self.apply(Paste {}),
22            KeyCode::Char('x') if ctrl => self.apply(Cut {}),
23            KeyCode::Char('k') if ctrl => self.apply(DeleteLine {}),
24            KeyCode::Char('d') if ctrl => self.apply(Duplicate {}),
25            KeyCode::Char('a') if ctrl => self.apply(SelectAll {}),
26            KeyCode::Char('u') if ctrl => self.apply(UnIndent {}),
27            KeyCode::Left => self.apply(MoveLeft { shift }),
28            KeyCode::Right => self.apply(MoveRight { shift }),
29            KeyCode::Up => self.apply(MoveUp { shift }),
30            KeyCode::Down => self.apply(MoveDown { shift }),
31            KeyCode::Backspace => self.apply(Delete {}),
32            KeyCode::Enter => self.apply(InsertNewline {}),
33            KeyCode::Char(c) => self.apply(InsertText {
34                text: c.to_string(),
35            }),
36            KeyCode::Tab => self.apply(Indent {}),
37            KeyCode::BackTab => self.apply(UnIndent {}),
38            _ => {}
39        }
40        self.focus(&area);
41        Ok(())
42    }
43
44    pub fn mouse(&mut self, mouse: MouseEvent, area: &Rect) -> Result<()> {
45        match mouse.kind {
46            MouseEventKind::ScrollUp => self.scroll_up(),
47            MouseEventKind::ScrollDown => self.scroll_down(area.height as usize),
48            MouseEventKind::Down(MouseButton::Left) => {
49                if self.toggle_fold_at_mouse(mouse.column, mouse.row, area) {
50                    return Ok(());
51                }
52                if self.expand_hidden_diff_at_mouse(mouse.column, mouse.row, area) {
53                    return Ok(());
54                }
55                let pos = self.cursor_from_mouse(mouse.column, mouse.row, area);
56                if let Some(cursor) = pos {
57                    self.handle_mouse_down(cursor);
58                }
59            }
60            MouseEventKind::Drag(MouseButton::Left) => {
61                // Auto-scroll when dragging on the last or first visible row
62                if mouse.row == area.top() {
63                    self.scroll_up();
64                }
65                if mouse.row == area.bottom().saturating_sub(1) {
66                    self.scroll_down(area.height as usize);
67                }
68                let pos = self.cursor_from_mouse(mouse.column, mouse.row, area);
69                if let Some(cursor) = pos {
70                    self.handle_mouse_drag(cursor);
71                }
72            }
73            MouseEventKind::Up(MouseButton::Left) => {
74                self.selection_snap = SelectionSnap::None;
75            }
76            _ => {}
77        }
78        Ok(())
79    }
80}