Skip to main content

Span

Struct Span 

Source
pub struct Span { /* private fields */ }
Available on crate feature span only.
Expand description

text_size::TextRange wrapper

Stored source code pointers, allowing for easy retrieval of lines, columns, and source code text

If len() == 0, it is used to indicate offset

§Examples

use line_column::span::*;

let source = Span::new_full("foo,bar,baz");
let comma = source.create(TextRange::at(3.into(), TextSize::of(',')));
let bar = comma.after().take(TextSize::of("bar"));

assert_eq!(comma.text(), ",");
assert_eq!(bar.text(), "bar");
assert_eq!(bar.source(), "foo,bar,baz");
assert_eq!(bar.line_column(), (1, 5));

Implementations§

Source§

impl Span

Source

pub fn new(source: impl Into<String>, range: TextRange) -> Self

New a source and span range.

NOTE: It is not recommended to call repeatedly, otherwise the source will be allocated repeatedly. Consider using Span::create

§Panics
  • Panics if range out of source.
  • Panics if source.len() out of TextSize.
§Examples
use line_column::span::*;

let source = "abcdef";
let span = Span::new(source, TextRange::new(2.into(), 4.into()));
assert_eq!(span.text(), "cd");
Source

pub fn new_full(source: impl Into<String>) -> Self

New a full span of source.

NOTE: It is not recommended to call repeatedly, otherwise the source will be allocated repeatedly. Consider using Span::create

§Panics
  • Panics if source.len() out of TextSize.
§Examples
use line_column::span::*;

let source = "abcdef";
let full = Span::new_full(source);
assert_eq!(full.text(), "abcdef");
Source

pub fn create(&self, range: TextRange) -> Self

New a span source range from exist span.

§Panics
  • Panics if range out of source.
§Examples
use line_column::span::*;

let source = "abcdef";
let full = Span::new_full(source);
assert_eq!(full.text(), "abcdef");

let span = full.create(TextRange::at(1.into(), 3.into()));
assert_eq!(span.text(), "bcd");
let span2 = span.create(TextRange::at(3.into(), 3.into()));
assert_eq!(span2.text(), "def");
Source

pub fn slice(&self, range: TextRange) -> Self

New a span relative range from exist span.

§Panics
  • Panics if range+start out of source.
§Examples
use line_column::span::*;

let source = "abcdef";
let full = Span::new_full(source);
assert_eq!(full.text(), "abcdef");

let span = full.slice(TextRange::at(1.into(), 3.into()));
assert_eq!(span.text(), "bcd");
let span2 = span.slice(TextRange::at(1.into(), 3.into()));
assert_eq!(span2.text(), "cde");
Source

pub fn split(&self, len: TextSize) -> (Self, Self)

New splited span pair relative index from exist span.

§Panics
  • Panics if len+start out of source.
§Examples
use line_column::span::*;

let source = "abcdef";
let full = Span::new_full(source);
assert_eq!(full.text(), "abcdef");

let (a, span) = full.split(TextSize::of("a"));
assert_eq!(a.text(), "a");
assert_eq!(span.text(), "bcdef");

let (bcd, span2) = span.split(TextSize::of("bcd"));
assert_eq!(bcd.text(), "bcd");
assert_eq!(span2.text(), "ef");
Source

pub fn split_at(&self, index: TextSize) -> (Self, Self)

New splited span pair index from exist span.

§Panics
  • Panics if index out of source.
§Examples
use line_column::span::*;

let source = "abcdef";
let full = Span::new_full(source);
assert_eq!(full.text(), "abcdef");

let (a, span) = full.split_at(TextSize::of("a"));
assert_eq!(a.text(), "a");
assert_eq!(span.text(), "bcdef");

let (bcd, span2) = span.split_at(TextSize::of("abcd"));
assert_eq!(bcd.text(), "bcd");
assert_eq!(span2.text(), "ef");
Source

pub fn is_empty(&self) -> bool

Returns the is empty of this Span range.

§Examples
use line_column::span::*;

let span = Span::new_full("foo");
let empty = span.create(TextRange::empty(1.into()));
assert_eq!(span.is_empty(),  false);
assert_eq!(empty.is_empty(), true);
assert_eq!(empty.range(),    TextRange::new(1.into(), 1.into()));
Source

pub fn len(&self) -> TextSize

Returns the length of this Span range.

§Examples
use line_column::span::*;

let span = Span::new_full("foo");
let empty = span.create(TextRange::empty(1.into()));
assert_eq!(span.len(),  TextSize::new(3));
assert_eq!(empty.len(), TextSize::new(0));
Source

pub fn before(&self) -> Self

Returns the source before of this Span.

§Examples
use line_column::span::*;

let span = Span::new("foobarbaz", TextRange::new(3.into(), 6.into()));
assert_eq!(span.text(),          "bar");
assert_eq!(span.before().text(), "foo");
Source

pub fn after(&self) -> Self

Returns the source after of this Span.

§Examples
use line_column::span::*;

let span = Span::new("foobarbaz", TextRange::new(3.into(), 6.into()));
assert_eq!(span.text(),          "bar");
assert_eq!(span.after().text(),  "baz");
Source

pub fn take(&self, len: TextSize) -> Self

Returns truncated sub-span.

§Examples
use line_column::span::*;

let span = Span::new("foobarbaz", TextRange::new(3.into(), 7.into()));
assert_eq!(span.text(), "barb");
assert_eq!(span.take(3.into()).text(), "bar");
Source

pub fn start(&self) -> Self

Returns the start of this Span.

§Examples
use line_column::span::*;

