Skip to main content

Selection

Struct Selection 

Source
pub struct Selection {
    pub anchor: usize,
    pub head: usize,
}
Expand description

Represents a text selection using an anchor and a head.

The anchor is the position where the selection started, while the head is the current cursor position. When they differ, the selection extends between these two positions.

Both positions are byte offsets into the document.

§Examples

A collapsed selection, where the cursor is at byte offset 10:

let selection = Selection::point(10);
assert!(selection.is_empty());

A selection from byte offset 10 to 20:

let selection = Selection::range(10, 20);
assert_eq!(selection.byte_range(), 10..20);

Fields§

§anchor: usize

The byte offset where the selection started.

This remains fixed while the selection is extended or contracted by moving the head.

§head: usize

The current cursor position, represented as a byte offset.

Moving the head changes the extent and direction of the selection while the anchor remains fixed.

Implementations§

Source§

impl Selection

Source

pub const fn point(offset: usize) -> Self

Creates a collapsed selection with the cursor at offset.

Both the anchor and head are initialized to the same position.

Source

pub const fn range(anchor: usize, head: usize) -> Self

Creates a selection from an anchor position to a head position.

The positions are not reordered, so anchor may be greater than head. Use Self::byte_range to obtain the normalized range.

Source

pub fn is_empty(&self) -> bool

Returns true if the selection is collapsed.

A collapsed selection has the anchor and head at the same position.

Source

pub fn byte_range(&self) -> Range<usize>

Returns the normalized byte range covered by the selection.

The returned range always starts at the smaller of the anchor and head and ends at the larger, regardless of the direction in which the selection was made.

Trait Implementations§

Source§

impl Clone for Selection

Source§

fn clone(&self) -> Selection

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Selection

Source§

impl Debug for Selection

Source§

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

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

impl Eq for Selection

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 (const: unstable) · 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 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.