Struct mun_syntax::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§

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

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

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

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

Identity methods.

The start point of this range.

The end point of this range.

The size of this range.

Check if this range is empty.

Manipulation methods.

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

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

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

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

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

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

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.

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.

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§

The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Start index bound. Read more
End index bound. Read more
Returns true if item is contained in the range. Read more
Serialize this value into the given Serde serializer. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
Performs the -= operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.