Added support for duplication

This commit is contained in:
Joshua Barretto 2025-09-23 13:20:32 +01:00
parent cec0ae50e5
commit 5fcd147c8c
3 changed files with 35 additions and 0 deletions

View file

@ -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,
}
}

View file

@ -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())
}

View file

@ -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),
}
}