//! Text edits addressed by byte ranges. use std::ops::Range; /// Replaces the bytes in `range` with `text`. /// /// An empty range inserts, an empty `text` deletes. Ranges are byte offsets /// that must lie on UTF-8 character boundaries of the text the edit is /// applied to; see [`crate::Buffer::apply`] for how a batch is interpreted. #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] pub struct Edit { /// Byte range to replace. pub range: Range, /// Replacement text. pub text: String, } impl Edit { /// Replaces `range` with `text`. pub fn replace(range: Range, text: impl Into) -> Self { Self { range, text: text.into(), } } /// Inserts `text` at byte offset `at`. pub fn insert(at: usize, text: impl Into) -> Self { Self::replace(at..at, text) } /// Deletes the bytes in `range`. pub fn delete(range: Range) -> Self { Self::replace(range, String::new()) } }