//! Ranges in line and UTF-16 column coordinates. use lutra_text::{Buffer, ColumnUnit, Position}; /// A range between two positions, end exclusive. /// /// Lines and columns are zero-based; columns count UTF-16 code units, as /// CodeMirror and LSP do. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] pub struct Range { /// First position inside the range. pub start: Position, /// Position just after the range. pub end: Position, } impl Range { /// Converts the byte range `start..end` of `text` into a [`Range`]. /// /// Returns `None` when either offset is past the end of the text, inside /// a character or inside a CRLF line break. pub(crate) fn from_bytes(text: &Buffer, start: usize, end: usize) -> Option { Some(Self { start: text.offset_to_position(start, ColumnUnit::Utf16).ok()?, end: text.offset_to_position(end, ColumnUnit::Utf16).ok()?, }) } }