redox_core/buffer/edit.rs
1//! Edit types for applying changes to a `TextBuffer`.
2//!
3//! This module is intentionally small and self-contained so it can be reused by
4//! higher-level systems (undo/redo, macros, repeat, etc).
5//!
6//! All indices are **character indices** (Unicode scalar values), matching
7//! `ropey`'s primary indexing model.
8
9/// A text edit expressed in character indices within the buffer.
10///
11/// The `range` is half-open: `[start, end)`.
12/// - To represent an insertion, use an empty range: `start == end`.
13/// - To represent a deletion, use an empty `insert` string.
14/// - To represent a replacement, set both a non-empty range and insert text.
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct Edit {
17 pub range: core::ops::Range<usize>,
18 pub insert: String,
19}
20
21impl Edit {
22 /// Create an insertion at the given char index.
23 pub fn insert(at_char: usize, text: impl Into<String>) -> Self {
24 Self {
25 range: at_char..at_char,
26 insert: text.into(),
27 }
28 }
29
30 /// Create a deletion for the given char range.
31 pub fn delete(range: core::ops::Range<usize>) -> Self {
32 Self {
33 range,
34 insert: String::new(),
35 }
36 }
37
38 /// Create a replacement for the given char range.
39 pub fn replace(range: core::ops::Range<usize>, text: impl Into<String>) -> Self {
40 Self {
41 range,
42 insert: text.into(),
43 }
44 }
45}