Skip to main content

PositionMapper

Struct PositionMapper 

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

Centralized position mapper using rope for efficiency.

Converts between byte offsets (used by the parser) and LSP positions (line/character in UTF-16 code units) while handling mixed line endings.

§Examples

use perl_position_tracking::PositionMapper;

let text = "my $x = 1;\nmy $y = 2;\n";
let mapper = PositionMapper::new(text);

// Convert byte offset 0 → LSP position (line 0, char 0)
let pos = mapper.byte_to_lsp_pos(0);
assert_eq!(pos.line, 0);
assert_eq!(pos.character, 0);

// Second line starts at byte 11
let pos = mapper.byte_to_lsp_pos(11);
assert_eq!(pos.line, 1);
assert_eq!(pos.character, 0);

Implementations§

Source§

impl PositionMapper

Source

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

Create a new position mapper from text.

Detects line endings and builds an internal rope for efficient position conversions.

§Examples
use perl_position_tracking::PositionMapper;

let mapper = PositionMapper::new("print 'hello';\n");
let pos = mapper.byte_to_lsp_pos(6);
assert_eq!(pos.line, 0);
assert_eq!(pos.character, 6);
Source

pub fn update(&mut self, text: &str)

Update the text content

Source

pub fn apply_edit(&mut self, start_byte: usize, end_byte: usize, new_text: &str)

Apply an incremental edit

Source

pub fn lsp_pos_to_byte(&self, pos: Position) -> Option<usize>

Convert LSP position to byte offset.

Takes a line/character position (UTF-16 code units, as specified by the LSP protocol) and returns the corresponding byte offset in the source.

§Examples
use perl_position_tracking::{PositionMapper, WirePosition};

let mapper = PositionMapper::new("my $x = 1;\nmy $y = 2;\n");
// Line 1, character 3 → "$y"
let byte = mapper.lsp_pos_to_byte(WirePosition { line: 1, character: 3 });
assert_eq!(byte, Some(14));
Source

pub fn byte_to_lsp_pos(&self, byte_offset: usize) -> Position

Convert byte offset to LSP position.

Returns line/character (UTF-16 code units) suitable for LSP responses.

§Examples
use perl_position_tracking::PositionMapper;

let mapper = PositionMapper::new("sub foo {\n    return 1;\n}\n");
let pos = mapper.byte_to_lsp_pos(14);  // points into "return"
assert_eq!(pos.line, 1);
assert_eq!(pos.character, 4);
Source

pub fn text(&self) -> String

Get the text content

Source

pub fn slice(&self, start_byte: usize, end_byte: usize) -> String

Get a slice of text

Source

pub fn len_bytes(&self) -> usize

Get total byte length

Source

pub fn len_lines(&self) -> usize

Get total number of lines

Source

pub fn lsp_pos_to_char(&self, pos: Position) -> Option<usize>

Convert LSP position to char index (for rope operations)

Source

pub fn char_to_lsp_pos(&self, char_idx: usize) -> Position

Convert char index to LSP position

Source

pub fn is_empty(&self) -> bool

Check if empty

Source

pub fn line_ending(&self) -> LineEnding

Get line ending style

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.