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
lineandcolumninstead for robust conversion
§Usage with GTK
When converting to GTK TextIter:
- Convert line:
parser_line (1-based)→gtk_line (0-based) - Get line text from GTK buffer
- Convert column:
byte_offset → char_offsetusingchar_indices() - 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: usizeLine number (1-based, CommonMark convention)
column: usizeColumn 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: usizeAbsolute byte offset from document start
For debugging/logging only - do not use for GTK positioning!
Implementations§
Source§impl Position
impl Position
Sourcepub fn new(line: usize, column: usize, offset: usize) -> Self
pub fn new(line: usize, column: usize, offset: usize) -> Self
Create a new source position from 1-based line/column and absolute offset.
Sourcepub fn line_start_offset(&self) -> usize
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<'de> Deserialize<'de> for Position
impl<'de> Deserialize<'de> for Position
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Copy for Position
impl Eq for Position
impl StructuralPartialEq for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnsafeUnpin for Position
impl UnwindSafe for Position
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.