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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use xi_rope::Interval;

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use super::byte_rope::*;
use super::mode::*;
use super::selection::*;

#[derive(Debug, PartialEq, Clone, Copy, Eq, Hash)]
pub enum OverflowSelectionStyle {
    Cursor,
    Tail,
    CursorTail,
}

#[derive(Default)]
pub struct Buffer {
    pub path: Option<PathBuf>,
    pub data: Rope,
    pub selection: Selection,
    pub registers: HashMap<char, Vec<Vec<u8>>>,
    pub dirty: bool,
}

impl Buffer {
    pub fn from_data_and_path(data: Vec<u8>, path: Option<impl Into<PathBuf>>) -> Buffer {
        Buffer {
            data: data.into(),
            selection: Selection::new(),
            registers: HashMap::new(),
            dirty: false,
            path: path.map(Into::into),
        }
    }

    pub fn name(&self) -> String {
        if let Some(path) = &self.path {
            format!("{}", path.display())
        } else {
            "*scratch*".to_string()
        }
    }

    pub fn map_selections(&mut self, mut f: impl FnMut(SelRegion) -> Vec<SelRegion>) -> DirtyBytes {
        let mut invalidated_ranges = Vec::new();
        self.selection.map_selections(|region| {
            invalidated_ranges.push(Interval::from(region.min()..=region.max()));
            let new = f(region);
            for new_reg in new.iter() {
                invalidated_ranges.push(Interval::from(new_reg.min()..=new_reg.max()));
            }
            new
        });
        invalidated_ranges.sort_by(|a, b| a.start.cmp(&b.start));

        let mut disjoint_invalidated_ranges = Vec::new();
        for r in invalidated_ranges {
            if disjoint_invalidated_ranges.is_empty() {
                disjoint_invalidated_ranges.push(r);
                continue;
            }
            let last = disjoint_invalidated_ranges.last().unwrap();
            if last.contains(r.start) {
                *disjoint_invalidated_ranges.last_mut().unwrap() = last.union(r);
                continue;
            }
            disjoint_invalidated_ranges.push(r);
        }
        DirtyBytes::ChangeInPlace(disjoint_invalidated_ranges)
    }

    pub fn apply_delta(&mut self, delta: &RopeDelta) -> DirtyBytes {
        self.selection.apply_delta(&delta, self.data.len());
        self.data = self.data.apply_delta(&delta);
        self.dirty = true;

        DirtyBytes::ChangeLength
    }

    pub fn apply_delta_offset_carets(
        &mut self,
        delta: &RopeDelta,
        caret_offset: isize,
        tail_offset: isize,
    ) -> DirtyBytes {
        self.selection
            .apply_delta_offset_carets(delta, caret_offset, tail_offset, self.data.len());
        self.data = self.data.apply_delta(&delta);
        self.dirty = true;

        DirtyBytes::ChangeLength
    }

    fn switch_main_sel(&mut self, f: impl FnOnce(&mut Selection)) -> DirtyBytes {
        let old_main_sel_interval = self.selection.main().into();
        f(&mut self.selection);
        let new_main_sel_interval = self.selection.main().into();
        DirtyBytes::ChangeInPlace(vec![old_main_sel_interval, new_main_sel_interval])
    }

    fn modify_sels_in_place(&mut self, f: impl FnOnce(&mut Selection)) -> DirtyBytes {
        let dirty =
            DirtyBytes::ChangeInPlace(self.selection.iter().copied().map(Into::into).collect());
        f(&mut self.selection);

        dirty
    }

    pub fn remove_selection(&mut self, index: usize) -> DirtyBytes {
        self.modify_sels_in_place(|sel| sel.remove(index % sel.len()))
    }
    pub fn retain_selection(&mut self, index: usize) -> DirtyBytes {
        self.modify_sels_in_place(|sel| sel.retain(index % sel.len()))
    }
    pub fn select_next(&mut self, count: usize) -> DirtyBytes {
        self.switch_main_sel(|sel| sel.select_next(count))
    }
    pub fn select_prev(&mut self, count: usize) -> DirtyBytes {
        self.switch_main_sel(|sel| sel.select_prev(count))
    }

    pub fn yank_selections(&mut self, reg: char) {
        if self.data.is_empty() {
            self.registers
                .insert(reg, vec![vec![]; self.selection.len()]);
            return;
        }

        let selections = self
            .selection
            .iter()
            .map(|region| self.data.slice_to_cow(region.min()..=region.max()).to_vec())
            .collect();
        self.registers.insert(reg, selections);
    }

    pub fn overflow_sel_style(&self) -> Option<OverflowSelectionStyle> {
        let last_sel = self.selection.iter().last().unwrap();
        let len = self.data.len();
        if last_sel.caret == len && last_sel.tail == len {
            Some(OverflowSelectionStyle::CursorTail)
        } else if last_sel.caret == len {
            Some(OverflowSelectionStyle::Cursor)
        } else if last_sel.tail == len {
            Some(OverflowSelectionStyle::Tail)
        } else {
            None
        }
    }
}

pub struct Buffers {
    list: Vec<Buffer>,
    cur_buf_index: usize,
}

impl Buffers {
    pub fn new() -> Buffers {
        Buffers::with_buffer(Buffer::default())
    }

    pub fn with_buffer(buf: Buffer) -> Buffers {
        Buffers {
            cur_buf_index: 0,
            list: vec![buf],
        }
    }

    pub fn current(&self) -> &Buffer {
        &self.list[self.cur_buf_index]
    }
    pub fn current_mut(&mut self) -> &mut Buffer {
        &mut self.list[self.cur_buf_index]
    }

    pub fn iter(&self) -> impl Iterator<Item = &Buffer> {
        self.list.iter()
    }
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Buffer> {
        self.list.iter_mut()
    }

    pub fn switch_buffer(&mut self, filename: impl AsRef<Path>) -> Result<(), std::io::Error> {
        let canon = filename.as_ref().canonicalize()?;
        for (i, buf) in self.list.iter().enumerate() {
            if let Some(path) = &buf.path {
                if path.canonicalize()? == canon {
                    self.cur_buf_index = i;
                    return Ok(());
                }
            }
        }

        self.list.push(Buffer::from_data_and_path(
            std::fs::read(&filename)?,
            Some(filename.as_ref().to_owned()),
        ));
        self.cur_buf_index = self.list.len() - 1;
        Ok(())
    }

    pub fn delete_current(&mut self) {
        self.list.remove(self.cur_buf_index);
        self.cur_buf_index = self.cur_buf_index.saturating_sub(1);
        if self.list.is_empty() {
            self.list.push(Buffer::default());
        }
    }
}