Skip to main content

perl_position_tracking/
lib.rs

1//! UTF-8/UTF-16 position tracking, conversion, and span types.
2//!
3//! This crate provides foundational types for source location tracking in the
4//! Perl LSP ecosystem:
5//!
6//! - [`ByteSpan`]: Byte-offset based spans for parser/AST use
7//! - [`LineStartsCache`]: Efficient line index for offset-to-position conversion
8//! - [`WirePosition`]/[`WireRange`]: LSP protocol-compatible position types
9//!
10//! # Example
11//!
12//! ```
13//! use perl_position_tracking::{ByteSpan, LineStartsCache};
14//!
15//! let source = "line 1\nline 2\nline 3";
16//! let cache = LineStartsCache::new(source);
17//!
18//! // Create a span covering "line 2"
19//! let span = ByteSpan::new(7, 13);
20//! assert_eq!(span.slice(source), "line 2");
21//!
22//! // Convert to line/column for LSP
23//! let (line, col) = cache.offset_to_position(source, span.start);
24//! assert_eq!(line, 1); // 0-indexed
25//! assert_eq!(col, 0);
26//! ```
27
28pub use convert::{offset_to_utf16_line_col, utf16_line_col_to_offset};
29pub use line_index::{LineIndex, LineStartsCache};
30pub use mapper::{
31    LineEnding, PositionMapper, apply_edit_utf8, json_to_position, last_line_column_utf8,
32    newline_count, position_to_json,
33};
34pub use position::{Position, Range};
35pub use span::{ByteSpan, SourceLocation};
36
37mod convert;
38mod line_index;
39pub mod mapper;
40mod position;
41mod span;
42
43mod wire;
44pub use wire::{WireLocation, WirePosition, WireRange};