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
impl PositionMapper
Sourcepub fn new(text: &str) -> Self
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);Sourcepub fn apply_edit(&mut self, start_byte: usize, end_byte: usize, new_text: &str)
pub fn apply_edit(&mut self, start_byte: usize, end_byte: usize, new_text: &str)
Apply an incremental edit
Sourcepub fn lsp_pos_to_byte(&self, pos: Position) -> Option<usize>
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));Sourcepub fn byte_to_lsp_pos(&self, byte_offset: usize) -> Position
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);Sourcepub fn lsp_pos_to_char(&self, pos: Position) -> Option<usize>
pub fn lsp_pos_to_char(&self, pos: Position) -> Option<usize>
Convert LSP position to char index (for rope operations)
Sourcepub fn char_to_lsp_pos(&self, char_idx: usize) -> Position
pub fn char_to_lsp_pos(&self, char_idx: usize) -> Position
Convert char index to LSP position
Sourcepub fn line_ending(&self) -> LineEnding
pub fn line_ending(&self) -> LineEnding
Get line ending style
Auto Trait Implementations§
impl Freeze for PositionMapper
impl RefUnwindSafe for PositionMapper
impl Send for PositionMapper
impl Sync for PositionMapper
impl Unpin for PositionMapper
impl UnsafeUnpin for PositionMapper
impl UnwindSafe for PositionMapper
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