let span = Span::new("abcdef", TextRange::new(1.into(), 4.into()));
assert_eq!(span.start().range(), TextRange::new(1.into(), 1.into()));
Source

pub fn end(&self) -> Self

Returns the end of this Span.

§Examples
use line_column::span::*;

let span = Span::new("abcdef", TextRange::new(1.into(), 4.into()));
assert_eq!(span.end().range(), TextRange::new(4.into(), 4.into()));
Source

pub fn index(&self) -> TextSize

Returns the start index of this Span range.

§Examples
use line_column::span::*;

let span = Span::new("abcdef", TextRange::new(1.into(), 4.into()));
assert_eq!(span.index(), TextSize::new(1));
Source

pub fn text(&self) -> &str

Returns the source text of the range reference.

§Examples
use line_column::span::*;

let span = Span::new("abcdef", TextRange::new(1.into(), 4.into()));
assert_eq!(span.text(), "bcd");
Source

pub fn range(&self) -> TextRange

Returns the source text of the range reference.

§Examples
use line_column::span::*;

let span = Span::new("abcdef", TextRange::new(1.into(), 4.into()));
assert_eq!(span.range(),       TextRange::new(1.into(), 4.into()));
Source

pub fn source(&self) -> &str

Returns the source text.

§Examples
use line_column::span::*;

let span = Span::new("abcdef", TextRange::new(1.into(), 4.into()));
assert_eq!(span.source(), "abcdef");
assert_eq!(span.text(),   "bcd");
Source§

impl Span

Source

pub fn line_column(&self) -> (u32, u32)

Use line_column calculate line and column

§Examples
use line_column::span::*;

let span = Span::new("ab\ncdef", TextRange::empty(TextSize::of("ab\ncd")));
assert_eq!(span.before().text(), "ab\ncd");
assert_eq!(span.line_column(), (2, 3));
Source

pub fn line(&self) -> u32

Get line from Span::line_column

Source

pub fn column(&self) -> u32

Get column from Span::line_column

Source

pub fn current_line(&self) -> Self

Returns the current line of this Span.

Maybe include end of line char, like '\n'.

§Examples
use line_column::span::*;

let span = Span::new_full("foo\nbar\nbaz");
let next = span.create(TextRange::at(TextSize::of("foo\n"), 5.into()));
let tail = span.create(TextRange::at(TextSize::of("foo\nbar\n"), 3.into()));
let endl = span.create(TextRange::at(TextSize::of("foo"), 3.into()));

assert_eq!(next.text(), "bar\nb");
assert_eq!(tail.text(), "baz");
assert_eq!(endl.text(), "\nba");

assert_eq!(span.current_line().text(), "foo\n");
assert_eq!(next.current_line().text(), "bar\n");
assert_eq!(tail.current_line().text(), "baz");
assert_eq!(endl.current_line().text(), "foo\n");
Source

pub fn prev_line(&self) -> Self

Returns the previous line of this Span.

§Examples
use line_column::span::*;

let span = Span::new_full("foo\nbar\nbaz");
let next = span.create(TextRange::at(TextSize::of("foo\n"), 5.into()));
let tail = span.create(TextRange::at(TextSize::of("foo\nbar\n"), 3.into()));
let endl = span.create(TextRange::at(TextSize::of("foo"), 3.into()));

assert_eq!(next.text(), "bar\nb");
assert_eq!(tail.text(), "baz");
assert_eq!(endl.text(), "\nba");

assert_eq!(span.prev_line().text(), "");
assert_eq!(next.prev_line().text(), "foo\n");
assert_eq!(tail.prev_line().text(), "bar\n");
assert_eq!(endl.prev_line().text(), "");
Source

pub fn next_line(&self) -> Self

Returns the next line of this Span.

§Examples
use line_column::span::*;

let span = Span::new_full("foo\nbar\nbaz");
let next = span.create(TextRange::at(TextSize::of("foo\n"), 5.into()));
let tail = span.create(TextRange::at(TextSize::of("foo\nbar\n"), 3.into()));
let endl = span.create(TextRange::at(TextSize::of("foo"), 3.into()));

assert_eq!(next.text(), "bar\nb");
assert_eq!(tail.text(), "baz");
assert_eq!(endl.text(), "\nba");

assert_eq!(span.next_line().text(), "bar\n");
assert_eq!(next.next_line().text(), "baz");
assert_eq!(tail.next_line().text(), "");
assert_eq!(endl.next_line().text(), "bar\n");
Source§

impl Span

Source

pub fn trim_end(&self) -> Self

Returns the trim end of this Span range.

§Examples
use line_column::span::*;

let span = Span::new("foo  bar  baz", TextRange::new(4.into(), 9.into()));
assert_eq!(span.text(), " bar ");
assert_eq!(span.trim_end().text(), " bar");
Source

pub fn trim_start(&self) -> Self

Returns the trim start of this Span range.

§Examples
use line_column::span::*;

let span = Span::new("foo  bar  baz", TextRange::new(4.into(), 9.into()));
assert_eq!(span.text(), " bar ");
assert_eq!(span.trim_start().text(), "bar ");

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Span

Returns a duplicate 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 Span

Source§

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

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

impl Default for Span

Source§

fn default() -> Span

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

impl From<&EmptySpan> for Span

Source§

fn from(span: &EmptySpan) -> Self

Converts to this type from the input type.
Source§

impl From<EmptySpan> for Span

Source§

fn from(span: EmptySpan) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Span

§

impl RefUnwindSafe for Span

§

impl Send for Span

§

impl Sync for Span

§

impl Unpin for Span

§

impl UnsafeUnpin for Span

§

impl UnwindSafe for Span

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, dest: *mut u8)

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