[]Struct ra_ap_syntax::TextRange

pub struct TextRange { /* fields omitted */ }

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

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

Implementations

impl TextRange

pub 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);

pub 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")

pub 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));

pub 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));

impl TextRange

Identity methods.

pub const fn start(self) -> TextSize

The start point of this range.

pub const fn end(self) -> TextSize

The end point of this range.

pub const fn len(self) -> TextSize

The size of this range.

pub const fn is_empty(self) -> bool

Check if this range is empty.

impl TextRange

Manipulation methods.

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

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

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

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())),
);

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()),
);

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()),
)

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.

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.

Trait Implementations

impl<'_> Add<&'_ TextSize> for TextRange

type Output = TextRange

The resulting type after applying the + operator.

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

type Output = TextRange

The resulting type after applying the + operator.

impl Add<TextSize> for TextRange

type Output = TextRange

The resulting type after applying the + operator.

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

impl Clone for TextRange

impl Copy for TextRange

impl Debug for TextRange

impl Default for TextRange

impl Eq for TextRange

impl Hash for TextRange

impl Index<TextRange> for str

type Output = str

The returned type after indexing.

impl IndexMut<TextRange> for str

impl PartialEq<TextRange> for TextRange

impl RangeBounds<TextSize> for TextRange

impl<'_> Sub<&'_ TextSize> for TextRange

type Output = TextRange

The resulting type after applying the - operator.

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

type Output = TextRange

The resulting type after applying the - operator.

impl Sub<TextSize> for TextRange

type Output = TextRange

The resulting type after applying the - operator.

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

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.