Struct rowan::TextRange

source ·
pub struct TextRange { /* private fields */ }
Expand description

A range in text, represented as a pair of TextSize.

It is a logic error for start to be greater than end.

Implementations§

source§

impl TextRange

source

pub const fn new(start: TextSize, end: TextSize) -> TextRange

Creates a new TextRange with the given start and end (start..end).

§Panics

Panics if end < start.

§Examples
let start = TextSize::from(5);
let end = TextSize::from(10);
let range = TextRange::new(start, end);

assert_eq!(range.start(), start);
assert_eq!(range.end(), end);
assert_eq!(range.len(), end - start);
source

pub const fn at(offset: TextSize, len: TextSize) -> TextRange

Create a new TextRange with the given offset and len (offset..offset + len).

§Examples
let text = "0123456789";

let offset = TextSize::from(2);
let length = TextSize::from(5);
let range = TextRange::at(offset, length);

assert_eq!(range, TextRange::new(offset, offset + length));
assert_eq!(&text[range], "23456")
source

pub const fn empty(offset: TextSize) -> TextRange

Create a zero-length range at the specified offset (offset..offset).

§Examples
let point: TextSize;
let range = TextRange::empty(point);
assert!(range.is_empty());
assert_eq!(range, TextRange::new(point, point));
source

pub const fn up_to(end: TextSize) -> TextRange

Create a range up to the given end (..end).

§Examples
let point: TextSize;
let range = TextRange::up_to(point);

assert_eq!(range.len(), point);
assert_eq!(range, TextRange::new(0.into(), point));
assert_eq!(range, TextRange::at(0.into(), point));
source§

impl TextRange

Identity methods.

source

pub const fn start(self) -> TextSize

The start point of this range.

source

pub const fn end(self) -> TextSize

The end point of this range.

source

pub const fn len(self) -> TextSize

The size of this range.

source

pub const fn is_empty(self) -> bool

Check if this range is empty.

source§

impl TextRange

Manipulation methods.

source

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

Check if this range contains an offset.

The end index is considered excluded.

§Examples
let (start, end): (TextSize, TextSize);
let range = TextRange::new(start, end);
assert!(range.contains(start));
assert!(!range.contains(end));
source

pub fn contains_inclusive(self, offset: TextSize) -> bool

Check if this range contains an offset.

The end index is considered included.

§Examples
let (start, end): (TextSize, TextSize);
let range = TextRange::new(start, end);
assert!(range.contains_inclusive(start));
assert!(range.contains_inclusive(end));
source

pub fn contains_range(self, other: TextRange) -> bool

Check if this range completely contains another range.

§Examples
let larger = TextRange::new(0.into(), 20.into());
let smaller = TextRange::new(5.into(), 15.into());
assert!(larger.contains_range(smaller));
assert!(!smaller.contains_range(larger));

// a range always contains itself
assert!(larger.contains_range(larger));
assert!(smaller.contains_range(smaller));
source

pub fn intersect(self, other: TextRange) -> Option<TextRange>

The range covered by both ranges, if it exists. If the ranges touch but do not overlap, the output range is empty.

§Examples
assert_eq!(
    TextRange::intersect(
        TextRange::new(0.into(), 10.into()),
        TextRange::new(5.into(), 15.into()),
    ),
    Some(TextRange::new(5.into(), 10.into())),
);
source

pub fn cover(self, other: TextRange) -> TextRange

Extends the range to cover other as well.

§Examples
assert_eq!(
    TextRange::cover(
        TextRange::new(0.into(), 5.into()),
        TextRange::new(15.into(), 20.into()),
    ),
    TextRange::new(0.into(), 20.into()),
);
source

pub fn cover_offset(self, offset: TextSize) -> TextRange

Extends the range to cover other offsets as well.

§Examples
assert_eq!(
    TextRange::empty(0.into()).cover_offset(20.into()),
    TextRange::new(0.into(), 20.into()),
)
source

pub fn checked_add(self, offset: TextSize) -> Option<TextRange>

Add an offset to this range.

Note that this is not appropriate for changing where a TextRange is within some string; rather, it is for changing the reference anchor that the TextRange is measured against.

The unchecked version (Add::add) will always panic on overflow, in contrast to primitive integers, which check in debug mode only.

source

pub fn checked_sub(self, offset: TextSize) -> Option<TextRange>

