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
28#![warn(missing_docs)]
29
30pub use convert::{offset_to_utf16_line_col, utf16_line_col_to_offset};
31pub use line_index::{LineIndex, LineStartsCache};
32pub use mapper::{
33    LineEnding, PositionMapper, apply_edit_utf8, json_to_position, last_line_column_utf8,
34    newline_count, position_to_json,
35};
36pub use position::{Position, Range};
37pub use span::{ByteSpan, SourceLocation};
38
39mod convert;
40mod line_index;
41pub mod mapper;
42mod position;
43mod span;
44
45mod wire;
46pub use wire::{WireLocation, WirePosition, WireRange};