Skip to main content

LineIndex

Struct LineIndex 

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

Precomputed line start offsets for fast byte-offset-to-position conversion.

Construction is O(n) where n is the text length. Each position lookup is O(log L + k) where L is the number of lines and k is the byte length from the line start to the target offset.

Implementations§

Source§

impl LineIndex

Source

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

Builds a line index from the given text in O(n).

Source

pub fn byte_offset_to_position(&self, text: &str, offset: usize) -> (u32, u32)

Converts a byte offset to an LSP-compatible (line, character) pair.

  • line: 0-based line number
  • character: 0-based UTF-16 code unit offset from line start

If offset exceeds the text length, it is clamped to text.len().

Source

pub fn span_to_range(&self, text: &str, span: &Span) -> ((u32, u32), (u32, u32))

Converts a parser Span (byte offsets) to an LSP Range pair.

Returns ((start_line, start_char), (end_line, end_char)).

Source

pub fn position_to_byte_offset( &self, text: &str, line: u32, character: u32, ) -> usize

Converts an LSP Position (line, character in UTF-16 code units) to a byte offset.

Returns text.len() if the position is beyond the end of the text.

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> 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, 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.