aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/src/edit.rs
blob: 3edd0809dbf4dee36a2db902ad061530359cbfbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use {TextRange, TextUnit};

#[derive(Debug, Clone)]
pub struct Edit {
    atoms: Vec<AtomEdit>,
}

#[derive(Debug, Clone)]
pub struct AtomEdit {
    pub delete: TextRange,
    pub insert: String,
}

#[derive(Debug)]
pub struct EditBuilder {
    atoms: Vec<AtomEdit>
}

impl EditBuilder {
    pub fn new() -> EditBuilder {
        EditBuilder { atoms: Vec::new() }
    }

    pub fn replace(&mut self, range: TextRange, replacement: String) {
        self.atoms.push(AtomEdit { delete: range, insert: replacement })
    }

    pub fn delete(&mut self, range: TextRange) {
        self.replace(range, String::new());
    }

    pub fn insert(&mut self, offset: TextUnit, text: String) {
        self.replace(TextRange::offset_len(offset, 0.into()), text)
    }

    pub fn finish(self) -> Edit {
        let mut atoms = self.atoms;
        atoms.sort_by_key(|a| a.delete.start());
        for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
            assert!(a1.end() <= a2.start())
        }
        Edit { atoms }
    }
}

impl Edit {
    pub fn into_atoms(self) -> Vec<AtomEdit> {
        self.atoms
    }

    pub fn apply(&self, text: &str) -> String {
        let mut total_len = text.len();
        for atom in self.atoms.iter() {
            total_len += atom.insert.len();
            total_len -= atom.end() - atom.start();
        }
        let mut buf = String::with_capacity(total_len);
        let mut prev = 0;
        for atom in self.atoms.iter() {
            if atom.start() > prev {
                buf.push_str(&text[prev..atom.start()]);
            }
            buf.push_str(&atom.insert);
            prev = atom.end();
        }
        buf.push_str(&text[prev..text.len()]);
        assert_eq!(buf.len(), total_len);
        buf
    }

    pub fn apply_to_offset(&self, offset: TextUnit) -> Option<TextUnit> {
        let mut res = offset;
        for atom in self.atoms.iter() {
            if atom.delete.start() >= offset {
                break;
            }
            if offset < atom.delete.end() {
                return None
            }
            res += TextUnit::of_str(&atom.insert);
            res -= atom.delete.len();
        }
        Some(res)
    }
}

impl AtomEdit {
    fn start(&self) -> usize {
        u32::from(self.delete.start()) as usize
    }

    fn end(&self) -> usize {
        u32::from(self.delete.end()) as usize
    }
}