lutra-text gets Buffer on a ropey rope: byte/char/UTF-16/line counts, byte offset <-> (line, column) in UTF-16 code units or chars, absolute UTF-16 offsets, atomic Edit batches in LSP TextEdit[] semantics, and \n / \r\n / \r detection with a keep-the-file's-ending save policy. All conversions return TextError instead of panicking. ropey 1.6.1 (MIT) without unicode_lines, so only the LSP/CodeMirror line breaks split lines. proptest moves into workspace deps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
39 lines
1.5 KiB
Rust
39 lines
1.5 KiB
Rust
//! Line and column positions.
|
|
//!
|
|
//! LSP counts columns in UTF-16 code units by default
|
|
//! (<https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocuments>)
|
|
//! and CodeMirror addresses text in UTF-16 code units
|
|
//! (<https://codemirror.net/docs/ref/#state.Text>), so UTF-16 is the unit
|
|
//! that crosses the frontend and LSP boundaries. Chars (Unicode scalar
|
|
//! values) are offered for Rust-side code that walks text by `char`.
|
|
|
|
/// The unit a column is counted in.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
pub enum ColumnUnit {
|
|
/// UTF-16 code units: characters outside the Basic Multilingual Plane,
|
|
/// such as most emoji, count two.
|
|
Utf16,
|
|
/// Unicode scalar values (Rust `char`s): every code point counts one,
|
|
/// including each combining mark.
|
|
Char,
|
|
}
|
|
|
|
/// A zero-based line and column.
|
|
///
|
|
/// The column's unit is not stored; conversions take a [`ColumnUnit`]. A
|
|
/// column never reaches into the line break: the largest valid column is
|
|
/// the line's length without its line break.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
|
pub struct Position {
|
|
/// Zero-based line index.
|
|
pub line: usize,
|
|
/// Zero-based column in the unit given to the conversion.
|
|
pub column: usize,
|
|
}
|
|
|
|
impl Position {
|
|
/// Creates a position from a zero-based line and column.
|
|
pub const fn new(line: usize, column: usize) -> Self {
|
|
Self { line, column }
|
|
}
|
|
}
|