Skip to main content

Selection

Struct Selection 

Source
pub struct Selection {
    pub anchor: Position,
    pub active: bool,
    pub mode: SelectionMode,
}
Expand description

Selection state within a buffer.

Tracks whether a selection is active, where it started (anchor), and what mode it’s in. The selection extends from the anchor to the current cursor position.

§Selection Direction

The anchor is where selection started. The selection can extend in either direction from the anchor:

  • Forward: cursor is after anchor
  • Backward: cursor is before anchor

Use bounds() to get normalized (start, end) positions.

Fields§

§anchor: Position

The position where selection started.

§active: bool

Whether selection is currently active.

§mode: SelectionMode

The selection mode (character/block/line).

Implementations§

Source§

impl Selection

Source

pub const fn new() -> Self

Create a new inactive selection.

Source

pub const fn start(&mut self, pos: Position, mode: SelectionMode)

Start a selection at the given position with the specified mode.

This sets the anchor and activates the selection.

§Arguments
  • pos - The anchor position (where selection starts)
  • mode - The selection mode
Source

pub const fn start_char(&mut self, pos: Position)

Start a character-wise selection at the given position.

Convenience method equivalent to start(pos, SelectionMode::Character).

Source

pub const fn start_line(&mut self, pos: Position)

Start a line-wise selection at the given position.

Convenience method equivalent to start(pos, SelectionMode::Line).

Source

pub const fn start_block(&mut self, pos: Position)

Start a block selection at the given position.

Convenience method equivalent to start(pos, SelectionMode::Block).

Source

pub const fn clear(&mut self)

Clear (deactivate) the selection.

The anchor position is preserved but the selection becomes inactive.

Source

pub const fn is_active(&self) -> bool

Check if selection is currently active.

Source

pub const fn mode(&self) -> SelectionMode

Get the selection mode.

Source

pub const fn set_mode(&mut self, mode: SelectionMode)

Change the selection mode without changing anchor or active state.

Source

pub fn bounds(&self, cursor_pos: Position) -> Option<(Position, Position)>

Get selection bounds in document order (start <= end).

Returns None if selection is not active. The returned positions are always ordered so that start comes before or equals end in document order.

§Arguments
  • cursor_pos - The current cursor position (selection endpoint)
Source

pub fn block_bounds(&self, cursor_pos: Position) -> Option<(Position, Position)>

Get block selection bounds (top-left, bottom-right).

For block selections, this returns the rectangle corners:

  • Top-left: (min_line, min_column)
  • Bottom-right: (max_line, max_column)

Returns None if selection is not active or not in block mode.

§Arguments
  • cursor_pos - The current cursor position
Source

pub fn line_bounds(&self, cursor_pos: Position) -> Option<(usize, usize)>

Get line-wise selection bounds (start_line, end_line).

For line selections, this returns the range of selected lines. Returns None if selection is not active.

§Arguments
  • cursor_pos - The current cursor position
Source

pub fn contains(&self, pos: Position, cursor_pos: Position) -> bool

Check if a position is within the selection.

The behavior depends on the selection mode:

  • Character: position is between start and end
  • Block: position is within the rectangle
  • Line: position’s line is within the line range
§Arguments
  • pos - The position to check
  • cursor_pos - The current cursor position (selection endpoint)
Source

pub fn line_count(&self, cursor_pos: Position) -> usize

Get the number of lines in the selection.

Returns 0 if selection is not active.

§Arguments
  • cursor_pos - The current cursor position

Trait Implementations§

Source§

impl Clone for Selection

Source§

fn clone(&self) -> Selection

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Selection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Selection

Source§

fn default() -> Selection

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Selection

Source§

fn eq(&self, other: &Selection) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Selection

Source§

impl Eq for Selection

Source§

impl StructuralPartialEq for Selection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.