Subtract an offset from this range.

Note that this is not appropriate for changing where a TextRange is within some string; rather, it is for changing the reference anchor that the TextRange is measured against.

The unchecked version (Sub::sub) will always panic on overflow, in contrast to primitive integers, which check in debug mode only.

source

pub fn ordering(self, other: TextRange) -> Ordering

Relative order of the two ranges (overlapping ranges are considered equal).

This is useful when, for example, binary searching an array of disjoint ranges.

§Examples

let a = TextRange::new(0.into(), 3.into());
let b = TextRange::new(4.into(), 5.into());
assert_eq!(a.ordering(b), Ordering::Less);

let a = TextRange::new(0.into(), 3.into());
let b = TextRange::new(3.into(), 5.into());
assert_eq!(a.ordering(b), Ordering::Less);

let a = TextRange::new(0.into(), 3.into());
let b = TextRange::new(2.into(), 5.into());
assert_eq!(a.ordering(b), Ordering::Equal);

let a = TextRange::new(0.into(), 3.into());
let b = TextRange::new(2.into(), 2.into());
assert_eq!(a.ordering(b), Ordering::Equal);

let a = TextRange::new(2.into(), 3.into());
let b = TextRange::new(2.into(), 2.into());
assert_eq!(a.ordering(b), Ordering::Greater);

Trait Implementations§

source§

impl Add<&TextSize> for TextRange

source§

type Output = TextRange

The resulting type after applying the + operator.
source§

fn add(self, other: &TextSize) -> TextRange

Performs the + operation. Read more
source§

impl<T> Add<T> for &TextRange
where TextRange: Add<T, Output = TextRange>,

source§

type Output = TextRange

The resulting type after applying the + operator.
source§

fn add(self, other: T) -> TextRange

Performs the + operation. Read more
source§

impl Add<TextSize> for TextRange

source§

type Output = TextRange

The resulting type after applying the + operator.
source§

fn add(self, offset: TextSize) -> TextRange

Performs the + operation. Read more
source§

impl<A> AddAssign<A> for TextRange
where TextRange: Add<A, Output = TextRange>,

source§

fn add_assign(&mut self, rhs: A)

Performs the += operation. Read more
source§

impl Clone for TextRange

source§

fn clone(&self) -> TextRange

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 TextRange

source§

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

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

impl Default for TextRange

source§

fn default() -> TextRange

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

impl Hash for TextRange

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Index<TextRange> for str

source§

type Output = str

The returned type after indexing.
source§

fn index(&self, index: TextRange) -> &str

Performs the indexing (container[index]) operation. Read more
source§

impl IndexMut<TextRange> for str

source§

fn index_mut(&mut self, index: TextRange) -> &mut str

Performs the mutable indexing (container[index]) operation. Read more
source§

impl PartialEq for TextRange

source§

fn eq(&self, other: &TextRange) -> 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 RangeBounds<TextSize> for TextRange

source§

fn start_bound(&self) -> Bound<&TextSize>

Start index bound. Read more
source§

fn end_bound(&self) -> Bound<&TextSize>

End index bound. Read more
1.35.0 · source§

fn contains<U>(&self, item: &U) -> bool
where T: PartialOrd<U>, U: PartialOrd<T> + ?Sized,

Returns true if item is contained in the range. Read more
source§

impl Sub<&TextSize> for TextRange

source§

type Output = TextRange

The resulting type after applying the - operator.
source§

fn sub(self, other: &TextSize) -> TextRange

Performs the - operation. Read more
source§

impl<T> Sub<T> for &TextRange
where TextRange: Sub<T, Output = TextRange>,

source§

type Output = TextRange

The resulting type after applying the - operator.
source§

fn sub(self, other: T) -> TextRange

Performs the - operation. Read more
source§

impl Sub<TextSize> for TextRange

source§

type Output = TextRange

The resulting type after applying the - operator.
source§

fn sub(self, offset: TextSize) -> TextRange

Performs the - operation. Read more
source§

impl<S> SubAssign<S> for TextRange
where TextRange: Sub<S, Output = TextRange>,

source§

fn sub_assign(&mut self, rhs: S)

Performs the -= operation. Read more
source§

impl Copy for TextRange

source§

impl Eq for TextRange

source§

impl StructuralPartialEq for TextRange

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 T)

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

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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.