Skip to main content

LineMap

Struct LineMap 

Source
pub struct LineMap { /* private fields */ }
Expand description

A precomputed table of line-start byte offsets for a single source file.

Constructed once per file via LineMap::new; the compiler and diagnostics layer read it cheaply thereafter.

Implementations§

Source§

impl LineMap

Source

pub fn new(text: &str) -> LineMap

Build a line map from source text. \n, \r\n, and \r all count as line terminators, matching how the input parser treats logical lines.

Source

pub fn offset_to_linecol(&self, offset: BytePos) -> LineCol

Convert a byte offset to a 1-based (line, column).

Offsets past the end of the file clamp to the last byte of the last line rather than overflowing, so a slightly-out-of-range span still renders something sensible.

Source

pub fn linecol_to_offset(&self, lc: LineCol) -> Option<BytePos>

Convert a 1-based (line, column) back to a byte offset.

Returns None if line is zero or beyond the number of lines. A column past the end of the line clamps to the line’s last byte.

Source

pub fn line_count(&self) -> usize

The total number of lines.

Source

pub fn line_range(&self, line: u32) -> Option<(BytePos, BytePos)>

The byte range [start, end) of a given 1-based line number, or None if the line number is out of range.

end is the byte offset where the next line begins (or the file end for the final line), so it includes any line terminator. Callers that render the line text should trim it with LineMap::trim_line_terminator.

Source

pub fn trim_line_terminator( text: &[u8], start: BytePos, end: BytePos, ) -> BytePos

The content end of a line extent [start, end): end minus the bytes of the single line terminator (\n, \r, or \r\n) that separates this line from the next. Reads the actual bytes, so a CRLF is trimmed as one terminator and not as two.

LineMap::line_range deliberately hands back the extent including the terminator and tells the caller to trim it — this is that trim, and the only copy of it. The terminator set has to stay in step with the scanner in LineMap::new, so the rule lives beside the scanner: a change there is one edit rather than several that can silently desync.

Takes the bytes rather than reading self.text so that a caller which already holds the source trims against the very bytes it is about to slice. An extent with nothing in it, and a final line with no terminator at all, are both returned unchanged.

Trait Implementations§

Source§

impl Clone for LineMap

Source§

fn clone(&self) -> LineMap

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LineMap

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.