Skip to main content

escriba_core/
edit.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::range::Range;
5
6/// A primitive text mutation — the atom of undo/redo.
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
8pub struct Edit {
9    pub range: Range,
10    pub kind: EditKind,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
14pub enum EditKind {
15    /// Insert `text` at `range.start`; range.end is ignored.
16    Insert { text: String },
17    /// Delete `range`.
18    Delete,
19    /// Replace `range` with `text`.
20    Replace { text: String },
21}
22
23impl Edit {
24    #[must_use]
25    pub fn insert(at: crate::position::Position, text: impl Into<String>) -> Self {
26        Self {
27            range: Range::point(at),
28            kind: EditKind::Insert { text: text.into() },
29        }
30    }
31
32    #[must_use]
33    pub fn delete(range: Range) -> Self {
34        Self {
35            range,
36            kind: EditKind::Delete,
37        }
38    }
39
40    #[must_use]
41    pub fn replace(range: Range, text: impl Into<String>) -> Self {
42        Self {
43            range,
44            kind: EditKind::Replace { text: text.into() },
45        }
46    }
47}