#[repr(C)]pub struct Span {
pub start: u32,
pub end: u32,
/* private fields */
}Expand description
A range in text, represented by a zero-indexed start and end offset.
It is a logical error for end to be less than start.
let text = "foo bar baz";
let span = Span::new(4, 7);
assert_eq!(&text[span], "bar");Spans use u32 for offsets, meaning only files up to 4GB are supported.
This is sufficient for “all” reasonable programs. This tradeof cuts the size
of Span in half, offering a sizeable performance improvement and memory
footprint reduction.
§Creating Spans
Span offers several constructors, each of which is more or less convenient
depending on the context. In general, Span::new is sufficient for most
cases. If you want to create a span starting at some point of a certain
length, you can use Span::sized.
let a = Span::new(5, 10); // Start and end offsets
let b = Span::sized(5, 5); // Start offset and size
assert_eq!(a, b);§Re-Sizing Spans
Span offsets can be mutated directly, but it is often more convenient to use
one of the expand or shrink methods. Each of these create a new span
without modifying the original.
let s = Span::new(5, 10);
assert_eq!(s.shrink(2), Span::new(7, 8));
assert_eq!(s.shrink(2), s.shrink_left(2).shrink_right(2));
assert_eq!(s.expand(5), Span::new(0, 15));
assert_eq!(s.expand(5), s.expand_left(5).expand_right(5));§Comparison
Span has a normal implementation of PartialEq. If you want to compare two
AST nodes without considering their locations (e.g. to see if they have the
same content), use ContentEq instead.
§Implementation Notes
See the text-size crate for details.
Utility methods can be copied from the text-size crate if they are needed.
Fields§
§start: u32The zero-based start offset of the span
end: u32The zero-based end offset of the span. This may be equal to start if
the span is empty, but should not be less than it.
Implementations§
Source§impl Span
impl Span
Sourcepub const fn is_empty(self) -> bool
pub const fn is_empty(self) -> bool
Returns true if self covers a range of zero length.
§Example
use oxc_span::Span;
assert!(Span::new(0, 0).is_empty());
assert!(Span::new(5, 5).is_empty());
assert!(!Span::new(0, 5).is_empty());Sourcepub const fn is_unspanned(self) -> bool
pub const fn is_unspanned(self) -> bool
Returns true if self is not a real span.
i.e. SPAN which is used for generated nodes which are not in source code.
§Example
use oxc_span::{Span, SPAN};
assert!(SPAN.is_unspanned());
assert!(!Span::new(0, 5).is_unspanned());
assert!(!Span::new(5, 5).is_unspanned());Sourcepub const fn contains_inclusive(self, span: Span) -> bool
pub const fn contains_inclusive(self, span: Span) -> bool
Check if this Span contains another Span.
Spans that start & end at the same position as this Span are
considered contained.
§Examples
let span = Span::new(5, 10);
assert!(span.contains_inclusive(span)); // always true for itself
assert!(span.contains_inclusive(Span::new(5, 5)));
assert!(span.contains_inclusive(Span::new(6, 10)));
assert!(span.contains_inclusive(Span::empty(5)));
assert!(!span.contains_inclusive(Span::new(4, 10)));
assert!(!span.contains_inclusive(Span::empty(0)));Sourcepub fn expand(self, offset: u32) -> Self
pub fn expand(self, offset: u32) -> Self
Create a Span that is grown by offset on either side.
This is equivalent to span.expand_left(offset).expand_right(offset).
See expand_left and expand_right for more info.
§Example
use oxc_span::Span;
let span = Span::new(3, 5);
assert_eq!(span.expand(1), Span::new(2, 6));
// start and end cannot be expanded past `0` and `u32::MAX`, respectively
assert_eq!(span.expand(5), Span::new(0, 10));Sourcepub fn shrink(self, offset: u32) -> Self
pub fn shrink(self, offset: u32) -> Self
Create a Span that has its start and end positions shrunk by
offset amount.
It is a logical error to shrink the start of the Span past its end
position. This will panic in debug builds.
This is equivalent to span.shrink_left(offset).shrink_right(offset).
See shrink_left and shrink_right for more info.
§Example
use oxc_span::Span;
let span = Span::new(5, 10);
assert_eq!(span.shrink(2), Span::new(7, 8));Sourcepub const fn expand_left(self, offset: u32) -> Self
pub const fn expand_left(self, offset: u32) -> Self
Create a Span that has its start position moved to the left by
offset bytes.
§Example
use oxc_span::Span;
let a = Span::new(5, 10);
assert_eq!(a.expand_left(5), Span::new(0, 10));§Bounds
The leftmost bound of the span is clamped to 0. It is safe to call this method with a value larger than the start position.
use oxc_span::Span;
let a = Span::new(0, 5);
assert_eq!(a.expand_left(5), Span::new(0, 5));Sourcepub const fn shrink_left(self, offset: u32) -> Self
pub const fn shrink_left(self, offset: u32) -> Self
Create a Span that has its start position moved to the right by
offset bytes.
It is a logical error to shrink the start of the Span past its end
position.
§Example
use oxc_span::Span;
let a = Span::new(5, 10);
let shrunk = a.shrink_left(5);
assert_eq!(shrunk, Span::new(10, 10));
// Shrinking past the end of the span is a logical error that will panic
// in debug builds.
std::panic::catch_unwind(|| {
shrunk.shrink_left(5);
});Sourcepub const fn expand_right(self, offset: u32) -> Self
pub const fn expand_right(self, offset: u32) -> Self
Create a Span that has its end position moved to the right by
offset bytes.
§Example
use oxc_span::Span;
let a = Span::new(5, 10);
assert_eq!(a.expand_right(5), Span::new(5, 15));§Bounds
The rightmost bound of the span is clamped to u32::MAX. It is safe to
call this method with a value larger than the end position.
use oxc_span::Span;
let a = Span::new(0, u32::MAX);
assert_eq!(a.expand_right(5), Span::new(0, u32::MAX));Sourcepub const fn shrink_right(self, offset: u32) -> Self
pub const fn shrink_right(self, offset: u32) -> Self
Create a Span that has its end position moved to the left by
offset bytes.
It is a logical error to shrink the end of the Span past its start
position.
§Example
use oxc_span::Span;
let a = Span::new(5, 10);
let shrunk = a.shrink_right(5);
assert_eq!(shrunk, Span::new(5, 5));
// Shrinking past the start of the span is a logical error that will panic
// in debug builds.
std::panic::catch_unwind(|| {
shrunk.shrink_right(5);
});Sourcepub fn source_text(self, source_text: &str) -> &str
pub fn source_text(self, source_text: &str) -> &str
Sourcepub fn label<S: Into<String>>(self, label: S) -> LabeledSpan
pub fn label<S: Into<String>>(self, label: S) -> LabeledSpan
Create a LabeledSpan covering this Span with the given label.
Use Span::primary_label if this is the primary span for the diagnostic.
Sourcepub fn primary_label<S: Into<String>>(self, label: S) -> LabeledSpan
pub fn primary_label<S: Into<String>>(self, label: S) -> LabeledSpan
Creates a primary LabeledSpan covering this Span with the given label.
Trait Implementations§
Source§impl<'a> CloneIn<'a> for Span
impl<'a> CloneIn<'a> for Span
Source§fn clone_in(&self, _: &'a Allocator) -> Self
fn clone_in(&self, _: &'a Allocator) -> Self
self into the given allocator. allocator may be the same one
that self is already in.Source§fn clone_in_with_semantic_ids(
&self,
allocator: &'new_alloc Allocator,
) -> Self::Cloned
fn clone_in_with_semantic_ids( &self, allocator: &'new_alloc Allocator, ) -> Self::Cloned
clone_in, but for some special type, it will also clone the semantic ids.
Please use this method only if you make sure semantic info is synced with the ast node.Source§impl From<Span> for LabeledSpan
impl From<Span> for LabeledSpan
Source§impl From<Span> for SourceSpan
impl From<Span> for SourceSpan
Source§impl GetSpanMut for Span
impl GetSpanMut for Span
Source§impl Index<Span> for CompactStr
impl Index<Span> for CompactStr
Source§impl Ord for Span
impl Ord for Span
Source§impl PartialOrd for Span
impl PartialOrd for Span
impl Copy for Span
impl Eq for Span
Auto Trait Implementations§
impl Freeze for Span
impl RefUnwindSafe for Span
impl Send for Span
impl Sync for Span
impl Unpin for Span
impl UnwindSafe for Span
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more