pub struct LineIndex { /* private fields */ }Expand description
Index for mapping byte offsets to line/column positions.
Built once per source text, then queried as needed (e.g. for error display).
use pydocstring::ast::{LineIndex, TextSize};
let source = "first\nsecond\nthird";
let index = LineIndex::from_source(source);
let (line, col) = index.line_col(TextSize::new(6));
assert_eq!(line, 1); // 0-indexed: second line
assert_eq!(col, 0); // start of lineImplementations§
Source§impl LineIndex
impl LineIndex
Sourcepub fn from_source(source: &str) -> Self
pub fn from_source(source: &str) -> Self
Build a line index from source text.
Sourcepub fn line_col(&self, offset: TextSize) -> (u32, u32)
pub fn line_col(&self, offset: TextSize) -> (u32, u32)
Returns 0-indexed (line, column) for a byte offset.
Column is the byte offset from the start of the line.
Sourcepub fn line(&self, offset: TextSize) -> u32
pub fn line(&self, offset: TextSize) -> u32
Returns the 0-indexed line number for a byte offset.
Sourcepub fn line_start(&self, line: u32) -> TextSize
pub fn line_start(&self, line: u32) -> TextSize
Returns the byte offset of the start of a given line.
Sourcepub fn line_count(&self) -> u32
pub fn line_count(&self) -> u32
Number of lines in the source.
Auto Trait Implementations§
impl Freeze for LineIndex
impl RefUnwindSafe for LineIndex
impl Send for LineIndex
impl Sync for LineIndex
impl Unpin for LineIndex
impl UnsafeUnpin for LineIndex
impl UnwindSafe for LineIndex
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more