Refactor cursor API in Editor

Co-authored-by: Neeraj Jaiswal <neerajj85@gmail.com>
This commit is contained in:
Héctor Ramón Jiménez 2025-12-01 19:40:36 +01:00
parent 2ac62f7512
commit 63e9eeffb5
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
5 changed files with 95 additions and 47 deletions

View file

@ -20,13 +20,11 @@ pub trait Editor: Sized + Default {
/// Returns the current [`Cursor`] of the [`Editor`].
fn cursor(&self) -> Cursor;
/// Returns the current cursor position of the [`Editor`].
///
/// Line and column, respectively.
fn cursor_position(&self) -> (usize, usize);
/// Returns the current [`Selection`] of the [`Editor`].
fn selection(&self) -> Selection;
/// Returns the current selected text of the [`Editor`].
fn selection(&self) -> Option<String>;
fn copy(&self) -> Option<String>;
/// Returns the text of the given line in the [`Editor`], if it exists.
fn line(&self, index: usize) -> Option<Line<'_>>;
@ -187,12 +185,31 @@ pub enum Direction {
/// The cursor of an [`Editor`].
#[derive(Debug, Clone)]
pub enum Cursor {
pub enum Selection {
/// Cursor without a selection
Caret(Point),
/// Cursor selecting a range of text
Selection(Vec<Rectangle>),
Range(Vec<Rectangle>),
}
/// The range of an [`Editor`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Cursor {
/// The cursor position.
pub position: Position,
/// The selection position, if any.
pub selection: Option<Position>,
}
/// A cursor position in an [`Editor`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Position {
/// The line of text.
pub line: usize,
/// The column in the line.
pub column: usize,
}
/// A line of an [`Editor`].