Lutra/crates/text/src/edit.rs
danny 5dac918b14 M1.1: text buffer with UTF-16 positions, edit batches and line endings
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>
2026-10-14 19:20:18 +02:00

36 lines
1,021 B
Rust

//! 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<usize>,
/// Replacement text.
pub text: String,
}
impl Edit {
/// Replaces `range` with `text`.
pub fn replace(range: Range<usize>, text: impl Into<String>) -> Self {
Self {
range,
text: text.into(),
}
}
/// Inserts `text` at byte offset `at`.
pub fn insert(at: usize, text: impl Into<String>) -> Self {
Self::replace(at..at, text)
}
/// Deletes the bytes in `range`.
pub fn delete(range: Range<usize>) -> Self {
Self::replace(range, String::new())
}
}