Merge pull request #3125 from nrjais/editor-api

Improve `cursor` API in `Editor` and add `move_to` method
This commit is contained in:
Héctor 2025-12-01 20:26:25 +01:00 committed by GitHub
commit a3abe94dad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 389 additions and 304 deletions

View file

@ -166,14 +166,17 @@ impl text::Editor for () {
}
fn cursor(&self) -> text::editor::Cursor {
text::editor::Cursor::Caret(Point::ORIGIN)
text::editor::Cursor {
position: text::editor::Position { line: 0, column: 0 },
selection: None,
}
}
fn cursor_position(&self) -> (usize, usize) {
(0, 0)
fn selection(&self) -> text::editor::Selection {
text::editor::Selection::Caret(Point::ORIGIN)
}
fn selection(&self) -> Option<String> {
fn copy(&self) -> Option<String> {
None
}
@ -187,6 +190,8 @@ impl text::Editor for () {
fn perform(&mut self, _action: text::editor::Action) {}
fn move_to(&mut self, _cursor: text::editor::Cursor) {}
fn bounds(&self) -> Size {
Size::ZERO
}

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<'_>>;
@ -37,6 +35,9 @@ pub trait Editor: Sized + Default {
/// Performs an [`Action`] on the [`Editor`].
fn perform(&mut self, action: Action);
/// Moves the cursor to the given position.
fn move_to(&mut self, cursor: Cursor);
/// Returns the current boundaries of the [`Editor`].
fn bounds(&self) -> Size;
@ -187,12 +188,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`].