pub struct Span { /* private fields */ }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
impl Span
Sourcepub fn new(source: impl Into<String>, range: TextRange) -> Self
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
rangeout of source. - Panics if
source.len()out ofTextSize.
§Examples
use line_column::span::*;
let source = "abcdef";
let span = Span::new(source, TextRange::new(2.into(), 4.into()));
assert_eq!(span.text(), "cd");Sourcepub fn new_full(source: impl Into<String>) -> Self
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 ofTextSize.
§Examples
use line_column::span::*;
let source = "abcdef";
let full = Span::new_full(source);
assert_eq!(full.text(), "abcdef");Sourcepub fn create(&self, range: TextRange) -> Self
pub fn create(&self, range: TextRange) -> Self
New a span source range from exist span.
§Panics
- Panics if
rangeout 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");Sourcepub fn slice(&self, range: TextRange) -> Self
pub fn slice(&self, range: TextRange) -> Self
New a span relative range from exist span.
§Panics
- Panics if
range+startout 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");Sourcepub fn split(&self, len: TextSize) -> (Self, Self)
pub fn split(&self, len: TextSize) -> (Self, Self)
New splited span pair relative range from exist span.
§Panics
- Panics if
range+atout 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");Sourcepub fn take(&self, len: TextSize) -> Self
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");Sourcepub fn text(&self) -> &str
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§impl Span
impl Span
pub fn line_column(&self) -> (u32, u32)
pub fn line(&self) -> u32
pub fn column(&self) -> u32
Sourcepub fn current_line(&self) -> Self
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");Sourcepub fn prev_line(&self) -> Self
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(), "");Sourcepub fn next_line(&self) -> Self
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");