Skip to main content

Position

Struct Position 

Source
pub struct Position {
    pub line: usize,
    pub column: usize,
    pub offset: usize,
}
Expand description

Position in a source document using multiple coordinate systems.

This struct tracks a position using three different representations:

  • Line/Column: CommonMark-style 1-based coordinates
  • Absolute Offset: Byte offset from document start

§Coordinate Systems

§Line/Column (Primary for GTK Integration)

  • line: 1-based line number (CommonMark convention)
  • column: 1-based byte offset from the start of the line

Important: column is a BYTE offset, not a character offset!

  • For ASCII: byte offset == character offset
  • For UTF-8: Multi-byte characters cause divergence
    • Example: “Tëst” has ‘ë’ at byte columns 3-4, but char column 2
    • Example: “🎨” (emoji) occupies 4 bytes but is 1 character

§Absolute Offset (For Debugging Only)

  • offset: Absolute byte offset from document start
  • Do NOT use for GTK TextIter positioning!
  • Use line and column instead for robust conversion

§Usage with GTK

When converting to GTK TextIter:

  1. Convert line: parser_line (1-based)gtk_line (0-based)
  2. Get line text from GTK buffer
  3. Convert column: byte_offset → char_offset using char_indices()
  4. Set position: iter_at_line(gtk_line).set_line_offset(char_offset)

See the host editor’s cursor-conversion bridge for a reference implementation of byte-to-character offset mapping.

Fields§

§line: usize

Line number (1-based, CommonMark convention)

§column: usize

Column as byte offset from line start (1-based, CommonMark convention)

Note: This is NOT a character offset! Multi-byte UTF-8 characters cause byte offsets to differ from character positions.

§offset: usize

Absolute byte offset from document start

For debugging/logging only - do not use for GTK positioning!

Implementations§

Source§

impl Position

Source

pub fn new(line: usize, column: usize, offset: usize) -> Self

Create a new source position from 1-based line/column and absolute offset.

Source

pub fn line_start_offset(&self) -> usize

Compute the absolute byte offset of the start of this position’s line.

Uses the invariant that column is a 1-based byte offset from the start of the line, and offset is the absolute byte offset from the start of the document. The formula is:

line_start_offset = offset - (column - 1)

This function uses saturating math to avoid underflow in case of malformed positions.

Trait Implementations§

Source§

impl Clone for Position

Source§

fn clone(&self) -> Position

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 Position

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Position

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Position

Source§

fn eq(&self, other: &Position) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Position

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Position

Source§

impl Eq for Position

Source§

impl StructuralPartialEq for Position

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,