Struct Selection

Source
pub struct Selection { /* private fields */ }
Expand description

A selection holding one or more SelRegion. Regions are kept in order from the leftmost selection to the rightmost selection.

Implementations§

Source§

impl Selection

Source

pub fn new() -> Selection

Creates a new empty Selection

Source

pub fn caret(offset: usize) -> Selection

Creates a caret Selection, i.e. a selection with a single caret SelRegion

Source

pub fn region(start: usize, end: usize) -> Self

Creates a region Selection, i.e. a selection with a single SelRegion from start to end position

Source

pub fn sel_region(region: SelRegion) -> Self

Creates a Selection, with a single SelRegion equal to region.

Source

pub fn contains(&self, offset: usize) -> bool

Returns whether this Selection, contains the given offset position or not.

Example:

let  selection = Selection::region(0, 2);
assert!(selection.contains(0));
assert!(selection.contains(1));
assert!(selection.contains(2));
assert!(!selection.contains(3));
Source

pub fn regions(&self) -> &[SelRegion]

Returns this selection regions

Source

pub fn regions_mut(&mut self) -> &mut [SelRegion]

Returns a mutable reference to this selection regions

Source

pub fn min(&self) -> Selection

Returns a copy of self with all regions converted to caret region at their respective SelRegion::min offset.

Examples:

let mut selection = Selection::new();
selection.add_region(SelRegion::new(1, 3, None));
selection.add_region(SelRegion::new(6, 12, None));
selection.add_region(SelRegion::new(24, 48, None));

assert_eq!(selection.min().regions(), vec![
    SelRegion::caret(1),
    SelRegion::caret(6),
    SelRegion::caret(24)
]);
Source

pub fn first(&self) -> Option<&SelRegion>

Get the leftmost SelRegion in this selection if present.

Source

pub fn last(&self) -> Option<&SelRegion>

Get the rightmost SelRegion in this selection if present.

Source

pub fn last_inserted(&self) -> Option<&SelRegion>

Get the last inserted SelRegion in this selection if present.

Source

pub fn last_inserted_mut(&mut self) -> Option<&mut SelRegion>

Get a mutable reference to the last inserted SelRegion in this selection if present.

Source

pub fn len(&self) -> usize

The number of SelRegion in this selection.

Source

pub fn is_caret(&self) -> bool

A Selection is considered to be a caret if it contains only caret SelRegion (see SelRegion::is_caret)

Source

pub fn current_caret(&self) -> Option<usize>

Source

pub fn is_empty(&self) -> bool

Returns true if self has zero SelRegion

Source

pub fn min_offset(&self) -> usize

Returns the minimal offset across all region of this selection.

This function panics if the selection is empty.

Example:

let mut selection = Selection::new();
selection.add_region(SelRegion::caret(4));
selection.add_region(SelRegion::new(0, 12, None));
selection.add_region(SelRegion::new(24, 48, None));
assert_eq!(selection.min_offset(), 0);
Source

pub fn max_offset(&self) -> usize

Returns the maximal offset across all region of this selection.

This function panics if the selection is empty.

Example:

let mut selection = Selection::new();
selection.add_region(SelRegion::caret(4));
selection.add_region(SelRegion::new(0, 12, None));
selection.add_region(SelRegion::new(24, 48, None));
assert_eq!(selection.max_offset(), 48);
Source

pub fn regions_in_range(&self, start: usize, end: usize) -> &[SelRegion]

Returns regions in self overlapping or fully enclosed in the provided start to end range.

Example:

let mut selection = Selection::new();
selection.add_region(SelRegion::new(0, 3, None));
selection.add_region(SelRegion::new(3, 6, None));
selection.add_region(SelRegion::new(7, 8, None));
selection.add_region(SelRegion::new(9, 11, None));
let regions = selection.regions_in_range(5, 10);
assert_eq!(regions, vec![
    SelRegion::new(3, 6, None),
    SelRegion::new(7, 8, None),
    SelRegion::new(9, 11, None)
]);
Source

pub fn full_regions_in_range(&self, start: usize, end: usize) -> &[SelRegion]

Returns regions in self starting between start to end range.

Example:

let mut selection = Selection::new();
selection.add_region(SelRegion::new(0, 3, None));
selection.add_region(SelRegion::new(3, 6, None));
selection.add_region(SelRegion::new(7, 8, None));
selection.add_region(SelRegion::new(9, 11, None));
let regions = selection.full_regions_in_range(5, 10);
assert_eq!(regions, vec![
    SelRegion::new(7, 8, None),
    SelRegion::new(9, 11, None)
]);
Source

pub fn delete_range(&mut self, start: usize, end: usize)

Deletes regions in self overlapping or enclosing in start to end range.

Example:

let mut selection = Selection::new();
selection.add_region(SelRegion::new(0, 3, None));
selection.add_region(SelRegion::new(3, 6, None));
selection.add_region(SelRegion::new(7, 8, None));
selection.add_region(SelRegion::new(9, 11, None));
selection.delete_range(5, 10);
assert_eq!(selection.regions(), vec![SelRegion::new(0, 3, None)]);
Source

pub fn add_region(&mut self, region: SelRegion)

Add a regions to self. Note that if provided region overlap on of the selection regions they will be merged in a single region.

Example:

let mut selection = Selection::new();
// Overlapping
selection.add_region(SelRegion::new(0, 4, None));
selection.add_region(SelRegion::new(3, 6, None));
assert_eq!(selection.regions(), vec![SelRegion::new(0, 6, None)]);
// Non-overlapping
let mut selection = Selection::new();
selection.add_region(SelRegion::new(0, 3, None));
selection.add_region(SelRegion::new(3, 6, None));
assert_eq!(selection.regions(), vec![
    SelRegion::new(0, 3, None),
    SelRegion::new(3, 6, None)
]);
Source

pub fn add_range_distinct(&mut self, region: SelRegion) -> (usize, usize)

Add a region to the selection. This method does not merge regions and does not allow ambiguous regions (regions that overlap).

On ambiguous regions, the region with the lower start position wins. That is, in such a case, the new region is either not added at all, because there is an ambiguous region with a lower start position, or existing regions that intersect with the new region but do not start before the new region, are deleted.

Source

pub fn apply_delta( &self, delta: &RopeDelta, after: bool, drift: InsertDrift, ) -> Selection

Apply [lapce_xi_rope::RopeDelta] to this selection. Typically used to apply an edit to a buffer and update its selections Parameters:

  • delta[lapce_xi_rope::RopeDelta]
  • after parameter indicate if the delta should be applied before or after the selection
  • drift see InsertDrift
Source

pub fn get_cursor_offset(&self) -> usize

Returns cursor position, which corresponds to last inserted region end offset,

Source

pub fn replace_last_inserted_region(&mut self, region: SelRegion)

Replaces last inserted SelRegion of this selection with the provided one.

Trait Implementations§

Source§

impl AsRef<Selection> for Selection

Source§

fn as_ref(&self) -> &Selection

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Selection

Source§

fn clone(&self) -> Selection

Returns a copy 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() -> Self

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

impl<'de> Deserialize<'de> for Selection

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. 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 Serialize for Selection

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,