From 5fcd147c8c1c730aa435edf34e6a0dbbc5c04e2a Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Tue, 23 Sep 2025 13:20:32 +0100 Subject: [PATCH] Added support for duplication --- src/action.rs | 7 +++++++ src/state.rs | 23 +++++++++++++++++++++++ src/ui/input.rs | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/src/action.rs b/src/action.rs index c4593e1..7e1b5a8 100644 --- a/src/action.rs +++ b/src/action.rs @@ -48,6 +48,7 @@ pub enum Action { Copy, Cut, Paste, + Duplicate, } /// How far should movement go? @@ -464,6 +465,12 @@ impl RawEvent { kind: KeyEventKind::Press, .. }) => Some(Action::Paste), + TerminalEvent::Key(KeyEvent { + code: KeyCode::Char('d'), + modifiers: KeyModifiers::CONTROL, + kind: KeyEventKind::Press, + .. + }) => Some(Action::Duplicate), _ => None, } } diff --git a/src/state.rs b/src/state.rs index ee78201..91a98f1 100644 --- a/src/state.rs +++ b/src/state.rs @@ -768,6 +768,29 @@ impl Buffer { } } + pub fn duplicate(&mut self, cursor_id: CursorId) { + let Some(cursor) = self.cursors.get_mut(cursor_id) else { + return; + }; + if let Some(s) = cursor.selection() + && let Some(text) = cursor.selection().and_then(|s| self.text.chars().get(s)) + { + // cursor.place_at(s.end); + self.insert_after(cursor_id, text.to_vec()) + } else { + let coord = self.text.to_coord(cursor.pos); + let line = self + .text + .lines() + .nth(coord[1].max(0) as usize) + .map(|l| l.to_vec()); + if let Some(line) = line { + let end_of_line = self.text.to_pos([0, coord[1] + 1]); + self.insert(end_of_line, line); + } + } + } + pub fn start_session(&mut self) -> CursorId { self.cursors.insert(Cursor::default()) } diff --git a/src/ui/input.rs b/src/ui/input.rs index f56974c..baf6e3a 100644 --- a/src/ui/input.rs +++ b/src/ui/input.rs @@ -188,6 +188,11 @@ impl Input { Ok(Resp::handled(Some(Event::Bell))) } } + Some(Action::Duplicate) => { + buffer.duplicate(cursor_id); + self.refocus(buffer, cursor_id); + Ok(Resp::handled(None)) + } _ => Err(event), } }