Skip to main content

LineIndex

Struct LineIndex 

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

Line-start table over the original source.

Implementations§

Source§

impl LineIndex

Source

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

Build a LineIndex from full source text. O(n) in input length.

Source

pub fn line_col(&self, offset: TextSize) -> LineCol

Convert a byte offset to 0-based line/column (UTF-8).

If offset falls inside a multi-byte character the result reports the mechanical byte-offset within the line — callers that need char-aligned columns are responsible for snapping.

§Panics

Panics if the computed line index does not fit in u32, which requires a source file with more than u32::MAX lines — impossible for any input that also fits in a TextSize.

Source

pub fn to_utf16(&self, pos: LineCol) -> WideLineCol

Convert a LineCol (UTF-8) to the LSP UTF-16 flavour.

Source

pub fn from_utf16(&self, pos: WideLineCol) -> LineCol

Convert an LSP UTF-16 position back to a UTF-8 byte LineCol.

Source

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

Range of the given 0-based line in full-source byte offsets. Includes the trailing \n if the line has one. Returns None for lines past the end.

For the final line of the input (which has no trailing newline in the index) the range extends to TextSize::from(u32::MAX) as a sentinel end — callers that care about the “real” tail should clamp to the input length.

Source

pub fn line_count(&self) -> u32

Number of lines in the input. Always >= 1; empty input has a single empty line.

Trait Implementations§

Source§

impl Clone for LineIndex

Source§

fn clone(&self) -> LineIndex

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 LineIndex

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.