pub struct Source { /* private fields */ }Expand description
One immutable source snapshot shared by every span into it.
Implementations§
Source§impl Source
impl Source
Sourcepub fn new(text: impl Into<Arc<str>>) -> Arc<Self> ⓘ
pub fn new(text: impl Into<Arc<str>>) -> Arc<Self> ⓘ
Creates an ordinary source file with a fresh compact identity.
Sourcepub fn synthetic(text: impl Into<Arc<str>>) -> Arc<Self> ⓘ
pub fn synthetic(text: impl Into<Arc<str>>) -> Arc<Self> ⓘ
Creates source text for syntax constructed outside an input file.
Sourcepub fn with_file(file: File, text: impl Into<Arc<str>>) -> Arc<Self> ⓘ
pub fn with_file(file: File, text: impl Into<Arc<str>>) -> Arc<Self> ⓘ
Creates a source snapshot with an explicitly selected source kind.
Sourcepub fn trailing_whitespace(&self) -> Range<Offset> ⓘ
pub fn trailing_whitespace(&self) -> Range<Offset> ⓘ
Returns the byte range of the layout that closes this snapshot.
The range is empty when the text ends with a significant byte. Parsers give the end-of-file marker exactly this range, so the bytes that follow the last token survive a parse and emit round trip.
use m2_syn::Source;
let source = Source::new("x = 1\n\n");
assert_eq!(source.trailing_whitespace(), 5..7);
assert_eq!(&source.text()[source.trailing_whitespace()], "\n\n");Sourcepub fn leading_whitespace(&self) -> Range<Offset> ⓘ
pub fn leading_whitespace(&self) -> Range<Offset> ⓘ
Returns the byte range of the layout that opens this snapshot.
The range is empty when the text starts with a significant byte, and
also when the text holds no significant byte at all: that layout closes
the snapshot, so Source::trailing_whitespace owns it instead.
use m2_syn::Source;
let source = Source::new("\n x = 1\n");
assert_eq!(source.leading_whitespace(), 0..3);
assert_eq!(Source::new(" \n").leading_whitespace(), 0..0);Sourcepub fn line_column(&self, offset: Offset) -> Option<LineColumn>
pub fn line_column(&self, offset: Offset) -> Option<LineColumn>
Converts a byte boundary into a one-based line and character column.
Returns None when offset lies outside this snapshot or inside a
multi-byte UTF-8 character.